-
Notifications
You must be signed in to change notification settings - Fork 0
/
Requestable.swift
48 lines (44 loc) · 1.64 KB
/
Requestable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// RequestType.swift
// Networking
//
// Created by Viktor Gidlöf.
//
import Foundation
/// A protocol for a request type. Used to build backend API requests.
public protocol Requestable {
/// The request authorization. Defaults to `none`.
var authorization: Request.Authorization { get }
/// The content type of the request. Defaults to `nil`.
var contentType: HTTP.ContentType? { get }
/// A time interval for request timeout. Defaults to 30 seconds.
var timeoutInterval: TimeInterval { get }
/// The request parameters. Defaults to an empty dictionary.
var parameters: HTTP.Parameters { get }
/// The encoding used fot the request
var encoding: Request.Encoding { get }
/// The request HTTP method
var httpMethod: HTTP.Method { get }
/// The API endpoint
var endpoint: EndpointType { get }
}
// MARK: -
public extension Requestable {
/// Configure a new `URLRequest` from a requestable object with a server configuration
/// - parameter server: The given server config to use
/// - throws: An error if the request can't be build
/// - returns: A new `URLRequest` with all the configurations
func configure(withServer server: ServerConfig) throws -> URLRequest {
let config = Request.Config(request: self, server: server)
let urlRequest = try URLRequest(withConfig: config)
urlRequest.log()
return urlRequest
}
}
// MARK: -
public extension Requestable {
var authorization: Request.Authorization { .none }
var timeoutInterval: TimeInterval { 30.0 }
var contentType: HTTP.ContentType? { nil }
var parameters: HTTP.Parameters { [:] }
}