To run the example project, clone the repo, and run pod install
from the Example directory first.
GenAPI utilizes Swift generics for you to easily define the return types of your API calls.
let userAPIObject = APIObject<User, DefaultError>(success: { (user) in
print(user)
}, failure:{ (apiError) in
})
userAPIObject.baseURL = URL(string:"https://jsonplaceholder.typicode.com")
userAPIObject.endPoint = "/users/1"
userAPIObject.debugOptions = .printDetailedTransaction
userAPIObject.get()
All that is required is that your return types conform to the protocol Decodable
.
struct User: Decodable{
struct Company: Decodable{
var name: String
var catchPhrase: String
}
var id: Int
var name: String?
var company: Company?
}
The APIError
enum breaks down any error that occurs into 1 of 3 different possibilities:
- a session error
- a coding error
- or an API error
A session error is returned if there is an error with the Session
.
A coding error is returned if an error is thrown when trying to decode either the Response type OR the Error type. (A coding error is most commonly a DecodingError
.)
An API error is returned if the HTTP status code of the response is determined to be outside of the success range.
The APIObject
class has many convenience functions to easily manipulate the underlying URLRequest
, but the request is also publicly available in case you need to do any custom configuration. Simply access with APIObject.request
.
GenAPI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'GenAPI'
Lucas Best, [email protected]
GenAPI is available under the MIT license. See the LICENSE file for more info.