https://github.com/hromni/swiftapiclient
Simple, flexible, generic and easy to use HTTP API Client written in Swift
https://github.com/hromni/swiftapiclient
api-client async async-await combine combine-framework http-client http-requests ios macos networking rest-api rest-client swift swift-api swift-api-client swiftpackage swiftpm tvos
Last synced: 8 months ago
JSON representation
Simple, flexible, generic and easy to use HTTP API Client written in Swift
- Host: GitHub
- URL: https://github.com/hromni/swiftapiclient
- Owner: hromni
- License: mit
- Created: 2023-03-06T10:06:05.000Z (over 3 years ago)
- Default Branch: develop
- Last Pushed: 2025-09-11T08:22:27.000Z (9 months ago)
- Last Synced: 2025-10-20T06:11:00.047Z (8 months ago)
- Topics: api-client, async, async-await, combine, combine-framework, http-client, http-requests, ios, macos, networking, rest-api, rest-client, swift, swift-api, swift-api-client, swiftpackage, swiftpm, tvos
- Language: Swift
- Homepage:
- Size: 51.8 KB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## SwiftAPIClient

[](https://swiftpackageindex.com/hromni/SwiftAPIClient)
[](https://swiftpackageindex.com/hromni/SwiftAPIClient)
Light weight and simplistic API Client written in Swift using protocol oriented programming. You can send requests using `async await` or `Combine` publishers without having to change anything. **SwiftAPIClient** can help you implement your HTTP and server API calls with just a couple lines of code.
Open Sourced by [HR Omni Solutions](https://www.hromni.com)
| Table of contents |
| --- |
| [Install with SPM](#spm) |
| [Define your endpoints using enum](#enum-endpoints) |
| [Define endpoint using struct](#struct-endpoint) |
| [Decode JSON responses](#decode-json) |
| [Send request using Combine](#send-combine) |
| [Send request using async](#send-async) |
| [Response validation](#response-validation) |
| [Contribution](#contribution) |
### Swift Package Manager
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
Once you have your Swift package set up, adding SwiftAPIClient as a dependency is as easy as adding it to the dependencies value of your Package.swift.
```swift
dependencies: [
.package(url: "https://github.com/hromni/SwiftAPIClient.git", .upToNextMajor(from: "0.1"))
]
```
### Cocoapods
```
pod 'HROmni.SwiftApiClient'
```
or
```
pod 'HROmni.SwiftApiClient', :git => 'git@github.com:hromni/SwiftAPIClient.git'
```
### Define your endpoints using enum
```swift
import Foundation
import SwiftAPIClient
enum Endpoints: Endpoint {
case getData, addData(_ name: String)
var baseUrlString: String { "https://example.com/" }
var httpBody: RequestBody? {
switch self {
case .addData(let name):
return .jsonDictionary(["name" : name])
default: return nil
}
}
var httpMethod: HTTPMethod {
switch self {
case .getData: return .get
case .addData: return .post
}
}
var path: String {
// URL path should always start with forward slash
switch self {
case .getData:
return "/getData"
case .addData(let data):
return "/addData"
}
}
}
```
### Endpoint example using `struct`
```swift
struct GetDataEndpoint: Endpoint {
var baseUrlString: String { "https://example.com/" }
// URL path should always start with forward slash
var path: String { "/getData" }
}
```
### Decode JSON responses
If you're expecting a JSON response you can use `JsonResponse` protocol which is a wrapper of `Decodable` with some extra build-in functionality. You can also create your own response type by conforming to `Response` protocol.
```swift
import Foundation
import SwiftAPIClient
struct ExampleDataResponse: JsonResponse {
let name: String
}
```
### Send request using Combine
Create client wrapper with `Combine` using the endpoints
```swift
struct ApiClient {
static func getData() -> AnyPublisher {
Endpoints.getData.send()
// GetDataEndpoint().send()
}
}
```
### Send request using async
As mentioned the endpoint automatically handles both `Combine` and `async`, so you can use either approach.
For example if you want to use `async` all you need to do is replace `send()` with `asyncSend()` using the same `Endponts` definition above
```swift
struct ApiClient {
static func getData() async throws -> ExampleDataResponse {
try await Endpoints.getData.asyncSend()
// try await GetDataEndpoint().asyncSend()
}
}
```
### Validating responses
The default validator code is below.
```swift
struct BasicResponseValidator: ResponseValidator {
public func validate(_ response: (data: Data, response: URLResponse)) throws {
if let statusCode = (response.response as? HTTPURLResponse)?.statusCode,
statusCode >= 300 {
throw SwiftApiClientError.serverError(statusCode: statusCode, payload: response.data)
}
}
}
```
You can also create your own validator by conforming to
```
public protocol ResponseValidator {
func validate(_ response: (data: Data, response: URLResponse)) throws
}
```
and then pass it as a validator to your endpoints to replace the default response validation
### Contribution
Contributors are welcome.