Today, Amazon released Smoke Framework, a lightweight server-side service framework written in the Swift programming language.
The Smoke Framework uses SwiftNIO for its networking layer by default. This framework can be used for REST-like or RPC-like services and in conjunction with code generators from service models such as Swagger/OpenAPI. The framework also has a built-in support for a JSON-encoded request and response payloads.
Working of Swift-based Smoke Framework
The Smoke Framework provides the ability to specify handlers for operations the service application needs to perform. When a request is received, the framework will decode the request into the operation’s input. When the handler returns, its response (if any) will be encoded and sent in the response.
Each invocation of a handler is also passed an application-specific context, allowing application-scope entities such as other service clients to be passed to operation handlers. Using the context allows operation handlers to remain pure functions (where its return value is determined by the function’s logic and input values) and hence easily testable.
Parts of the Smoke framework
The Operation Delegate
The Operation Delegate handles specifics such as encoding and decoding requests to the handler’s input and output.
The Smoke Framework provides the JSONPayloadHTTP1OperationDelegate implementation that expects a JSON encoded request body as the handler’s input and returns the output as the JSON encoded response body.
The Operation Function
By default, the Smoke framework provides four function signatures that this function can conform to
- ((InputType, ContextType) throws -> ()): Synchronous method with no output.
- ((InputType, ContextType) throws -> OutputType): Synchronous method with output.
- ((InputType, ContextType, ( Swift.Error?) -> ()) throws -> ()): Asynchronous method with no output.
- ((InputType, ContextType, (SmokeResult
) -> ()) throws -> ()): Asynchronous method with output.
By default, any errors thrown from an operation handler will fail the operation and the framework will return a 500 Internal Server Error to the caller (the framework also logs this event at Error level). This behavior prevents any unintentional leakage of internal error information.
The Smoke Framework has been designed to make testing of operation handlers straightforward. It is recommended that operation handlers are pure functions (where its return value is determined by the function’s logic and input values). In this case, the function can be called in unit tests with appropriately constructed input and context instances.
To know more about this in detail, visit Smoke framework’s official GitHub page.