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
Decodableprotocol, 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 -
dateDecodingStrategyDefault implementationDefines the
JSONDecoder.DateDecodingStrategyto use when decoding from JSON into the conforming instance. Defaults to.deferredToDateDefault Implementation
Defines the
JSONDecoder.DateDecodingStrategyto use when decoding from JSON into the conforming type. Defaults to.deferredToDateDeclaration
Swift
var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy -
dateEncodingStrategyDefault implementationDefines the
JSONEncoder.DateEncodingStrategyto use when JSON encoding a conforming instance. Defaults to.deferredToDateDefault Implementation
Defines the
JSONEncoder.DateEncodingStrategyto use when JSON encoding a conforming instance. Defaults to.deferredToDateDeclaration
Swift
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy -
baseURLDefault 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 -
pathDefault implementationThe Path to the endpoint. Example:
/some/path
Default Implementation
The Path to the endpoint. Example:
/some/path
Declaration
Swift
var path: String -
urlDefault implementationAn optional URL to use for the request instead of
baseURLandpath. When presetbaseURLandpathwill be ignored. Defaults to nil.Default Implementation
An optional URL to use for the request instead of
baseURLandpath. When presetbaseURLandpathwill be ignored. Defaults to nil.Declaration
Swift
var url: URL? -
cachePolicyDefault 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 -
timeoutIntervalDefault 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 -> URLRequestReturn Value
A URLRequest object, if one was successfully created
-
resultFormatDefault implementationDefines the expected format of the response
Default Implementation
Defines the expected format of the response Defaults to
.jsonDeclaration
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 -> URLSessionDataTaskParameters
callbackOnMainA flag indicating if the
completionHandlershould be dispatched to the main queue.sessionThe URLSession from which the URLSessionDataTask will be created.
completionThe handler to be called upon completion or failure.
Return Value
The URLSessionDataTask
View on GitHub
Restable Protocol Reference