APSignedAPIClient is a Swift package for making signed API requests with HMAC-SHA256 authentication. It provides a flexible, reusable client for iOS applications, leveraging Alamofire for networking. This package is designed to simplify secure API communication by handling request signing, authentication tokens, and error handling out of the box.
- 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.
- iOS 16.0+
- Swift 5.9+
- Xcode 15.0+
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"]
)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"
)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)")
}- 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