This lib is about network requests with blackjack, roulette and craps!
Using it you will be able to convert your massive API layer code into an awesome convenient controllers with easy and maintainable network requests!
Use Codable models for everything related to API requests:
- json payload
- url-encoded payload
- multipart payload
- plain and json response
- url query
- headers
Wondered? That's only little part of what you will get from this lib! ?
Quick examples
How to send GET request
How to send POST request
How to send DELETE request
How to send several requests?
You're able to run up to 10 requests one-by-one!
Or you can run unlimited amount of requests one-by-one or at the same time, if you need just a completion handler.
to run them concurrently just add .concurrent(by: 3)
to run by 3 at the same time
Of course you'll be able to send PUT and PATCH requests, send multipart codable structs with upload progress callback, catch errors, even redefine error descriptions for every endpoint. Wondered? ? Let's read the whole readme below! ?
How to install
CodyFire is available through CocoaPods and SPM.
To install it, simply add the following line in your Podfile:
Or you're looking for reactive code support? I have it! ?
- for RxSwift https://github.com/MihaelIsaev/RxCodyFire
using this pod you should always import just RxCodyFire
and every APIRequest will have .observable
- for ReactiveCocoa https://github.com/MihaelIsaev/ReactiveCodyFire
using this pod you should always import just ReactiveCodyFire
and every APIRequest will have .signalProducer
How to setup
CodyFire automatically detects which environment you're on, so I suggest you to definitely use this awesome feature ?
Isn't it cool? ?
Or you can set different Server URL for every APIRequest
And then initialize your APIRequest
s like this ?
Or in some cases you even can do it like this ?
Create your API controllers
I promise that this is API code architecture from your dreams which are come true!
Create an API
folder and API.swift
file inside it
Create a folder named Controllers
inside API
folder, and create a folder for each controller
API/Controllers/Auth/Auth.swift
API/Controllers/Task/Task.swift
Create an extension file for each controller's endpoint
Auth login as simple POST request
API/Controllers/Auth/Auth+Login.swift
Auth login for Basic auth
API/Controllers/Auth/Auth+Login.swift
Task REST endpoints
Get task by id or a list of tasks by offset and limit
API/Controllers/Task/Task+Get.swift
Create a task
API/Controllers/Task/Task+Create.swift
Edit a task
API/Controllers/Task/Task+Edit.swift
Delete a task
API/Controllers/Task/Task+Delete.swift
Easily use your API endpoints!
Send login request
Get a list of tasks
Create a task
Delete a task
Multipart example
Easy right? ?
Details
How to put Authorization Bearer
token into every request?
For that we have a global headers wrapper, which is called for every request.
You need to declare it e.g. somewhere in AppDelegate.
There are two options
- Use Codable model for headers (recommended)
- Use [String: String] dictionary
How to set a global unauthorized
handler?
Again, somewhere in AppDelegate declare it like this
CodyFire.shared.unauthorizedHandler = {
//kick out user
}
Handle if network isn't available (e.g. wifi/lte turned off)
Run something right before request started (works only if network is available)
How to avoid log error for request
How to set desired status code and what's that means?
Usually servers response with 200 OK
and CodyFire expect to receive 200 OK
to call onSuccess
handler by default.
You may need to specify another code, e.g. 201 CREATED
for some POST requests.
or you even can set your custom code
How to set some headers for a request?
Supported HTTP methods
You may use: GET, POST, PUT, PATCH, DELETE, HEAD, TRACE, CONNECT, OPTIONS
How to switch environments through Xcode's run schemes?
It's really useful feature and I suggest to use it in every iOS project!
Create three schemes named: Development, TestFlight, AppStore like on the screenshot below
TIP: Make sure that they're marked as Shared
to have them in git
Then in every scheme in Arguments
tab add Environment variable
named env
with one of those values: dev, testFlight, appStore.
Take a look at example below
Then somewhere in AppDelegate.didFinishLaunchingWithOptions add
All done, now you're able to easily switch environments!
How to execute request without onSuccess clojure?
Sometimes useful for DELETE or PATCH requests
How to cancel request?
and you're able to handle cancellation
What does custom error means?
You may define your own custom errors, globally or for each request.
onError
block contains NetworkError
object with StatusCode
enum, an error description, and a raw response Data
. Error description you could change to whatever you want for any error code.
By default there are already defined some good descriptions for common errors.
Let's take a look how we can use powerful onError
block
More than that!!! In your controller while declaring APIRequest you're able to add your own custom errors!!! ?
I believe that's really awesome and useful! Finally a lot of things may be declared in one place! ?
How to set response timeout?
and of course you're able to catch timeout
How to add interactive additional timeout? (my favourite one ?)
If you want to make sure that your request will take 2 or more seconds (to not be too fast ?) you can do that
e.g. in case if your request will be executed in 0.5 seconds, onSuccess
handler will be fired only in 1.5s after that
but in case if your request will take more than 2s then onSuccess
handler will be fired immediatelly
How to declare payload model for multipart
request
Your struct/class just should conform to MultipartPayload
protocol
Supported payload types
You may conform your struct/class to: FormURLEncodedPayload
, MultipartPayload
, and JSONPayload
How to declare payload model for json
request
Your struct/class just should conform to JSONPayload
protocol
How to declare payload model for x-www-form-urlencoded
request
Your struct/class just should conform to FormURLEncodedPayload
protocol
How to declare url query params model
Your struct/class just should conform to Codable
protocol
How to set date decoding/encoding strategy
Our DateCodingStrategy
support
- secondsSince1970
- millisecondsSince1970
- formatted(customDateFormatter: DateFormatter)
By default all the dates are inyyyy-MM-dd'T'HH:mm:ss'Z'
format
You have interesting options here:
- you can set global date decoder/encoder
- you can set date decoder/encoder for request in your controller
- or you even can use different date encoder/decoder for each payload type (highest priority)
How to enable/disable logging
e.g. in AppDelegate you may set logging mode
and also you can set log handler
by default for the AppStore the log level if .off
How you're detecting current environment?
It's easy
Chained requests
Run up to 10 requests one-by-one!
onRequestStarted, onNetworkUnavailable, onCancellation, onNotAuthorized, onTimeout also available!
//TBD: onProgress
I believe it is awesome! Especially for whom who not familiar or don't like reactive programming ?
Flatten
If you want to run several requests one-by-one or at the same time but with just completion handler you can do that with .flatten()
to run them concurrently just add .concurrent(by: 3)
to run by 3 at the same time
to skip errors also add .avoidCancelOnError()
to get progress add .onProgress
Contribution
Please feel free to send pull requests and ask your questions in issues
Hope this lib will be really useful in your projects! Tell you friends about it! Please press STAR ⭐️ button!!!
Author
Mike Isaev, isaev.mihael@gmail.com