@@ -11,7 +11,7 @@ public extension Http{
1111
1212 /// Http client for creating requests to the server
1313 @available ( iOS 15 . 0 , macOS 12 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
14- actor Proxy < R: IReader , W: IWriter > : IProxy {
14+ struct Proxy < R: IReader , W: IWriter > : IProxy {
1515
1616 /// An array of name-value pairs for a request
1717 public typealias Query = Http . Query
@@ -138,27 +138,79 @@ public extension Http{
138138
139139private extension Http . Proxy {
140140
141- /// A URL load request buider method
141+ /// Url buider method
142+ /// - Parameters:
143+ /// - baseURL: Base url
144+ /// - path: Path
145+ /// - query: An array of name-value pairs
146+ /// - Returns: A value that identifies the location of a resource
147+ func buildURL( baseURL: URL , for path: String , query : Http . Query ? = nil ) throws -> URL {
148+
149+ guard let url = URL ( string: path, relativeTo: baseURL) else {
150+ throw URLError ( . badURL)
151+ }
152+
153+ guard var components = URLComponents ( url: url, resolvingAgainstBaseURL: true ) else {
154+ throw URLError ( . badURL)
155+ }
156+
157+ if let query = query, query. isEmpty == false {
158+ components. queryItems = query. map ( URLQueryItem . init) }
159+
160+ guard let url = components. url else { throw URLError ( . badURL) }
161+
162+ return url
163+ }
164+
165+ /// A URL load request builder method
142166 /// - Parameters:
143167 /// - path: Path
144168 /// - method: The HTTP request method
145169 /// - query: An array of name-value pairs
146170 /// - body: The data sent as the message body of a request, such as for an HTTP POST or PUT requests
171+ /// - headers: A dictionary containing all of the HTTP header fields for a request
147172 /// - Returns: A URL load request
148173 func buildURLRequest(
149174 for path: String ,
150175 method : Http . Method = . get,
151176 query : Query ? = nil ,
152177 body : Encodable ? = nil ,
153- headers : Headers ? = nil
178+ headers : Headers ?
179+
154180 ) throws -> URLRequest {
155- try Http . buildURLRequest (
156- config: config,
157- for: path,
158- method: method,
159- query: query,
160- body: body,
161- headers: headers
162- )
181+ //url + query
182+ let url = try buildURL ( baseURL: config. baseURL, for: path, query: query)
183+
184+ var request = URLRequest ( url: url)
185+
186+ //headers
187+ if let headers{
188+ request. allHTTPHeaderFields = headers
189+ }
190+
191+ // method
192+ request. httpMethod = method. rawValue
193+
194+ //body
195+ if let body = body{
196+ request. httpBody = try config. writer. write ( body)
197+
198+ if hasNotContentType ( config. getSession, request) {
199+ let content = config. defaultContentType
200+ request. setValue ( content, forHTTPHeaderField: " Content-Type " )
201+ }
202+ }
203+
204+ return request
205+ }
206+
207+ /// Check presents of the content type header
208+ /// - Parameters:
209+ /// - session: URLSession
210+ /// - request: URLRequest
211+ /// - Returns: true - content-type header is not empty
212+ func hasNotContentType( _ session : URLSession , _ request : URLRequest ) -> Bool {
213+ request. value ( forHTTPHeaderField: " Content-Type " ) == nil &&
214+ session. configuration. httpAdditionalHeaders ? [ " Content-Type " ] == nil
163215 }
164216}
0 commit comments