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
initDeclaration
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
baseURLThe base URL of the server
loggingEnabledWhen true it logs all requests in a detailed manner (including successfull requests)
tagTag to idenfiy the logs
apiAuthConfigurationThe API Configuration system
jwtTokenThe JWT token for the authentication
contentTypeHeaderThe content type header (recommended application/json)
-
Sets the content type header
Declaration
Swift
public func setContentType(_ contentType: String)Parameters
contentTypeThe content type header value, defaults to “application/json”
-
Configures the base URL to be used
Throws
SwiftlyRestError.invalidURLif the provided URL is not validDeclaration
Swift
public func setBaseURL(_ url: String) throwsParameters
urlThe base URL to use
-
Configures the API Auth with the
ApiAuthenticationInterfaceprotocol When set the headers will be automatically added to the requestDeclaration
Swift
public func configureApiAuth(_ apiAuthConfiguration: ApiAuthenticationInterface)Parameters
apiAuthConfigurationThe API configuration
-
Sets the JWT token for the requests
Declaration
Swift
public func setJwtToken(_ jwtToken: String?)Parameters
jwtTokenThe JWT to use in the requests, when not
nilthey will be automatically added to the request -
Enables/Disabled the request logging
Declaration
Swift
public func loggingEnabled(_ enabled: Bool)Parameters
enabledThe 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
keyThe key where the token is stored
-
get(_:Asynchronousheaders: ) 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
awaitableResult<T, SwiftlyRestError>Where the .success contains the parsed response or aSwiftlyRestError -
post(_:Asynchronousbody: headers: ) 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
awaitableResult<T, SwiftlyRestError>Where the .success contains the parsed response or aSwiftlyRestError -
patch(_:Asynchronousbody: headers: ) 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
awaitableResult<T, SwiftlyRestError>Where the .success contains the parsed response or aSwiftlyRestError -
put(_:Asynchronousbody: headers: ) 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
awaitableResult<T, SwiftlyRestError>Where the .success contains the parsed response or aSwiftlyRestError -
delete(_:Asynchronousheaders: ) 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
awaitableResult<T, SwiftlyRestError>Where the .success contains the parsed response or aSwiftlyRestError -
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