SwiftlyRest

@available(macOS 12.0, *)
public class SwiftlyRest : SwiftlyRestInterface

Main class for interacting with REST APIs using SwiftlyRest.

Provides methods for configuring authentication, base URL, content type, and for performing HTTP requests (GET, POST, PUT, PATCH, DELETE). Handles JWT storage, request logging, and header generation. Use the shared instance for most operations.

  • Singleton for SwiftlyRest if needed you can use the init

    Declaration

    Swift

    @MainActor
    public static let shared: SwiftlyRest
  • Initializer to use SwiftlyRest as a single instance instead of a singleton

    Declaration

    Swift

    public init(
        baseURL: URL? = nil,
        loggingEnabled: Bool = false,
        tag: String = "[SwiftlyRest]",
        apiAuthConfiguration: ApiAuthenticationInterface? = nil,
        jwtToken: String? = nil,
        contentTypeHeader: String = "application/json"
    )

    Parameters

    baseURL

    The base URL of the server

    loggingEnabled

    When true it logs all requests in a detailed manner (including successfull requests)

    tag

    Tag to idenfiy the logs

    apiAuthConfiguration

    The API Configuration system

    jwtToken

    The JWT token for the authentication

    contentTypeHeader

    The content type header (recommended application/json)

  • Sets the content type header

    Declaration

    Swift

    public func setContentType(_ contentType: String)

    Parameters

    contentType

    The content type header value, defaults to “application/json”

  • Configures the base URL to be used

    Throws

    SwiftlyRestError.invalidURL if the provided URL is not valid

    Declaration

    Swift

    public func setBaseURL(_ url: String) throws

    Parameters

    url

    The base URL to use

  • Configures the API Auth with the ApiAuthenticationInterface protocol When set the headers will be automatically added to the request

    Declaration

    Swift

    public func configureApiAuth(_ apiAuthConfiguration: ApiAuthenticationInterface)

    Parameters

    apiAuthConfiguration

    The API configuration

  • Sets the JWT token for the requests

    Declaration

    Swift

    public func setJwtToken(_ jwtToken: String?)

    Parameters

    jwtToken

    The JWT to use in the requests, when not nil they will be automatically added to the request

  • Enables/Disabled the request logging

    Declaration

    Swift

    public func loggingEnabled(_ enabled: Bool)

    Parameters

    enabled

    The state of the logging

  • Checks if the user is authenticated

    Declaration

    Swift

    public func hasJwtSet() -> Bool
  • Stored the active jwt token on the platform if a jwt is passed that token will be stored instead The jwt will be stored on the key token by default

    Declaration

    Swift

    public func storeJwtOnKeychain(jwt token: String? = nil, on key: String = "token")
  • Loads the JWT stored in the keychain onto the JWT variable to use in the requests

    Declaration

    Swift

    public func loadJwtFromKeychain(from key: String = "token")

    Parameters

    key

    The key where the token is stored

  • get(_:headers:) Asynchronous

    Performs a HTTP GET request

    Declaration

    Swift

    @available(iOS 15.0, *)
    public func get<T: Codable>(
        _ endpoint: EndpointInterface,
        headers: [String: String] = [:]
    ) async -> Result<T, SwiftlyRestError>

    Return Value

    awaitable Result<T, SwiftlyRestError> Where the .success contains the parsed response or a SwiftlyRestError

  • post(_:body:headers:) Asynchronous

    Performs a HTTP POST request

    Declaration

    Swift

    @available(iOS 15.0, *)
    public func post<T: Codable, U: Codable>(
        _ endpoint: EndpointInterface,
        body: U? = nil,
        headers: [String: String] = [:]
    ) async -> Result<T, SwiftlyRestError>

    Return Value

    awaitable Result<T, SwiftlyRestError> Where the .success contains the parsed response or a SwiftlyRestError

  • patch(_:body:headers:) Asynchronous

    Performs a HTTP PATCH request

    Declaration

    Swift

    @available(iOS 15.0, *)
    public func patch<T: Codable, U: Codable>(
        _ endpoint: EndpointInterface,
        body: U? = nil,
        headers: [String: String] = [:]
    ) async -> Result<T, SwiftlyRestError>

    Return Value

    awaitable Result<T, SwiftlyRestError> Where the .success contains the parsed response or a SwiftlyRestError

  • put(_:body:headers:) Asynchronous

    Performs a HTTP PUT request

    Declaration

    Swift

    @available(iOS 15.0, *)
    public func put<T: Codable, U: Codable>(
        _ endpoint: EndpointInterface,
        body: U? = nil,
        headers: [String: String] = [:]
    ) async -> Result<T, SwiftlyRestError>

    Return Value

    awaitable Result<T, SwiftlyRestError> Where the .success contains the parsed response or a SwiftlyRestError

  • delete(_:headers:) Asynchronous

    Performs a HTTP DELETE request

    Declaration

    Swift

    @available(iOS 15.0, *)
    public func delete<T: Codable>(
        _ endpoint: EndpointInterface,
        headers: [String: String] = [:]
    ) async -> Result<T, SwiftlyRestError>

    Return Value

    awaitable Result<T, SwiftlyRestError> Where the .success contains the parsed response or a SwiftlyRestError

  • Generates the headers for a request

    Declaration

    Swift

    public func generateHeaders(for method: HTTPMethod, with body: Data?) -> [String : String]

    Return Value

    The headers for a request