Entity

public struct Entity<ContentType>
extension Entity: TypedContentAccessors

An HTTP entity. Consists of data content plus metadata about the content’s type and freshness.

Typically extracted from an HTTP message body.

  • The data itself. When constructed from an HTTP response, it begins its life as Data, but may become any type of object after running though the service’s ResponseTransformer chain.

    When using content, because you do not know what the server actually returned, write your code to handle it being of an unexpected type. Siesta provides TypedContentAccessors to help deal with this.

    Note

    Siesta’s future direction is to let users declare their expected type at the resource level by asking for a Resource<T>, and have that resource report an unexpected content type from the server as a request failure. However, limitations of Swift’s type system currently make this unworkable. Given what the core Swift team is saying, we’re cautiously optimistic that Swift 4 will be able to support this.

    Declaration

    Swift

    public var content: ContentType
  • The type of data contained in the content.

    If the content was parsed into a data structure, this property typically contains the type of the original raw data. For example, the type might be application/json even though content is a Dictionary and no longer the original JSON text data.

    This property may include MIME parameters, so beware of using exact string matches. For a plain text response, for example, you might see “text/plain”, “text/plain; charset=utf-8”, or even “text/foo+plain”.

    Declaration

    Swift

    public var contentType: String { get }
  • The charset given with the content type, if any.

    Declaration

    Swift

    public var charset: String?
  • The etag of this data. If non-nil, Siesta will send an If-None-Match header with subsequent loads.

    Declaration

    Swift

    public var etag: String? { get }
  • Returns the value of the HTTP header with the given key. The key is case insensitive.

    Entity does not support multi-valued headers (i.e. headers which occur more than once in the response).

    Declaration

    Swift

    public func header(forKey key: String) -> String?

    Parameters

    key

    The case-insensitive header name.

  • All HTTP headers sent with this entity. The keys are in lower case (and will be converted to lowercase if you mutate the dictionary).

    See

    See also: header(forKey:)

    Declaration

    Swift

    public var headers: [String : String] { get set }
  • The time at which this data was last known to be valid.

    Declaration

    Swift

    public var timestamp: TimeInterval
  • Extracts data from a network response.

    Declaration

    Swift

    public init(response: HTTPURLResponse?, content: ContentType)
  • For creating ad hoc data locally.

    Declaration

    Swift

    public init(
            content: ContentType,
            contentType: String,
            charset: String? = nil,
            headers: [String:String] = [:])
  • Full-width initializer, typically used only for reinflating cached data.

    Declaration

    Swift

    public init(
            content: ContentType,
            charset: String? = nil,
            headers: [String:String],
            timestamp: TimeInterval? = nil)
  • Updates timestamp to the current time.

    Declaration

    Swift

    public mutating func touch()
  • Returns an identical Entity with content cast to NewType if the type is convertible, nil otherwise.

    Declaration

    Swift

    public func withContentRetyped<NewType>() -> Entity<NewType>?
  • Typed content accessors such as .text and .jsonDict apply to this entity’s content.

    Declaration

    Swift

    public var entityForTypedContentAccessors: Entity<ContentType>? { get }