Skip to content

Endpoint

Josh Wright edited this page Jan 13, 2021 · 3 revisions

Endpoint

Endpoint is an abstraction around making REST requests. It includes a Request type, representing the data needed to make the request, and a Response type, representing the expected response from the server.

public struct Endpoint<Request: EndpointRequest, Response: Codable>

Endpoints are defined via property wrapped (@GET, @POST, etc...) properties on an EndpointGroup.

Endpoints are intended to be used on either client or server for requesting external endpoints or on server for providing and validating endpoints. There are partner libraries (PapyrusAlamofire and Alchemy) for requesting or validating endpoints on client or server platforms.

Properties

method

The method, or verb, of this endpoint.

let method: EndpointMethod

path

The path of this endpoint, relative to self.baseURL

var path: String

baseURL

The baseURL of this endpoint.

var baseURL: String = ""

Methods

request(_:with:decoder:)

Requests a Papyrus.Endpoint, returning a future with the decoded Endpoint.Response.

public func request(_ dto: Request, with client: HTTPClient = Services.client, decoder: JSONDecoder = JSONDecoder()) throws -> EventLoopFuture<(content: Response, response: HTTPClient.Response)>

Parameters

  • dto: An instance of the request DTO; Endpoint.Request.
  • client: The HTTPClient to request this with. Defaults to Client.default.
  • decoder: The decoder with which to decode response data to Endpoint.Response. Defaults to JSONDecoder().

Throws

An error if there is an issue encoding the request or decoding the response.

Returns

A future containing the decoded Endpoint.Response as well as the raw response of the HTTPClient.

parameters(dto:)

Gets any information that may be needed to request this Endpoint.

public func parameters(dto: Request) throws -> RequestComponents

Parameters

  • dto: An instance of Endpoint.Request.

Throws

any errors that may occur when parsing out data from the Endpoint.

Returns

A struct containing any information needed to request this endpoint with the provided instance of Request.

with(baseURL:)

Creates a copy of this Endpoint with the provided baseURL.

public func with(baseURL: String) -> Self

Parameters

  • baseURL: The baseURL for the Endpoint.

Returns

A copy of this Endpoint with the baseURL.

request(_:session:jsonEncoder:jsonDecoder:completion:)

Request an endpoint.

public func request(_ request: Request, session: Session = .default, jsonEncoder: JSONEncoder = JSONEncoder(), jsonDecoder: JSONDecoder = JSONDecoder(), completion: @escaping (AFDataResponse<Data?>, Result<Response, Error>) -> Void) throws

Parameters

  • request: The request data of this endpoint.
  • session: The Alamofire.Session with which to request this. Defaults to Session.default.
  • jsonEncoder: The JSONEncoder to use when encoding the Request. Defaults to JSONEncoder().
  • jsonDecoder: The JSONDecoder to use when decoding the Response. Defaults to JSONDecoder().
  • completion: A completion that will be called when the request is complete. Contains the raw AFDataResponse<Data> as well as a Result containing either the parsed Response or an Error.

Throws

any errors encountered while encoding the request parameters.

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally