## UnboxedAlamofire
UnboxedAlamofire is a powerful networking library built on top of Alamofire that simplifies the process of making network requests in Swift. It provides a set of convenient and easy-to-use features to handle common networking tasks, such as making GET and POST requests, handling responses, and working with JSON and Codable models.
### Features
#### 1. Simple Request Building
UnboxedAlamofire allows you to easily create network requests using a simple request builder. You can specify the HTTP method, URL, headers, parameters, and encoding in a clear and concise syntax.
“`swift
import UnboxedAlamofire
// Create a GET request
let request = UnboxedAlamofire.request(.get, “https://api.example.com/posts”)
// Set headers and parameters
request
.header(“Authorization”, “Bearer your_token”)
.parameters([“category”: “news”])
// Send the request and handle the response
request.responseJSON { response in
switch response.result {
case .success(let value):
// Do something with the response
break
case .failure(let error):
// Handle the error
break
}
}
“`
#### 2. Codable Support
UnboxedAlamofire simplifies the process of parsing JSON responses into Swift Codable models. You can easily define a Codable model and use it to decode the JSON response using the `responseDecodable` method.
“`swift
struct Post: Codable {
let id: Int
let title: String
let body: String
}
UnboxedAlamofire.request(.get, “https://api.example.com/posts”)
.responseDecodable { (response: DataResponse<[Post]>) in
switch response.result {
case .success(let posts):
// Do something with the decoded posts
break
case .failure(let error):
// Handle the error
break
}
}
“`
#### 3. Error Handling
UnboxedAlamofire provides a convenient way to handle errors when making network requests. You can use the `responseError` method to handle error responses or network failures.
“`swift
UnboxedAlamofire.request(.get, “https://api.example.com/posts”)
.responseError { (response: DataResponseError) in
switch response {
case .success(let value):
// Handle a successful response
break
case .error(let error):
// Handle the error
break
case .networkError(let error):
// Handle a network error
break
}
}
“`
#### 4. Response Validation
UnboxedAlamofire allows you to validate the response using different validators. You can use the pre-defined validators or create your own custom validators.
“`swift
UnboxedAlamofire.request(.get, “https://api.example.com/posts”)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch response.result {
case .success(let value):
// Do something with the response
break
case .failure(let error):
// Handle the error
break
}
}
```
### Installation
UnboxedAlamofire is available through Cocoapods. To integrate it into your Xcode project, specify it in your `Podfile`:
“`ruby
pod ‘UnboxedAlamofire’
“`
Then run the following command:
“`bash
$ pod install
“`
### Requirements
UnboxedAlamofire has the following requirements:
– Swift 4.2 or later
– Alamofire 5.0 or later