Endpoint
public class Endpoint : EndpointInterface
A class that implements the EndpointInterface
protocol, providing methods to build and manipulate API endpoint URLs.
This class allows for the addition of versions, controllers, paths, and query parameters to create a complete URL for API requests.
It supports both string and enumerator-based inputs for flexibility and readability.
-
The URL of the request.
Declaration
Swift
public var url: String
-
Initializes the Endpoint with an optional URL. If no URL is provided, it defaults to an empty string.
Declaration
Swift
public init(url: String = "")
Parameters
url
The initial URL of the endpoint, defaulting to an empty string.
-
Adds a version to the endpoint This can be added using the
withPath(_ path: String)
method but it’s better for readability NOTE: Do not append nor prepend the foward slashDeclaration
Swift
@discardableResult public func withVersion(_ version: String) -> Endpoint
Parameters
version
The version of the endpoint as string
-
Adds a version to the endpoint This can be added using the
withPath(_ path: String)
method but it’s better for readability This method accepts a enumerator instead of a stringDeclaration
Swift
@discardableResult public func withVersion<T>(_ version: T) -> Endpoint where T : StringRepresentable
Parameters
version
The version of the endpoint as string
T
The type of the enumerator containing the version
-
Adds a controller to the endpoint This can be added using the
withPath(_ path: String)
method but it’s better for readability NOTE: Do not append nor prepend the foward slashDeclaration
Swift
@discardableResult public func withController(_ controller: String) -> Endpoint
Parameters
controller
The controller of the endpoint, usually a controller contains muitiple endpoints
-
Adds a controller to the endpoint This can be added using the
withPath(_ path: String)
method but it’s better for readabilityDeclaration
Swift
@discardableResult public func withController<T>(_ controller: T) -> Endpoint where T : StringRepresentable
Parameters
controller
The controller of the endpoint, usually a controller contains muitiple endpoints
T
The type of the enumerator containing the controller
-
Adds a path to the endpoint NOTE: Do not append nor prepend the foward slash
Declaration
Swift
@discardableResult public func withPath(_ path: String) -> Endpoint
Parameters
path
The path to the endpoint
-
Adds a path to the endpoint The path can contain values like
{id}
where{id}
will be replaced by the key in the dictionaryparameters
NOTE: Do not append nor prepend the foward slashDeclaration
Swift
@discardableResult public func withPath(_ path: String, parameters: [String : String]) -> Endpoint
Parameters
path
The path to the endpoint
parameters
The dictionary with the parameters to replace
-
Adds query parameters to the url
Declaration
Swift
@discardableResult public func withQuery(_ query: [String : String]) -> Endpoint
Parameters
query
The dictionary of query values to add
-
Builds the URL
Throws
SwiftlyRestError.invalidURL
if the provided URL is not validDeclaration
Swift
public func build() throws -> String
Return Value
The built URL