Repository navigation
Expand file tree
/
Copy pathNetRequest.swift
More file actions
180 lines (124 loc) · 6.2 KB
/
Copy pathNetRequest.swift
File metadata and controls
180 lines (124 loc) · 6.2 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// NetRequest.swift
// Net
//
// Created by Alex Rupérez on 23/3/17.
//
//
import Foundation
public struct NetRequest {
public typealias NetContentLength = UInt64
public enum NetCachePolicy: UInt {
case useProtocolCachePolicy = 0, reloadIgnoringLocalCacheData = 1, returnCacheDataElseLoad = 2, returnCacheDataDontLoad = 3
}
public enum NetServiceType: UInt {
case `default`, voip, video, background, voice, callSignaling = 11
}
public enum NetMethod: String {
case GET, POST, PUT, DELETE, PATCH, UPDATE, HEAD, TRACE, OPTIONS, CONNECT, SEARCH, COPY, MERGE, LABEL, LOCK, UNLOCK, MOVE, MKCOL, PROPFIND, PROPPATCH
}
public enum NetContentEncoding: String {
case gzip, compress, deflate, identity, br
}
public let url: URL
public let cache: NetCachePolicy
public let timeout: TimeInterval
public let mainDocumentURL: URL?
public let serviceType: NetServiceType
public let contentType: NetContentType?
public let contentLength: NetContentLength?
public let accept: NetContentType?
public let acceptEncoding: [NetContentEncoding]?
public let cacheControl: [NetCacheControl]?
public let allowsCellularAccess: Bool
public let httpMethod: NetMethod
public let headers: [String : String]?
public let body: Data?
public let bodyStream: InputStream?
public let handleCookies: Bool
public let usePipelining: Bool
public let authorization: NetAuthorization
}
extension NetRequest {
public init(_ url: URL, cache: NetCachePolicy = .useProtocolCachePolicy, timeout: TimeInterval = 60, mainDocumentURL: URL? = nil, serviceType: NetServiceType = .default, contentType: NetContentType? = nil, contentLength: NetContentLength? = nil, accept: NetContentType? = nil, acceptEncoding: [NetContentEncoding]? = nil, cacheControl: [NetCacheControl]? = nil, allowsCellularAccess: Bool = true, method: NetMethod = .GET, headers: [String : String]? = nil, body: Data? = nil, bodyStream: InputStream? = nil, handleCookies: Bool = true, usePipelining: Bool = true, authorization: NetAuthorization = .none) {
self.url = url
self.cache = cache
self.timeout = timeout
self.mainDocumentURL = mainDocumentURL
self.serviceType = serviceType
self.contentType = contentType
self.contentLength = contentLength
self.accept = accept
self.acceptEncoding = acceptEncoding
self.cacheControl = cacheControl
self.allowsCellularAccess = allowsCellularAccess
self.httpMethod = method
self.headers = headers
self.body = body
self.bodyStream = bodyStream
self.handleCookies = handleCookies
self.usePipelining = usePipelining
self.authorization = authorization
}
}
extension NetRequest {
public init?(_ urlString: String, cache: NetCachePolicy = .useProtocolCachePolicy, timeout: TimeInterval = 60, mainDocumentURL: URL? = nil, serviceType: NetServiceType = .default, contentType: NetContentType? = nil, contentLength: NetContentLength? = nil, accept: NetContentType? = nil, acceptEncoding: [NetContentEncoding]? = nil, cacheControl: [NetCacheControl]? = nil, allowsCellularAccess: Bool = true, method: NetMethod = .GET, headers: [String : String]? = nil, body: Data? = nil, bodyStream: InputStream? = nil, handleCookies: Bool = true, usePipelining: Bool = true, authorization: NetAuthorization = .none) {
guard let url = URL(string: urlString) else {
return nil
}
self.init(url, cache: cache, timeout: timeout, mainDocumentURL: mainDocumentURL, serviceType: serviceType, contentType: contentType, contentLength: contentLength, accept: accept, acceptEncoding: acceptEncoding, cacheControl: cacheControl, allowsCellularAccess: allowsCellularAccess, method: method, headers: headers, body: body, bodyStream: bodyStream, handleCookies: handleCookies, usePipelining: usePipelining, authorization: authorization)
}
}
extension NetRequest: CustomStringConvertible {
public var description: String {
return httpMethod.rawValue + " " + url.absoluteString
}
}
extension NetRequest: CustomDebugStringConvertible {
public var debugDescription: String {
var components = ["$ curl -i"]
if httpMethod != .GET {
components.append("-X \(httpMethod.rawValue)")
}
if let headers = headers {
for (field, value) in headers where field != "Content-Type" && field != "Accept" && field != "Accept-Encoding" && field != "Cache-Control" && field != "Content-Length" {
components.append("-H \"\(field): \(value)\"")
}
}
if let contentType = contentType {
components.append("-H \"Content-Type: \(contentType.rawValue)\"")
}
if let contentLength = contentLength {
components.append("-H \"Content-Length: \(contentLength)\"")
}
if let accept = accept {
components.append("-H \"Accept: \(accept.rawValue)\"")
}
if let acceptEncoding = acceptEncoding {
components.append("-H \"Accept-Encoding: \(acceptEncoding.flatMap({$0.rawValue}).joined(separator: ", "))\"")
}
if let cacheControl = cacheControl {
components.append("-H \"Cache-Control: \(cacheControl.flatMap({$0.rawValue}).joined(separator: ", "))\"")
}
if authorization != .none {
components.append("-H \"Authorization: \(authorization.rawValue)\"")
}
if let body = body, let bodyString = String(data: body, encoding: .utf8) {
var escapedBody = bodyString.replacingOccurrences(of: "\\\"", with: "\\\\\"")
escapedBody = escapedBody.replacingOccurrences(of: "\"", with: "\\\"")
components.append("-d \"\(escapedBody)\"")
}
components.append("\"\(url.absoluteString)\"")
return components.joined(separator: " \\\n\t")
}
}
extension NetRequest: Hashable {
public var hashValue: Int {
return url.hashValue + httpMethod.hashValue
}
}
extension NetRequest: Equatable {
public static func ==(lhs: NetRequest, rhs: NetRequest) -> Bool {
return lhs.url == rhs.url && lhs.httpMethod == rhs.httpMethod
}
}