An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

## SwiftAPIClient

![Group 3032 (3)](https://user-images.githubusercontent.com/11628358/223416855-71d0c02f-ee39-48f5-a5d4-4442e2a5089e.png)

[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fhromni%2FSwiftAPIClient%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/hromni/SwiftAPIClient)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fhromni%2FSwiftAPIClient%2Fbadge%3Ftype%3Dswift-versions)](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.