aporat/APSignedAPIClient
APSignedAPIClient is a Swift package for making signed API requests with HMAC-SHA256 authentication
Features
- Signed Requests: Automatically signs requests with HMAC-SHA256 using a client key.
- Authentication: Supports account and user authentication tokens, plus device ID headers.
- Configurable: Set base URL, app name, client version, and more via a single setup call.
- Error Handling: Maps API-specific status codes to meaningful errors using
APWebAuthentication. - Retry Logic: Includes a retrier for transient network failures and server errors.
Requirements
- iOS 16.0+
- Swift 5.9+
- Xcode 15.0+
Installation
Swift Package Manager
Add APSignedAPIClient to your project via Swift Package Manager:
- In Xcode, go to File > Add Packages.
- Enter the repository URL:
`` https://github.com/aporat/APSignedAPIClient.git ``
- Specify the version (e.g.,
1.0.0) or use the latest commit. - Add the package to your target.
Alternatively, add it to your Package.swift:
dependencies: [
.package(url: "https://github.com/aporat/APSignedAPIClient.git", from: "1.0.0")
]Then, include it in your target:
.target(
name: "YourApp",
dependencies: ["APSignedAPIClient"]
)Usage
Setup
Configure the client with your API credentials and settings:
import APSignedAPIClient
CoreAPIClient.setup(
baseURLString: "https://api.example.com",
appName: "MyApp",
clientVersion: "400",
clientId: "your-client-id",
clientKey: "your-client-key",
userAgent: "MyApp/1.0"
)Making a Request
Create an instance of CoreAPIClient and send a signed request:
let client = CoreAPIClient()
do {
let request = try client.request(
"/endpoint",
method: .post,
parameters: ["key": "value"]
)
request.responseData { response in
switch response.result {
case .success(let data):
print("Response: \(data)")
case .failure(let error):
print("Error: \(client.generateError(response))")
}
}
} catch {
print("Request creation failed: \(error)")
}Dependencies
- Alamofire (5.0.0+): Networking
- CryptoSwift (1.8.0+): HMAC-SHA256 signing
- APWebAuthentication: Error types
- SwiftyUserDefaults (5.0.0+): User defaults (optional)
- SwiftyJSON (5.0.0+): JSON parsing
Package Metadata
Repository: aporat/APSignedAPIClient
Stars: 0
Forks: 1
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md