Restable
public protocol Restable
The base protocol for method-specific protocols.
-
The expected, deserialized type for this request.
For example, if we are creating a request which gets a User with a specific ID, we might have have something like the following:
struct User: Decodable { enum Sex: String, Codable { case male, female } var name: String var age: Int var sex: Sex } struct GetUserRequest { var userId: Int } extension GetUserRequest: Gettable { typealias ResponseType = User var url: URL? { return URL(string: "https://myfancyapi.com/user/\(userId)") } }
In our request we are expecting some User JSON to come down. We create a struct which maps the structure of that JSON, have it conform to Swift’s
Decodable
protocol, and, using the ResponseType, tell ourGetUserRequest
“Hey, when you receive the JSON in the response, deserialize it to whatever ResponeType is (i.e. a User instance)Declaration
Swift
associatedtype ResponseType: Decodable
-
dateDecodingStrategy
Default implementationDefines the
JSONDecoder.DateDecodingStrategy
to use when decoding from JSON into the conforming instance. Defaults to.deferredToDate
Default Implementation
Defines the
JSONDecoder.DateDecodingStrategy
to use when decoding from JSON into the conforming type. Defaults to.deferredToDate
Declaration
Swift
var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy
-
dateEncodingStrategy
Default implementationDefines the
JSONEncoder.DateEncodingStrategy
to use when JSON encoding a conforming instance. Defaults to.deferredToDate
Default Implementation
Defines the
JSONEncoder.DateEncodingStrategy
to use when JSON encoding a conforming instance. Defaults to.deferredToDate
Declaration
Swift
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy
-
baseURL
Default implementationThe base url against which the request will be made. Example:
"https://www.google.com"
Default Implementation
The base url against which the request will be made. Example:
"https://www.google.com"
Declaration
Swift
var baseURL: String
-
path
Default implementationThe Path to the endpoint. Example:
/some/path
Default Implementation
The Path to the endpoint. Example:
/some/path
Declaration
Swift
var path: String
-
url
Default implementationAn optional URL to use for the request instead of
baseURL
andpath
. When presetbaseURL
andpath
will be ignored. Defaults to nil.Default Implementation
An optional URL to use for the request instead of
baseURL
andpath
. When presetbaseURL
andpath
will be ignored. Defaults to nil.Declaration
Swift
var url: URL?
-
cachePolicy
Default implementationThe cache policy to use for this
Restable
. Defaults to .useProtocolCachePolicyDefault Implementation
The cache policy to use for this
Restable
. Defaults to .useProtocolCachePolicyDeclaration
Swift
var cachePolicy: URLRequest.CachePolicy
-
timeoutInterval
Default implementationThe timeout interval for the request. Defaults to 60.0
Default Implementation
The timeout interval for the request. Defaults to 60.0
Declaration
Swift
var timeoutInterval: TimeInterval
-
Creates a URLRequest object.
Declaration
Swift
func request() throws -> URLRequest
Return Value
A URLRequest object, if one was successfully created
-
resultFormat
Default implementationDefines the expected format of the response
Default Implementation
Defines the expected format of the response Defaults to
.json
Declaration
Swift
var resultFormat: ResultFormat
-
submit(callbackOnMain:session:completion:)
Default implementationSubmits this request
Throws
If a URLSessionDataTask failed to createDefault Implementation
Submits this request
Throws
If a URLSessionDataTask failed to createDeclaration
Swift
@discardableResult func submit(callbackOnMain: Bool, session: URLSession, completion: RestableCompletionHandler<ResponseType>?) throws -> URLSessionDataTask
Parameters
callbackOnMain
A flag indicating if the
completionHandler
should be dispatched to the main queue.session
The URLSession from which the URLSessionDataTask will be created.
completion
The handler to be called upon completion or failure.
Return Value
The URLSessionDataTask