Response Pipeline
-
A sequence of transformation and cache operations that Siesta applies to server responses. A raw response comes in the pipeline, and the appropriate data structure for your app comes out the other end. Apps may optionally cache responses in one or more of their intermediate forms along the way.
A pipeline has sequence of stages, each of which is uniquely identified by a
PipelineStageKey
. Apps can create custom stages, and customize their order. Each stage has zero or moreResponseTransformer
s, followed by an optionalEntityCache
.The pipeline is a part of a
Configuration
, so you can thus customize the pipeline per API or per resource.service.configure { $0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"]) $0.pipeline[.cleanup].add(GithubErrorMessageExtractor()) $0.pipeline[.model].cacheUsing(myRealmCache) } service.configureTransformer("/item/*") { // Replaces .model stage by default Item(json: $0.content) }
By default, Siesta pipelines start with parsers for common data types (JSON, text, image) configured at the
PipelineStageKey.parsing
stage. You can remove these default transformers for individual configurations using calls such asclear()
andPipelineStage.removeAllTransformers()
, or you can disable these default parsers entirely by passingstandardTransformers: []
when creating aService
.Services do not have any persistent caching by default.
See moreDeclaration
Swift
public struct Pipeline
-
A logical grouping of transformers and a cache.
Why create separate stages?
- To be able to append, replace, or remove some transformations independently of others, e.g. override the model transform without disabling JSON parsing.
- To cache entities at any intermediate stage of processing.
To maintain multiple caches.
See
Declaration
Swift
public struct PipelineStage
-
An unique identifier for a
PipelineStage
within aPipeline
. Transformers and stages have no intrinsic notion of identity or equality, so these keys are the only way to alter transformers after they’re configured.Stage keys are arbitrary, and have no intrinsic meaning. The descriptions of the default stages are for human comprehensibility, and Siesta does not enforce them in any way (e.g. it does not prevent you from configuring the
rawData
stage to output something other thanData
).Because this is not an enum, you can add custom stages:
See moreextension PipelineStageKey { static let munging = PipelineStageKey(description: "munging"), twiddling = PipelineStageKey(description: "twiddling") } ... service.configure { $0.pipeline.order = [.rawData, .munging, .twiddling, .cleanup] }
Declaration
Swift
public final class PipelineStageKey : _OpenEnum, CustomStringConvertible
-
Transforms a response from a less parsed form (e.g.
Data
) to a more parsed data structure. Responses pass through a chain of transformers before being sent to response hooks or observers.Warning
Transformers run in a GCD background queue, and must be thread-safe. You’re in the clear if your transformer touches only its input parameters, and those parameters are value types or otherwise exclusively owned.Declaration
Swift
public protocol ResponseTransformer : CustomDebugStringConvertible
-
A simplified
ResponseTransformer
that deals only with the content of the response entity, and does not touch the surrounding metadata.If
processEntity(_:)
throws or returns nil, the output is an error.If the input entity’s content does not match the
See moreInputContentType
, the response is an error.Declaration
Swift
public struct ResponseContentTransformer<InputContentType, OutputContentType> : ResponseTransformer
-
A preconfigured combination of a transformer, content type, and pipeline stage. Use this to individually opt in to Siesta’s built-in response transformers.
See moreDeclaration
Swift
public struct StandardTransformer
-
Parses
Data
content as JSON using JSONSerialization, outputting either a dictionary or an array.Declaration
Swift
public func JSONResponseTransformer(_ transformErrors: Bool = true) -> ResponseTransformer
-
Parses
Data
content as text, using the encoding specified in the content type, or ISO-8859-1 by default.Declaration
Swift
public func TextResponseTransformer(_ transformErrors: Bool = true) -> ResponseTransformer
-
Parses
Data
content as an image, yielding aUIImage
.Declaration
Swift
public func ImageResponseTransformer(_ transformErrors: Bool = false) -> ResponseTransformer