-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathURLRequest+Init.swift
130 lines (109 loc) · 5.07 KB
/
URLRequest+Init.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
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
//
// Copyright (C) 2017 DB Systel GmbH.
// DB Systel GmbH; Jürgen-Ponto-Platz 1; D-60329 Frankfurt am Main; Germany; http://www.dbsystel.de/
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
import Foundation
extension URLRequest {
/// Convience initializer for easy request creation.
///
/// - Parameters:
/// - path: path to the resource.
/// - baseURL: the base url of the resource.
/// - HTTPMethod: the HTTP method for the request. Defaults to `.GET`
/// - parameters: url parameters for the request. Defaults to `nil`
/// - body: body data payload. Defaults to `nil`
/// - allHTTPHeaderFields: HTTP request header fields. Defaults to `nil`
///
/// - Important: path must not start with a `/`
public init(path: String, baseURL: URL,
HTTPMethod: HTTPMethod = .GET, parameters: [String: String]? = nil,
body: Data? = nil, allHTTPHeaderFields: Dictionary<String, String>? = nil) {
guard let url = URL(string: path, relativeTo: baseURL) else {
fatalError("Error creating absolute URL from path: \(path), with baseURL: \(baseURL)")
}
let urlWithParameters: URL
if let parameters = parameters, !parameters.isEmpty {
urlWithParameters = url.appending(queryParameters: parameters)
} else {
urlWithParameters = url
}
self.init(url: urlWithParameters)
self.httpBody = body
self.httpMethod = HTTPMethod.rawValue
self.allHTTPHeaderFields = allHTTPHeaderFields
}
}
extension Array where Element == URLQueryItem {
func appending(queryItems: [URLQueryItem], overrideExisting: Bool = true) -> [URLQueryItem] {
var items = overrideExisting ? [URLQueryItem]() : self
var itemsToAppend = queryItems
if overrideExisting {
for item in self {
var itemFound = false
for (index, value) in itemsToAppend.enumerated() where value.name == item.name {
itemFound = true
items.append(value)
itemsToAppend.remove(at: index)
break
}
if itemFound == false {
items.append(item)
}
}
}
items.append(contentsOf: itemsToAppend)
return items
}
}
extension Dictionary where Key == String, Value == String {
func asURLQueryItems() -> [URLQueryItem] {
return map { URLQueryItem(name: $0.0, value: $0.1) }
}
}
extension URL {
func modifyingComponents(using block:(inout URLComponents) -> Void) -> URL {
guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) else {
fatalError("Could not create url components from \(self.absoluteString)")
}
block(&urlComponents)
guard let absoluteURL = urlComponents.url else {
fatalError("Error creating absolute URL from path: \(path), with baseURL: \(baseURL?.absoluteString ?? "No BaseURL found")")
}
return absoluteURL
}
func appending(queryItems: [URLQueryItem], overrideExisting: Bool = true) -> URL {
return modifyingComponents { urlComponents in
let items = urlComponents.queryItems ?? [URLQueryItem]()
urlComponents.queryItems = items.appending(queryItems: queryItems, overrideExisting: overrideExisting)
}
}
func appending(queryParameters: [String: String], overrideExisting: Bool = true) -> URL {
return appending(queryItems: queryParameters.asURLQueryItems(), overrideExisting: overrideExisting)
}
func replacingAllQueryItems(with queryItems: [URLQueryItem]) -> URL {
return modifyingComponents { urlComonents in
urlComonents.queryItems = queryItems
}
}
func replacingAllQueryParameters(with queryParameters: [String: String]) -> URL {
return replacingAllQueryItems(with: queryParameters.asURLQueryItems())
}
}