Skip to content

Commit 1b21a7f

Browse files
committed
update
1 parent 57df294 commit 1b21a7f

File tree

4 files changed

+67
-107
lines changed

4 files changed

+67
-107
lines changed

Sources/async-http-client/protocol/code/IBody.swift

Lines changed: 0 additions & 14 deletions
This file was deleted.

Sources/async-http-client/proxy/http/Http.swift

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,86 +15,7 @@ public struct Http{
1515
public typealias Query = [(String, String?)]
1616

1717
/// A dictionary containing all of the HTTP header fields for a request
18-
public typealias Headers = [String: String]
18+
public typealias Headers = [String: String]
1919

20-
// MARK: - Static
21-
22-
/// Url buider method
23-
/// - Parameters:
24-
/// - baseURL: Base url
25-
/// - path: Path
26-
/// - query: An array of name-value pairs
27-
/// - Returns: A value that identifies the location of a resource
28-
static func buildURL(baseURL: URL, for path: String, query : Http.Query? = nil) throws -> URL{
29-
30-
guard let url = URL(string: path, relativeTo: baseURL)else{
31-
throw URLError(.badURL)
32-
}
33-
34-
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: true)else{
35-
throw URLError(.badURL)
36-
}
37-
38-
if let query = query, query.isEmpty == false {
39-
components.queryItems = query.map(URLQueryItem.init) }
40-
41-
guard let url = components.url else { throw URLError(.badURL) }
42-
43-
return url
44-
}
45-
46-
47-
/// A URL load request builder method
48-
/// - Parameters:
49-
/// - config: Configuration
50-
/// - path: Path
51-
/// - method: The HTTP request method
52-
/// - query: An array of name-value pairs
53-
/// - body: The data sent as the message body of a request, such as for an HTTP POST or PUT requests
54-
/// - headers: A dictionary containing all of the HTTP header fields for a request
55-
/// - Returns: A URL load request
56-
static func buildURLRequest<R,W>(
57-
config : Configuration<R,W>,
58-
for path: String,
59-
method : Http.Method = .get,
60-
query : Query? = nil,
61-
body : Encodable? = nil,
62-
headers : Headers?
63-
64-
) throws -> URLRequest {
65-
//url + query
66-
let url = try Http.buildURL(baseURL: config.baseURL, for: path, query: query)
67-
68-
var request = URLRequest(url: url)
69-
70-
//headers
71-
if let headers{
72-
request.allHTTPHeaderFields = headers
73-
}
74-
75-
// method
76-
request.httpMethod = method.rawValue
77-
78-
//body
79-
if let body = body{
80-
request.httpBody = try config.writer.write(body)
81-
82-
if hasNotContentType(config.getSession, request){
83-
let content = config.defaultContentType
84-
request.setValue(content, forHTTPHeaderField: "Content-Type")
85-
}
86-
}
8720

88-
return request
89-
}
90-
91-
/// Check presents of the content type header
92-
/// - Parameters:
93-
/// - session: URLSession
94-
/// - request: URLRequest
95-
/// - Returns: true - content-type header is not empty
96-
static func hasNotContentType(_ session : URLSession,_ request : URLRequest) -> Bool{
97-
request.value(forHTTPHeaderField: "Content-Type") == nil &&
98-
session.configuration.httpAdditionalHeaders?["Content-Type"] == nil
99-
}
10021
}

Sources/async-http-client/proxy/http/Proxy.swift

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

139139
private 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
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//
2-
// File.swift
2+
// ITaskDelegate.swift
33
//
44
//
55
// Created by Igor on 25.02.2023.
66
//
77

88
import Foundation
99

10-
public protocol ITaskDelegate: URLSessionTaskDelegate, Sendable{
10+
// A protocol that defines methods that URL session instances call on their delegates to handle task-level events
11+
public protocol ITaskDelegate: URLSessionTaskDelegate{
1112

1213
}

0 commit comments

Comments
 (0)