# APIClient
## Description
A simply networking layer in Swift that allows you to easily make HTTP requests and handle response data.
## Installation
To use the APIClient module in your Swift project, include this package as a dependency in your Package.swift file.
```swift
dependencies: [
.package(url: "https://github.com/doxuto/APIClient.git", from: "1.0.0")
]
- Import the APIClient module in your Swift files where you need to make API requests.
import APIClient
struct UserEndpoint: Endpoint {
var url: URL = URL(string: "https://httpbin.org/get")!
var requestMethod: RequestMethod = RequestMethod.get
var headers: [String : String]? = nil
var parameters: [String : String]? = nil
var timeoutInterval: TimeInterval = 60
}
let apiClient = APIClient(
validator: DefaultValidator(),
urlSession: URLSession.shared,
jsonDecoder: JSONDecoder()
)
let endpoint = UserEndpoint()
let user: User = try await apiClient.request(endpoint: endpoint)
Or you can use Combine's Publisher
type to handle API responses.
func fetchUser() -> AnyPublisher<User, Error> {
let endpoint = UserEndpoint()
return apiClient.request(endpoint: endpoint)
}
- If you want to customize the
Validator
of APIClient, you can do that in the contructorAPIClient
struct CustomizedValidator: Validator {
func validate(for response: HTTPURLResponse) throw -> Bool {
let statusCode = response.statusCode
return statusCode == 200
}
}
let apiClient = APIClient(
validator: CustomizedValidator(),
urlSession: URLSession.shared,
jsonDecoder: JSONDecoder()
)
let endpoint = UserEndpoint()
let user: User = try await apiClient.request(endpoint: endpoint)
- No external dependencies required.
- APIClient: Main module for handling API requests.
- APIClientTests: Unit tests for the APIClient module.
Feel free to contribute by forking the repository and submitting pull requests.
This package is released under the MIT License. See LICENSE file for more details.