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 more ResponseTransformers, followed by an optional EntityCache.

    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 as clear() and PipelineStage.removeAllTransformers(), or you can disable these default parsers entirely by passing standardTransformers: [] when creating a Service.

    Services do not have any persistent caching by default.

    See more

    Declaration

    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

    Pipeline

    See more

    Declaration

    Swift

    public struct PipelineStage
  • An unique identifier for a PipelineStage within a Pipeline. 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 than Data).

    Because this is not an enum, you can add custom stages:

    extension PipelineStageKey {
      static let
        munging   = PipelineStageKey(description: "munging"),
        twiddling = PipelineStageKey(description: "twiddling")
    }
    
    ...
    
    service.configure {
        $0.pipeline.order = [.rawData, .munging, .twiddling, .cleanup]
    }
    
    See more

    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.
    See more

    Declaration

    Swift

    public protocol ResponseTransformer : CustomDebugStringConvertible

Wrapper types

  • 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 InputContentType, the response is an error.

    See more

    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 also

    Service.init(...)’s standardTransformers: parameter
    See more

    Declaration

    Swift

    public struct StandardTransformer

Transformers for standard types