The Apifreaks Swift library provides convenient access to the Apifreaks APIs from Swift.
This SDK requires:
- Swift 5.7+
- iOS 15+
- macOS 12+
- tvOS 15+
- watchOS 8+
With Swift Package Manager (SPM), add the following to the top-level dependencies array within your Package.swift file:
dependencies: [
.package(url: "<git-url>", from: "1.0.1"),
]A full reference for this library is available here.
Instantiate and use the client with the following:
import Foundation
import APIFreaks
private func main() async throws {
let client = APIFreaks()
_ = try await client.bulkGeolocationLookup(
apiKey: "apiKey",
request: .init(ips: [
"ips"
])
)
}
try await main()This SDK allows you to configure different environments for API requests.
import APIFreaks
let client = APIFreaks(
...,
environment: .default
)The SDK throws a single error enum for all failures. Client-side issues encoding/decoding failures and network errors use dedicated cases, while non-success HTTP responses are wrapped in an HTTPError that exposes the status code, a simple classification and an optional decoded message.
import APIFreaks
let client = APIFreaks(...)
do {
let response = try await client.bulkGeolocationLookup(...)
// Handle successful response
} catch let error as APIFreaksError {
switch error {
case .httpError(let httpError):
print("Status code:", httpError.statusCode)
print("Kind:", httpError.kind)
print("Message:", httpError.body?.message ?? httpError.localizedDescription)
case .encodingError(let underlying):
print("Encoding error:", underlying)
case .networkError(let underlying):
print("Network error:", underlying)
default:
print("Other client error:", error)
}
} catch {
print("Unexpected error:", error)
}The SDK exports all request types as Swift structs. Simply import the SDK module to access them:
import APIFreaks
let request = Requests.BulkGeolocationLookupRequest(
...
)If you would like to send additional headers as part of the request, use the additionalHeaders request option.
try await client.bulkGeolocationLookup(..., requestOptions: .init(
additionalHeaders: [
"X-Custom-Header": "custom value"
]
))If you would like to send additional query string parameters as part of the request, use the additionalQueryParameters request option.
try await client.bulkGeolocationLookup(..., requestOptions: .init(
additionalQueryParameters: [
"custom_query_param_key": "custom_query_param_value"
]
))The SDK defaults to a 60-second timeout. Use the timeout option to configure this behavior.
try await client.bulkGeolocationLookup(..., requestOptions: .init(
timeout: 30
))The SDK allows you to customize the underlying URLSession used for HTTP requests. Use the urlSession option to provide your own configured URLSession instance.
import Foundation
import APIFreaks
let client = APIFreaks(
...,
urlSession: // Provide your implementation here
)While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!