IzziRequest is a super lightweight Swift package that helps you efficiently manage network calls in your application. It supports all key aspects of modern networking, including HTTP methods, request bodies, headers, timeout control, and flexible caching.
- Flexible code
- Support for HTTP methods: GET, POST, PUT, PATCH, DELETE
- Customizable timeoutInterval to prevent the app from freezing
- Supports file upload using multipart requests
- Caching for GET requests to optimize performance.
- Customizable caching duration.
- Supports auto encoding and decoding to snake_case
- Well-defined error handling
- Works with any
Codable
model using generics - Uses
async/await
for better performance - Automatically sets
Content-Type
based on the request body - Supports custom headers for additional flexibility
Parameter | Type | Description | Default |
---|---|---|---|
urlString |
String |
API endpoint URL. | N/A |
method |
HTTPMethod |
HTTP request method. | N/A |
body |
Codable |
Request payload. | N/A |
headers |
[String: String] |
HTTP headers. | N/A |
timeoutInterval |
TimeInterval |
Request timeout duration. | nil |
useCache |
Bool |
Enables caching for GET requests. | false |
cacheExpiry |
TimeInterval |
Cache validity duration. | 300.0 |
When you have a request model using snake_case
, it automatically converts to camelCase
in Swift.
{
"first_name": "Hablo",
"last_name": "Escobar"
}
Swift decodes it as:
struct MyModel: Codable {
let firstName: String
let lastName: String
}
By default, IzziRequest has a timeout interval of 30 seconds to prevent the app from freezing.
To start using IzziRequest, inject it into your ViewModel
:
import IzziRequest
final class ViewModel {
private let izziReq: IzziRequestProtocol
init(izziReq: IzziRequestProtocol = IzziRequest()) {
self.izziReq = izziReq
}
}
⏳ If you want to set a custom timeout interval, you can do it like this:
import IzziRequest
final class ViewModel {
private let izziReq: IzziRequestProtocol
init(izziReq: IzziRequestProtocol = IzziRequest(defaultTimeout: 60)) {
self.izziReq = izziReq
}
}
A basic GET request without headers or custom timeout.
let api = "http://localhost:3002/get_method"
func foo() {
Task {
do {
let response: MyResponseModel = try await izziReq.request(urlString: api, method: .GET)
print(response)
} catch {
print(error)
}
}
}
Pass custom headers, such as an authorization token.
let api = "http://localhost:3002/get_method"
let headers = ["Authorization" : "Bearer token"]
func foo() {
Task {
do {
let response: MyResponseModel = try await izziReq.request(
urlString: api,
method: .GET,
headers: headers
)
print(response)
} catch {
print(error)
}
}
}
Specify a custom timeout interval (e.g., 120 seconds).
let api = "http://localhost:3002/get_method"
let headers = ["Authorization" : "Bearer token"]
let interval: TimeInterval = 120
func foo() {
Task {
do {
let response: MyResponseModel = try await izziReq.request(
urlString: api,
method: .GET,
headers: headers,
timeoutInterval: interval
)
print(response)
} catch {
print(error)
}
}
}
Send a POST request with headers and a request body.
let api = "http://localhost:3002/post_method"
let headers = ["Authorization" : "Bearer token"]
let reqBody = LoginModel(email: "example@test.com", password: "12345678")
func foo() {
Task {
do {
let response: MyResponseModel = try await izziReq.request(
urlString: api,
method: .POST,
headers: headers,
body: reqBody
)
print(response)
} catch {
print(error)
}
}
}
Set a custom timeout interval (e.g., 15 seconds).
let api = "http://localhost:3002/post_method"
let headers = ["Authorization" : "Bearer token"]
let reqBody = LoginModel(email: "example@test.com", password: "12345678")
let interval: TimeInterval = 15
func foo() {
Task {
do {
let response: MyResponseModel = try await izziReq.request(
urlString: api,
method: .POST,
headers: headers,
body: reqBody,
timeoutInterval: interval
)
print(response)
} catch {
print(error)
}
}
}
💡 You can also use the DELETE, PUT, and PATCH methods, just like we had above with the GET and POST methods.
use with default cacheExpiry duration (5 minutes)
let api = "http://localhost:3002/get_method_with_caching"
func foo() {
Task {
do {
let response: MyResponseModel = try await izziReq.request(
urlString: api,
method: .GET,
useCache: true
)
print(response)
} catch {
print(error)
}
}
}
or use with custom cacheExpiry duration
let api = "http://localhost:3002/get_method_with_caching"
func foo() {
Task {
do {
let response: MyResponseModel = try await izziReq.request(
urlString: api,
method: .GET,
useCache: true,
cacheExpiry: 600.0
)
print(response)
} catch {
print(error)
}
}
}
- Open your project.
- Go to File → Add Package Dependencies.
- Enter URL: https://github.com/Desp0o/izziRequest.git.
- Click Add Package.