HTTPAPIClient
open class HTTPAPIClient
A class responsible for sending HTTP API requests and handling responses.
The HTTPAPIClient
class provides methods to send HTTP API requests using different encoding formats
and handle the responses. It supports callbacks, async-await, and Combine for handling responses.
-
The URL session provider used for sending requests.
Declaration
Swift
public private(set) var session: URLSessionProvider { get }
-
The base URL for the API requests.
Declaration
Swift
public var url: URL
-
An optional path prefix to be appended to the base URL.
Declaration
Swift
public var pathPrefix: String?
-
Initializes a new instance of
HTTPAPIClient
with the specified URL, path prefix, and session provider.Declaration
Swift
public init(url: URL, pathPrefix: String? = nil, session: URLSessionProvider = URLSession.shared)
Parameters
url
The base URL for the API requests.
pathPrefix
An optional path prefix to be appended to the base URL.
session
The URL session provider used for sending requests. Defaults to
URLSession.shared
.
-
Sends an HTTP API request using a callback to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myRequest) { result in switch result { case .success(let response): print("Received response: \(response)") case .failure(let error): print("Request failed with error: \(error)") } }
Declaration
Swift
open func send<R>(_ request: R, receiveOn queue: DispatchQueue? = nil, callback: @escaping (Result<R.Response, Error>) -> Void) where R : Request
Parameters
request
The request to send.
queue
An optional dispatch queue to receive the callback on. Defaults to
nil
.callback
The callback to handle the response.
-
Sends an HTTP API request with a JSON body using a callback to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myJsonRequest) { result in switch result { case .success(let response): print("Received response: \(response)") case .failure(let error): print("Request failed with error: \(error)") } }
Declaration
Swift
open func send<Request: JSONRequest>( _ request: Request, receiveOn queue: DispatchQueue? = nil, callback: @escaping ( Result<Request.Response, Error> ) -> Void )
Parameters
request
The request to send.
queue
An optional dispatch queue to receive the callback on. Defaults to
nil
.callback
The callback to handle the response.
-
Sends an HTTP API request with a Form URL Encoded body using a callback to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myFormURLEncodedRequest) { result in switch result { case .success(let response): print("Received response: \(response)") case .failure(let error): print("Request failed with error: \(error)") } }
Declaration
Swift
open func send<Request: FormURLEncodedRequest>( _ request: Request, receiveOn queue: DispatchQueue? = nil, callback: @escaping ( Result<Request.Response, Error> ) -> Void )
Parameters
request
The request to send.
queue
An optional dispatch queue to receive the callback on. Defaults to
nil
.callback
The callback to handle the response.
-
Sends an HTTP API request with a plain text body using a callback to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myPlainRequest) { result in switch result { case .success(let response): print("Received response: \(response)") case .failure(let error): print("Request failed with error: \(error)") } }
Declaration
Swift
open func send<Request: PlainRequest>( _ request: Request, receiveOn queue: DispatchQueue? = nil, callback: @escaping ( Result<Request.Response, Error> ) -> Void )
Parameters
request
The request to send.
queue
An optional dispatch queue to receive the callback on. Defaults to
nil
.callback
The callback to handle the response.
-
send(_:
Asynchronous) Sends an HTTP API request using async-await to handle the response.
Throws
An error if the request encoding or response decoding fails.Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) let response = try await client.send(myRequest) print("Received response: \(response)")
Declaration
Swift
open func send<R>(_ request: R) async throws -> R.Response where R : Request
Parameters
request
The request to send.
Return Value
The decoded response.
-
send(_:
Asynchronous) Sends an HTTP API request with a JSON body using async-await to handle the response.
Throws
An error if the request encoding or response decoding fails.Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) let response = try await client.send(myJsonRequest) print("Received response: \(response)")
Declaration
Swift
open func send<Request>(_ request: Request) async throws -> Request.Response where Request : JSONBodyProvider, Request : JSONFormatProvider, Request : Request
Parameters
request
The request to send.
Return Value
The decoded response.
-
send(_:
Asynchronous) Sends an HTTP API request with a Form URL Encoded body using async-await to handle the response.
Throws
An error if the request encoding or response decoding fails.Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) let response = try await client.send(myFormURLEncodedRequest) print("Received response: \(response)")
Declaration
Swift
open func send<Request>(_ request: Request) async throws -> Request.Response where Request : FormURLEncodedBodyProvider, Request : FormURLEncodedFormatProvider, Request : Request
Parameters
request
The request to send.
Return Value
The decoded response.
-
send(_:
Asynchronous) Sends an HTTP API request with a plain text body using async-await to handle the response.
Throws
An error if the request encoding or response decoding fails.Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) let response = try await client.send(myPlainRequest) print("Received response: \(response)")
Declaration
Swift
open func send<Request>(_ request: Request) async throws -> Request.Response where Request : PlainBodyProvider, Request : PlainFormatProvider, Request : Request
Parameters
request
The request to send.
Return Value
The decoded response.
-
Sends an HTTP API request using Combine to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myRequest) .sink(receiveCompletion: { completion in switch completion { case .finished: print("Request completed successfully") case .failure(let error): print("Request failed with error: \(error)") } }, receiveValue: { response in print("Received response: \(response)") }) .store(in: &cancellables)
Declaration
Swift
open func send<R>(_ request: R) -> AnyPublisher<R.Response, Error> where R : Request
Parameters
request
The request to send.
Return Value
A publisher that emits the decoded response or an error.
-
Sends an HTTP API request with a JSON body using Combine to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myJsonRequest) .sink(receiveCompletion: { completion in switch completion { case .finished: print("Request completed successfully") case .failure(let error): print("Request failed with error: \(error)") } }, receiveValue: { response in print("Received response: \(response)") }) .store(in: &cancellables)
Declaration
Swift
open func send<Request>(_ request: Request) -> AnyPublisher<Request.Response, Error> where Request : JSONBodyProvider, Request : JSONFormatProvider, Request : Request
Parameters
request
The request to send.
Return Value
A publisher that emits the decoded response or an error.
-
Sends an HTTP API request with a Form URL Encoded body using Combine to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myFormURLEncodedRequest) .sink(receiveCompletion: { completion in switch completion { case .finished: print("Request completed successfully") case .failure(let error): print("Request failed with error: \(error)") } }, receiveValue: { response in print("Received response: \(response)") }) .store(in: &cancellables)
Declaration
Swift
open func send<Request>(_ request: Request) -> AnyPublisher<Request.Response, Error> where Request : FormURLEncodedBodyProvider, Request : FormURLEncodedFormatProvider, Request : Request
Parameters
request
The request to send.
Return Value
A publisher that emits the decoded response or an error.
-
Sends an HTTP API request with a plain text body using Combine to handle the response.
Example usage:
let client = HTTPAPIClient(url: URL(string: "https://api.example.com")!) client.send(myPlainRequest) .sink(receiveCompletion: { completion in switch completion { case .finished: print("Request completed successfully") case .failure(let error): print("Request failed with error: \(error)") } }, receiveValue: { response in print("Received response: \(response)") }) .store(in: &cancellables)
Declaration
Swift
open func send<Request>(_ request: Request) -> AnyPublisher<Request.Response, Error> where Request : PlainBodyProvider, Request : PlainFormatProvider, Request : Request
Parameters
request
The request to send.
Return Value
A publisher that emits the decoded response or an error.