RestRequest
public class RestRequest
Object containing everything needed to build and execute HTTP requests.
-
The currently configured
CircuitBreakerinstance for thisRestRequest. In order to create aCircuitBreakeryou should set thecircuitParametersproperty.Declaration
Swift
internal(set) public var circuitBreaker: CircuitBreaker<(HTTPClient.Request, (Result<HTTPClient.Response, RestError>) -> Void), String>? { get } -
Parameters for a
CircuitBreakerinstance. When these parameters are set, a newcircuitBreakerinstance is created.Usage Example:
let circuitParameters = CircuitParameters(timeout: 2000, maxFailures: 2, fallback: breakFallback) let request = RestRequest(method: .GET, url: "http://myApiCall/hello") request.circuitParameters = circuitParametersDeclaration
Swift
public var circuitParameters: CircuitParameters<String>? { get set }
-
The HTTP method specified in the request, defaults to GET.
Usage Example:
request.method = .PUTDeclaration
Swift
public var method: HTTPMethod { get set } -
The HTTP authentication credentials for the request.
Usage Example:
The example below uses an API key to specify the authentication credentials. You can also use
.bearerAuthenticationand pass in a base64 encoded String as the token, or.basicAuthenticationwhere the username and password values to authenticate with are passed in.let request = RestRequest(url: "http://localhost:8080") request.credentials = .basicAuthentication(username: "Hello", password: "World")Declaration
Swift
public var credentials: Credentials? { get set } -
The HTTP header fields which form the header section of the request message.
The header fields set using this parameter will be added to the existing headers.
Usage Example:
request.headerParameters = ["Cookie": "v1"]Declaration
Swift
public var headerParameters: [String : String] { get set } -
The HTTP
Acceptheader, i.e. the media type that is acceptable for the response, it defaults to “application/json”.Usage Example:
request.acceptType = "text/html"Declaration
Swift
public var acceptType: String? { get set } -
HTTP
Content-Typeheader, i.e. the media type of the body of the request, it defaults to “application/json”.Usage Example:
request.contentType = "application/x-www-form-urlencoded"Declaration
Swift
public var contentType: String? { get set } -
HTTP
User-Agentheader, i.e. the user agent string of the software that is acting on behalf of the user. If you pass in<productName>/<productVersion>the value will be set to<productName>/<productVersion> <operatingSystem>/<operatingSystemVersion>.Usage Example:
request.productInfo = "swiftyrequest-sdk/2.0.4"Declaration
Swift
public var productInfo: String? { get set } -
The HTTP message body, i.e. the body of the request.
Usage Example:
request.messageBody = data ``Declaration
Swift
public var messageBody: Data? { get set } -
The HTTP message body, i.e. the body of the request, as a JSON dictionary.
Usage Example:
let dict: [String:Any] = ["key": "value"] request.messageBodyDictionary = dict ``Declaration
Swift
public var messageBodyDictionary: [String : Any]? { get set } -
The HTTP message body, i.e. the body of the request, as a JSON array.
Usage Example:
let json: [Any] = ["value1", "value2"] request.messageBodyArray = json ``Declaration
Swift
public var messageBodyArray: [Any]? { get set } -
The HTTP query items to specify in the request URL. If there are query items already specified in the request URL they will be replaced.
Usage Example:
request.queryItems = [ URLQueryItem(name: "flamingo", value: "pink"), URLQueryItem(name: "seagull", value: "white") ]Declaration
Swift
public var queryItems: [URLQueryItem]? { get set } -
Undocumented
Declaration
Swift
public convenience init(method: HTTPMethod = .get, url: String, containsSelfSignedCert: Bool? = false, clientCertificate: ClientCertificate? = nil, timeout: HTTPClient.Configuration.Timeout? = nil, eventLoopGroup: EventLoopGroup? = nil) -
Initialize a
RestRequestinstance.Usage Example:
let request = RestRequest(method: .GET, url: "http://myApiCall/hello")Declaration
Swift
public init(method: HTTPMethod = .get, url: String, insecure: Bool = false, clientCertificate: ClientCertificate? = nil, timeout: HTTPClient.Configuration.Timeout? = nil, eventLoopGroup: EventLoopGroup? = nil)Parameters
methodThe method specified in the request, defaults to GET.
urlURL string to use for the network request.
insecurePass
Trueto accept invalid or self-signed certificates.clientCertificateAn optional
ClientCertificatefor client authentication.timeoutAn optional
HTTPClient.Configuration.Timeoutspecifying how long to wait for connection or response from a remote service before timing out. Defaults tonil, which means no timeout.eventLoopGroupAn optional
EventLoopGroupthat should be used for requests, instead of the defaultMultiThreadedEventLoopGroupshared between all RestRequest instances. -
Convenience function to encode an
Encodabletype as the request body, using a defaultJSONEncoder.Throws
If the value cannot be encoded.Declaration
Swift
public func setBodyObject<T>(_ obj: T) throws where T : EncodableParameters
objThe value to encode as JSON
-
Request response method that either invokes
CircuitBreakeror executes the HTTP request. Note: this is equivalent toresponseVoid(templateParams:queryItems:completionHandler:).Declaration
Swift
public func response(templateParams: [String: String]? = nil, queryItems: [URLQueryItem]? = nil, completionHandler: @escaping (Result<HTTPClient.Response, RestError>) -> Void)Parameters
templateParamsURL templating parameters used for substituion if possible.
queryItemsSets the query parameters for this RestRequest, overwriting any existing parameters. Defaults to
nil, which means that this parameter will be ignored, andRestRequest.queryItemswill be used instead. Note that if you wish to clear any existing query parameters, then you should setrequest.queryItems = nilbefore calling this function.completionHandlerCallback used on completion of operation.
-
Request response method with the expected result of a
Dataobject.Declaration
Swift
public func responseData(templateParams: [String: String]? = nil, queryItems: [URLQueryItem]? = nil, completionHandler: @escaping (Result<RestResponse<Data>, RestError>) -> Void)Parameters
templateParamsURL templating parameters used for substituion if possible.
queryItemsSets the query parameters for this RestRequest, overwriting any existing parameters. Defaults to
nil, which means that this parameter will be ignored, andRestRequest.queryItemswill be used instead. Note that if you wish to clear any existing query parameters, then you should setrequest.queryItems = nilbefore calling this function.completionHandlerCallback used on completion of operation.
-
Request response method with the expected result of the object
Tspecified.Declaration
Swift
public func responseObject<T: Decodable>(templateParams: [String: String]? = nil, queryItems: [URLQueryItem]? = nil, completionHandler: @escaping (Result<RestResponse<T>, RestError>) -> Void)Parameters
templateParamsURL templating parameters used for substitution if possible.
queryItemsSets the query parameters for this RestRequest, overwriting any existing parameters. Defaults to
nil, which means that this parameter will be ignored, andRestRequest.queryItemswill be used instead. Note that if you wish to clear any existing query parameters, then you should setrequest.queryItems = nilbefore calling this function.completionHandlerCallback used on completion of operation.
-
Request response method with the expected result of an array of
AnyJSON.Declaration
Swift
public func responseArray(templateParams: [String: String]? = nil, queryItems: [URLQueryItem]? = nil, completionHandler: @escaping (Result<RestResponse<[Any]>, RestError>) -> Void)Parameters
templateParamsURL templating parameters used for substitution if possible.
queryItemsSets the query parameters for this RestRequest, overwriting any existing parameters. Defaults to
nil, which means that this parameter will be ignored, andRestRequest.queryItemswill be used instead. Note that if you wish to clear any existing query parameters, then you should setrequest.queryItems = nilbefore calling this function.completionHandlerCallback used on completion of operation.
-
Request response method with the expected result of a
[String: Any]JSON dictionary.Declaration
Swift
public func responseDictionary(templateParams: [String: String]? = nil, queryItems: [URLQueryItem]? = nil, completionHandler: @escaping (Result<RestResponse<[String: Any]>, RestError>) -> Void)Parameters
templateParamsURL templating parameters used for substitution if possible.
queryItemsSets the query parameters for this RestRequest, overwriting any existing parameters. Defaults to
nil, which means that this parameter will be ignored, andRestRequest.queryItemswill be used instead. Note that if you wish to clear any existing query parameters, then you should setrequest.queryItems = nilbefore calling this function.completionHandlerCallback used on completion of operation.
-
Request response method with the expected result of a
String.Declaration
Swift
public func responseString(templateParams: [String: String]? = nil, queryItems: [URLQueryItem]? = nil, completionHandler: @escaping (Result<RestResponse<String>, RestError>) -> Void)Parameters
templateParamsURL templating parameters used for substituion if possible.
queryItemsSets the query parameters for this RestRequest, overwriting any existing parameters. Defaults to
nil, which means that this parameter will be ignored, andRestRequest.queryItemswill be used instead. Note that if you wish to clear any existing query parameters, then you should setrequest.queryItems = nilbefore calling this function.completionHandlerCallback used on completion of operation.
-
Request response method to use when there is no expected result.
Declaration
Swift
public func responseVoid(templateParams: [String: String]? = nil, queryItems: [URLQueryItem]? = nil, completionHandler: @escaping (Result<HTTPClient.Response, RestError>) -> Void)Parameters
templateParamsURL templating parameters used for substituion if possible.
queryItemsSets the query parameters for this RestRequest, overwriting any existing parameters. Defaults to
nil, which means that this parameter will be ignored, andRestRequest.queryItemswill be used instead. Note that if you wish to clear any existing query parameters, then you should setrequest.queryItems = nilbefore calling this function.completionHandlerCallback used on completion of operation.
-
Utility method to download a file from a remote origin.
Declaration
Swift
public func download(to destination: URL, completionHandler: @escaping (Result<HTTPResponseHead, RestError>) -> Void)Parameters
destinationURL destination to save the file to.
completionHandlerCallback used on completion of the operation.
View on GitHub
RestRequest Class Reference