Session

open class Session

The Session class is used to faciliate the HTTP request to the SendGrid API endpoints. When starting out, you’ll want to configure Session with your authentication information, whether that be credentials or an API Key. A class conforming to the Resource protocol is then used to provide information about the desired API call.

You can also call the sharedInstance property of Session to get a singleton instance. This will allow you to configure the singleton once, and continually reuse it later without the need to re-configure it.

  • This property holds the authentication information (credentials, or API Key) for the API requests via the Authentication enum.

    Declaration

    Swift

    open var authentication: Authentication?
  • The user agent applied to all requests sent through this Session.

    Declaration

    Swift

    open var userAgent: String
  • If you’re authenticating with a parent account, you can set this property to the username of a subuser. Doing so will authenticate with the parent account, but retrieve the information of the specified subuser, effectively impersonating them as though you had authenticated with the subuser’s credentials.

    Declaration

    Swift

    open var onBehalfOf: String?
  • A shared singleton instance of SGSession. Using the shared instance allows you to configure it once with the desired authentication method, and then continually reuse it without the need for re-configuration.

    Declaration

    Swift

    public static let shared: Session
  • Default initializer.

    Declaration

    Swift

    public init()
  • Initiates an instance of SGSession with the given authentication method.

    Declaration

    Swift

    public init(auth: Authentication, onBehalfOf subuser: String? = nil)

    Parameters

    auth

    The Authentication to use for the API call.

    subuser

    A username of a subuser to impersonate.

  • This method is the most generic method to make an API call with. It allows you to specify the individual properties of an API call and retrieve the raw response back. If you use this method, you’ll most likely need to take the Data from the response and convert it into JSON (with something like JSONSerialization or JSONDecoder).

    Throws

    If there was a problem constructing or making the API call, an error will be thrown.

    Declaration

    Swift

    open func request<T>(path: String, method: HTTPMethod, parameters: T? = nil, headers: [String : String] = [:], encodingStrategy: EncodingStrategy = EncodingStrategy(), completionHandler: ((Result<(response: HTTPURLResponse, data: Data?), Error>) -> Void)? = nil) throws where T : Encodable

    Parameters

    path

    The path of the endpoint. This should not include the host. For example, /v3/user/profile (note the path must start with a /).

    method

    The HTTP method to make the API call with.

    parameters

    Optional parameters to include with the HTTP request.

    headers

    Headers to add to the request.

    encodingStrategy

    The encoding strategy for any dates or data in the parameters.

    completionHandler

    A callback containing the response information.

  • Makes the HTTP request with the given Request object.

    Throws

    If there was a problem constructing or making the API call, an error will be thrown.

    Declaration

    Swift

    open func send<Parameters>(request: Request<Parameters>, completionHandler: ((Result<HTTPURLResponse, Error>) -> Void)? = nil) throws where Parameters : Encodable

    Parameters

    request

    The Request instance to send.

    completionHandler

    A completion block that will be called after the API call completes.

  • Makes the HTTP request with the given ModeledRequest object. The success case in the response will contain the marshalled model object specified in the generic declaration of the ModeledRequest.

    Throws

    If there was a problem constructing or making the API call, an error will be thrown.

    Declaration

    Swift

    open func send<ModelType, Parameters>(modeledRequest request: ModeledRequest<ModelType, Parameters>, completionHandler: ((Result<(HTTPURLResponse, ModelType), Error>) -> Void)? = nil) throws where ModelType : Decodable, Parameters : Encodable

    Parameters

    request

    The ModeledRequest instance to send.

    completionHandler

    A completion block that will be called after the API call completes.