@@ -9,6 +9,12 @@ import Foundation
99
1010extension URLRequest : Builder { }
1111
12+ extension CharacterSet {
13+
14+ static var uriAllowed = CharacterSet . alphanumerics. union ( . init( charactersIn: " -_.!~*'() " ) )
15+
16+ }
17+
1218extension URLRequest {
1319
1420 subscript( header key: HTTPHeaderKey ) -> String ? {
@@ -28,41 +34,43 @@ extension URLRequest {
2834 var urlComponents = URLComponents ( )
2935 urlComponents. scheme = " https "
3036 urlComponents. path = path. fullPath
37+
38+ if let urlParameters = requestOptions? . urlParameters {
39+ urlComponents. queryItems = urlParameters
40+ . map { ( key, value) in
41+ guard let encodedName = key. rawValue. addingPercentEncoding ( withAllowedCharacters: . uriAllowed) else {
42+ return nil
43+ }
44+ let encodedValue = value? . addingPercentEncoding ( withAllowedCharacters: . uriAllowed)
45+ return URLQueryItem ( name: encodedName, value: encodedValue)
46+ }
47+ . compactMap { $0 }
48+ }
3149
3250 var request = URLRequest ( url: urlComponents. url!)
3351
3452 request. httpMethod = method. rawValue
3553 request. httpBody = body
36-
54+
3755 if let requestOptions = requestOptions {
38- request. setRequestOptions ( requestOptions)
56+
57+ requestOptions. headers. forEach { header in
58+ let ( value, field) = ( header. value, header. key. rawValue)
59+ request. setValue ( value, forHTTPHeaderField: field)
60+ }
61+
62+ // If body is set in query parameters, it will override the body passed as parameter to this function
63+ if let body = requestOptions. body, !body. isEmpty {
64+ let jsonEncodedBody = try ? JSONSerialization . data ( withJSONObject: body, options: [ ] )
65+ request. httpBody = jsonEncodedBody
66+ }
67+
3968 }
40-
69+
70+ request. httpBody = body
4171 self = request
4272 }
4373
44- mutating func setRequestOptions( _ requestOptions: RequestOptions ) {
45-
46- // Append headers
47- requestOptions. headers. forEach { setValue ( $0. value, forHTTPHeaderField: $0. key. rawValue) }
48-
49- // Append query items
50- if let url = url, var currentComponents = URLComponents ( string: url. absoluteString) {
51- let requestOptionsItems = requestOptions. urlParameters. map { URLQueryItem ( name: $0. key. rawValue, value: $0. value) }
52- var existingItems = currentComponents. queryItems ?? [ ]
53- existingItems. append ( contentsOf: requestOptionsItems)
54- currentComponents. queryItems = existingItems
55- self . url = currentComponents. url
56- }
57-
58- // Set body
59- if
60- let body = requestOptions. body,
61- let jsonData = try ? JSONSerialization . data ( withJSONObject: body, options: [ ] ) {
62- httpBody = jsonData
63- }
64- }
65-
6674}
6775
6876extension URLRequest {
0 commit comments