Pipeline

public struct 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.

  • The order in which the pipeline’s stages run. The default order is:

    1. PipelineStageKey.rawData
    2. decoding
    3. parsing
    4. model
    5. cleanup

    Stage keys do not have semantic significance — they are just arbitrary identifiers — so you are free arbitrarily add or reorder stages if you have complex response processing needs.

    Note

    Stages must be unique. Adding a duplicate stage key will cause a precondition failure.

    Note

    Any stage not present in the order will not run, even if it has transformers and/or cache configured.

    Declaration

    Swift

    public var order: [PipelineStageKey] { get set }
  • Retrieves the stage for the given key, or an empty one if none yet exists.

    Declaration

    Swift

    public subscript(key: PipelineStageKey) -> PipelineStage { get set }
  • Removes all transformers from all stages in the pipeline. Leaves caches intact.

    Declaration

    Swift

    public mutating func removeAllTransformers()
  • Removes all caches from all stages in the pipeline. Leaves transformers intact.

    You can use this to prevent sensitive resources from being cached:

    service.configure("/secret") {
      $0.pipeline.removeAllCaches()
    }
    

    Declaration

    Swift

    public mutating func removeAllCaches()
  • Removes all transformers and caches from all stages in the pipeline.

    Declaration

    Swift

    public mutating func clear()
  • Adds one of Siesta’s standard tranformers to a pipeline. Useful if you omitted one of the standard transformers in Service.init(...), but still want to configure it for certain resources.

    Declaration

    Swift

    public mutating func add(_ transformer: StandardTransformer)