Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Smoke: Amazon’s new lightweight server-side service framework

Save for later
  • 3 min read
  • 05 Oct 2018

article-image

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.
  • Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at €18.99/month. Cancel anytime
  • ((InputType, ContextType, (Swift.Error?) -> ()) throws -> ()): Asynchronous method with no output.
  • ((InputType, ContextType, (SmokeResult<OutputType>) -> ()) throws -> ()): Asynchronous method with output.

Error handling


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.

Testing


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.

ABI stability may finally come in Swift 5.0

Swift 4.2 releases with language, library and package manager updates!

What’s new in Vapor 3, the popular Swift based web framework