WrapFast includes a native API Client to make HTTP Requests with the Codable protocol easily and without depending on third party libraries.
To use it, you just have to call it by a Singleton: ApiClient.shared.sendRequest
sendRequest<T: Decodable>(endpoint: Endpoint, queryParams: [URLQueryItem]? = nil, body: Data? = nil, pathExtension: String? = nil, responseModel: T.Type, allowRetry: Bool = true) async -> Result<T, RequestError>
This method is designed to send an asynchronous network request to the specified endpoint and return the result. It utilizes Swift's generics and async/await
syntax to streamline API requests and their responses.
Parameters:
endpoint
: An Endpoint
indicating the specific API endpoint to which the request is sent.queryParams
: An optional array of URLQueryItem
to include in the request as query parameters.body
: Optional Data
to be sent as the request body, used for POST, PUT, or PATCH requests.pathExtension
: An optional string that can be appended to the endpoint's path, useful for endpoints that require an ID or additional specifier.responseModel
: The expected response model type that conforms to Decodable
. This type is used to decode the JSON response from the API.allowRetry
: A boolean indicating whether the request should be retried on failure. Defaults to true
.Returns:
Result<T, RequestError>
: An asynchronous result that either contains the decoded model of type T
on success or a RequestError
on failure.The endpoints are defined in the Endpoints
enum, that conforms to the Endpoint
Protocol. Define as many endpoints as you need.
Example of use:
let model: MealVisionRequestModel
let result = try await ApiClient.shared.sendRequest(
endpoint: Endpoints.vision,
body: JSONEncoder().encode(model),
responseModel: MealVisionResponse.self
)
The Endpoint
protocol is designed to provide a structured way to define and manage the various endpoints of a network service in the iOS application. It encapsulates all the necessary information required to construct a network request, including the base URL, path, HTTP method, headers, and request body.
Properties
baseURL
: A String
representing the base URL of the network service. This property is essential for constructing the full URL for a request. By default, it returns the base URL defined in Const.Api.baseURL
.path
: A String
that specifies the path component of the endpoint. This path is appended to the baseURL
to form the complete URL for the request.method
: A value of the RequestMethod
type that indicates the HTTP method (e.g., GET, POST) to be used for the request. RequestMethod
is assumed to be an RequestMethod
enum that covers various HTTP methods.header
: An optional dictionary of type [String: String]
that represents the HTTP headers to be included in the request. Being an asynchronous computed property allows for headers that may need to be dynamically generated or fetched, for example, authorization tokens that need to be retrieved from a secure storage or refreshed from a network service.body
: An optional dictionary of type [String: String]
representing the request body. This property is intended for requests that send data to the server, like POST or PUT requests, where the data is encoded as key-value pairs.Extension
The protocol includes an extension for the baseURL
property, providing a default implementation. This approach simplifies conforming types by allowing them to inherit the base URL directly from Const.Api.baseURL
, ensuring consistency across different endpoints and reducing boilerplate code.
In WrapFast, we define the following endpoints:
enum Endpoints {
case auth
case vision
case chatgpt
case dalle
case anthropicMessages
}
Switching over the enum
cases, we can define what header
, body
, method
, etc… that each endpoint request will use.
Following the provided examples, you can add and define as many endpoints you need. This way you can make requests to the APIs that your app will need in a clean way, using async/await and in native Swift — without depending on third-party libraries.
<aside>
💡 Take a look at Endpoints.swift
in the Xcode project to understand how the code is implemented
</aside>
← Previous
Next →
On this page