Skip to content

doxuto/APIClient

Repository files navigation

CI Swift Platforms

README.md

# 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")
]

Usage

  1. 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)
 }
    
  1. If you want to customize the Validator of APIClient, you can do that in the contructor APIClient
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)

Dependencies

  • No external dependencies required.

Structure

  • APIClient: Main module for handling API requests.
  • APIClientTests: Unit tests for the APIClient module.

Contribution

Feel free to contribute by forking the repository and submitting pull requests.

License

This package is released under the MIT License. See LICENSE file for more details.

About

A simply networking layer in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages