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
Authenticationenum.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
authThe Authentication to use for the API call.
subuserA 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
Datafrom the response and convert it into JSON (with something likeJSONSerializationorJSONDecoder).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 : EncodableParameters
pathThe path of the endpoint. This should not include the host. For example,
/v3/user/profile
(note the path must start with a/).methodThe HTTP method to make the API call with.
parametersOptional parameters to include with the HTTP request.
headersHeaders to add to the request.
encodingStrategyThe encoding strategy for any dates or data in the parameters.
completionHandlerA callback containing the response information.
-
Makes the HTTP request with the given
Requestobject.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 : EncodableParameters
requestThe
Requestinstance to send.completionHandlerA completion block that will be called after the API call completes.
-
Makes the HTTP request with the given
ModeledRequestobject. The success case in the response will contain the marshalled model object specified in the generic declaration of theModeledRequest.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 : EncodableParameters
requestThe
ModeledRequestinstance to send.completionHandlerA completion block that will be called after the API call completes.
View on GitHub
Session Class Reference