SmokeHTTP is a specialization of the generic HTTP client provided by async-http-client, providing the common functionality required to abstract service operations from the underlying HTTP protocol. This library is primarily used by SmokeFramework and SmokeAWS.
This library provides a number of features on top of those provided by async-http-client:
HTTPRequestInputProtocol
and HTTPResponseOutputProtocol
protocols provide a mechanism to deconstruct an input type into the different components of a HTTP request and construct an output type from the components of a HTTP response respectively.To use SmokeHTTPClient, a user can instantiate an HTTPOperationsClient
in the constructor of their specific client with instantiated delegates (HTTPClientDelegate
, HTTPClientInvocationDelegate
) that are used to define client-specific logic.
SmokeHTTP follows the same support policy as followed by SmokeAWS here.
SmokeHTTP uses the Swift Package Manager. To use the framework, add the following dependency to your Package.swift.
For swift-tools version 5.2 and greater:
dependencies: [
.package(url: "https://github.com/amzn/smoke-http.git", from: "2.0.0")
]
.target(name: ..., dependencies: [
...,
.product(name: "SmokeHTTPClient", package: "smoke-http"),
]),
For swift-tools version 5.1 and prior:
dependencies: [
.package(url: "https://github.com/amzn/smoke-http.git", from: "2.0.0")
]
.target(
name: ...,
dependencies: [..., "SmokeHTTPClient"]),
Construct a HTTPClient using the following code:
import SmokeHTTPClient
let httpOperationsClient = HTTPOperationsClient(endpointHostName: endpointHostName,
endpointPort: endpointPort,
contentType: contentType,
clientDelegate: clientDelegate,
connectionTimeoutSeconds: connectionTimeoutSeconds,
eventLoopProvider: = .createNew)
The inputs to this constructor are:
dynamodb.us-west-2.amazonaws.com
.443
.application/json
.HTTPClientDelegate
protocol.There are a number of variants of the execute call on the HTTPOperationsClient
. Below describes one variant but all are broadly similar-
try httpOperationsClient.executeAsyncRetriableWithOutput(
endpointOverride: nil,
endpointPath = endpointPath,
httpMethod: .GET,
input: InputType,
completion: completion,
asyncResponseInvocationStrategy: asyncResponseInvocationStrategy,
invocationContext: invocationContext,
retryConfiguration: retryConfiguration,
retryOnError: retryOnError)
The inputs to this function are:
HTTPRequestInputProtocol
protocol.(Result<OutputType, HTTPClientError>) -> ()
used to handle the outcome of the invocation. OutputType
must be a type that conforms to the HTTPResponseOutputProtocol
protocol.HTTPClientInvocationContext
.HTTPClientRetryConfiguration
to indicate how the client should handle automatic retries on failure.(HTTPClientError) -> Bool
that can be used to determine if an automatic retry should occur when the request failures with the provided error.The complete list of variants for the HTTPOperationsClient.execute*
functions are:
executeAsyncRetriableWithOutput
: Executes a HTTP request asynchronously with built-in support for automatic retries that produces an output.executeAsyncRetriableWithoutOutput
: Executes a HTTP request synchronously with built-in support for automatic retries that doesn't produce an output.executeAsyncWithoutOutput
: Executes a HTTP request asynchronously without built-in support for automatic retries that doesn't produce an output.executeAsyncWithOutput
: Executes a HTTP request asynchronously without built-in support for automatic retries that produces an output.executeSyncRetriableWithoutOutput
: Executes a HTTP request synchronously with built-in support for automatic retries that doesn't produce an output.executeSyncRetriableWithOutput
: Executes a HTTP request synchronously with built-in support for automatic retries that produces an output.executeSyncWithoutOutput
: Executes a HTTP request synchronously without built-in support for automatic retries that doesn't produce an output.executeSyncWithOutput
: Executes a HTTP request synchronously without built-in support for automatic retries that produces an output.The HTTPClientDelegate
protocol provides a number extension points that can be used to customise a client.
Protocol function requirements:
getResponseError
: determines the client-specific error based on the HTTP response from the client.encodeInputAndQueryString
: determines the components to be used for the HTTP request based on the input to an invocation.decodeOutput
creates an instance of an output type based on the HTTP response from the client.getTLSConfiguration
: retrieves the TLS configuration to be used by the client.The HTTPClientDelegate
protocol provides a number extension points that can be used to customise the invocation of a client.
Protocol property requirements:
specifyContentHeadersForZeroLengthBody
: If the Content-Length
and Content-Type
headers should be sent in the request even when there is no request body.Protocol function requirements:
addClientSpecificHeaders
: determines any additional headers to be added to HTTP request.handleErrorResponses
: determines the client-specific error based on the HTTP response from the client. Overrides HTTPClientDelegate.getResponseError
if a non-nil error is returned.The HTTPRequestInputProtocol
provides a mechanism used to transform an input into the different parts of a HTTP request. For an example of how this protocol is used to deconstruct an input type into a HTTP request see JSONAWSHttpClientDelegate.encodeInputAndQueryString().
Protocol property requirements:
queryEncodable
: Optionally, provides an instance of a type conforming to Encodable
that will be used to produce the query for the HTTP request.pathEncodable
: Optionally, provides an instance of a type conforming to Encodable
that will be used to provide any tokenized values for the path of the HTTP request.bodyEncodable
: Optionally, provides an instance of a type conforming to Encodable
that will be used to produce the body for the HTTP request.additionalHeadersEncodable
: Optionally, provides an instance of a type conforming to Encodable
that will be used to produce additional headers for the HTTP request.pathPostfix
: Optionally, provides a string that will be post-pended to the path template prior to having any tokens replaced by values from pathEncodable
.The HTTPResponseOutputProtocol
provides a mechanism to construct an output type from the components of a HTTP response. For an example of how this is achieved see JSONAWSHttpClientDelegate.decodeOutput().
Protocol function requirements:
compose
: A function that accepts bodyDecodableProvider
and headersDecodableProvider
closures that can be used to construct parts of the expected output type from parts of the HTTP response.The HTTPClientInvocationContext
type can be used to customise the invocation of a client.
The inputs to the HTTPClientInvocationContext
constructor are:
reporting
: An instance of a type conforming to the HTTPClientInvocationReporting
protocol.handlerDelegate
: An instance of a type conforming to the HTTPClientInvocationDelegate
protocol.The HTTPClientInvocationReporting
protocol provides a number of extension points focused on the reporting of a client invocation-
Protocol property requirements:
logger
: The logger to use for statements related to the HTTP client invocation.internalRequestId
: the internal identity of the request that is making the invocation to the client.traceContext
: An instance of a type conforming to the InvocationTraceContext
protocol.successCounter
: Optionally, a Metrics.Counter
that will record successful invocations of the client.failure5XXCounter
: Optionally, a Metrics.Counter
that will record unsuccessful invocations of the client that return with a 5xx response code.failure4XXCounter
: Optionally, a Metrics.Counter
that will record unsuccessful invocations of the client that return with a 4xx response code.retryCountRecorder
: Optionally, a Metrics.Recorder
that will record the retry count for invocations of the client.latencyTimer
: Optionally, a Metrics.Recorder
that will record the latency of invocations from the client.The InvocationTraceContext
provides an extension point for request-level tracing.
Protocol function requirements:
handleOutwardsRequestStart
: Provides the ability to handle an invocation of the client just prior to the request being sent, including the ability to modify the headers sent in the request.handleOutwardsRequestSuccess
: Provides the ability to handle a successful invocation just after the response has been received.handleOutwardsRequestFailure
: Provides the ability to handle a unsuccessful invocation just after the response has been received.This library is licensed under the Apache 2.0 License.
link |
Stars: 67 |
Last commit: 2 days ago |
Full Changelog: https://github.com/amzn/smoke-http/compare/2.22.1...2.22.2
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics