Skip to content

Commit e9826c0

Browse files
Merge pull request #34 from Yalantis/updates
Updates
2 parents 23121cd + b161ace commit e9826c0

16 files changed

+183
-159
lines changed

APIClient-Example/Gemfile.lock

+50-36
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,91 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
CFPropertyList (3.0.1)
5-
activesupport (4.2.11.1)
6-
i18n (~> 0.7)
7-
minitest (~> 5.1)
8-
thread_safe (~> 0.3, >= 0.3.4)
9-
tzinfo (~> 1.1)
10-
algoliasearch (1.27.1)
4+
CFPropertyList (3.0.5)
5+
rexml
6+
activesupport (6.1.6.1)
7+
concurrent-ruby (~> 1.0, >= 1.0.2)
8+
i18n (>= 1.6, < 2)
9+
minitest (>= 5.1)
10+
tzinfo (~> 2.0)
11+
zeitwerk (~> 2.3)
12+
addressable (2.8.0)
13+
public_suffix (>= 2.0.2, < 5.0)
14+
algoliasearch (1.27.5)
1115
httpclient (~> 2.8, >= 2.8.3)
1216
json (>= 1.5.1)
1317
atomos (0.1.3)
14-
claide (1.0.3)
15-
cocoapods (1.8.4)
16-
activesupport (>= 4.0.2, < 5)
18+
claide (1.1.0)
19+
cocoapods (1.11.3)
20+
addressable (~> 2.8)
1721
claide (>= 1.0.2, < 2.0)
18-
cocoapods-core (= 1.8.4)
22+
cocoapods-core (= 1.11.3)
1923
cocoapods-deintegrate (>= 1.0.3, < 2.0)
20-
cocoapods-downloader (>= 1.2.2, < 2.0)
24+
cocoapods-downloader (>= 1.4.0, < 2.0)
2125
cocoapods-plugins (>= 1.0.0, < 2.0)
2226
cocoapods-search (>= 1.0.0, < 2.0)
23-
cocoapods-stats (>= 1.0.0, < 2.0)
2427
cocoapods-trunk (>= 1.4.0, < 2.0)
2528
cocoapods-try (>= 1.1.0, < 2.0)
2629
colored2 (~> 3.1)
2730
escape (~> 0.0.4)
2831
fourflusher (>= 2.3.0, < 3.0)
2932
gh_inspector (~> 1.0)
30-
molinillo (~> 0.6.6)
33+
molinillo (~> 0.8.0)
3134
nap (~> 1.0)
32-
ruby-macho (~> 1.4)
33-
xcodeproj (>= 1.11.1, < 2.0)
34-
cocoapods-core (1.8.4)
35-
activesupport (>= 4.0.2, < 6)
35+
ruby-macho (>= 1.0, < 3.0)
36+
xcodeproj (>= 1.21.0, < 2.0)
37+
cocoapods-core (1.11.3)
38+
activesupport (>= 5.0, < 7)
39+
addressable (~> 2.8)
3640
algoliasearch (~> 1.0)
3741
concurrent-ruby (~> 1.1)
3842
fuzzy_match (~> 2.0.4)
3943
nap (~> 1.0)
40-
cocoapods-deintegrate (1.0.4)
41-
cocoapods-downloader (1.2.2)
44+
netrc (~> 0.11)
45+
public_suffix (~> 4.0)
46+
typhoeus (~> 1.0)
47+
cocoapods-deintegrate (1.0.5)
48+
cocoapods-downloader (1.6.3)
4249
cocoapods-plugins (1.0.0)
4350
nap
44-
cocoapods-search (1.0.0)
45-
cocoapods-stats (1.1.0)
46-
cocoapods-trunk (1.4.1)
51+
cocoapods-search (1.0.1)
52+
cocoapods-trunk (1.6.0)
4753
nap (>= 0.8, < 2.0)
4854
netrc (~> 0.11)
49-
cocoapods-try (1.1.0)
55+
cocoapods-try (1.2.0)
5056
colored2 (3.1.2)
51-
concurrent-ruby (1.1.5)
57+
concurrent-ruby (1.1.10)
5258
escape (0.0.4)
59+
ethon (0.15.0)
60+
ffi (>= 1.15.0)
61+
ffi (1.15.5)
5362
fourflusher (2.3.1)
5463
fuzzy_match (2.0.4)
5564
gh_inspector (1.1.3)
5665
httpclient (2.8.3)
57-
i18n (0.9.5)
66+
i18n (1.12.0)
5867
concurrent-ruby (~> 1.0)
59-
json (2.2.0)
60-
minitest (5.12.2)
61-
molinillo (0.6.6)
62-
nanaimo (0.2.6)
68+
json (2.6.2)
69+
minitest (5.16.2)
70+
molinillo (0.8.0)
71+
nanaimo (0.3.0)
6372
nap (1.1.0)
6473
netrc (0.11.0)
65-
ruby-macho (1.4.0)
66-
thread_safe (0.3.6)
67-
tzinfo (1.2.5)
68-
thread_safe (~> 0.1)
69-
xcodeproj (1.13.0)
74+
public_suffix (4.0.7)
75+
rexml (3.2.5)
76+
ruby-macho (2.5.1)
77+
typhoeus (1.4.0)
78+
ethon (>= 0.9.0)
79+
tzinfo (2.0.5)
80+
concurrent-ruby (~> 1.0)
81+
xcodeproj (1.22.0)
7082
CFPropertyList (>= 2.3.3, < 4.0)
7183
atomos (~> 0.1.3)
7284
claide (>= 1.0.2, < 2.0)
7385
colored2 (~> 3.1)
74-
nanaimo (~> 0.2.6)
86+
nanaimo (~> 0.3.0)
87+
rexml (~> 3.2.4)
88+
zeitwerk (2.6.0)
7589

7690
PLATFORMS
7791
ruby
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import Foundation
22
import Alamofire
33

4+
public typealias APIHTTPHeaders = HTTPHeaders
5+
46
public extension APIRequest {
57

68
var alamofireMethod: HTTPMethod {
7-
return HTTPMethod(rawValue: method.rawValue.uppercased()) ?? .get
9+
HTTPMethod(rawValue: method.rawValue.uppercased())
810
}
911

1012
var alamofireEncoding: ParameterEncoding {
11-
return encoding as? ParameterEncoding ?? URLEncoding.default
13+
encoding as? ParameterEncoding ?? URLEncoding.default
14+
}
15+
16+
var alamofireHeaders: HTTPHeaders {
17+
HTTPHeaders(headers ?? [:])
1218
}
1319

1420
}
1521

1622
extension URLEncoding: APIRequestEncoding {}
17-
extension PropertyListEncoding: APIRequestEncoding {}
23+
extension PropertyListEncoder: APIRequestEncoding {}
1824
extension JSONEncoding: APIRequestEncoding {}

APIClient/Alamofire/AlamofireRequestExecutor.swift

+42-42
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import Alamofire
33

44
open class AlamofireRequestExecutor: RequestExecutor {
55

6-
private let manager: SessionManager
6+
private let manager: Session
77
private let baseURL: URL
88

9-
public init(baseURL: URL, manager: SessionManager = SessionManager.default) {
9+
public init(baseURL: URL, manager: Session = .default) {
1010
self.manager = manager
1111
self.baseURL = baseURL
1212
}
@@ -20,7 +20,7 @@ open class AlamofireRequestExecutor: RequestExecutor {
2020
method: request.alamofireMethod,
2121
parameters: request.parameters,
2222
encoding: request.alamofireEncoding,
23-
headers: request.headers
23+
headers: request.alamofireHeaders
2424
)
2525
.response { response in
2626
guard let httpResponse = response.response, let data = response.data else {
@@ -40,39 +40,36 @@ open class AlamofireRequestExecutor: RequestExecutor {
4040
public func execute(multipartRequest: MultipartAPIRequest, completion: @escaping APIResultResponse) -> Cancelable {
4141
let cancellationSource = CancellationTokenSource()
4242
let requestPath = path(for: multipartRequest)
43-
44-
manager
45-
.upload(
46-
multipartFormData: multipartRequest.multipartFormData,
47-
to: requestPath,
48-
method: multipartRequest.alamofireMethod,
49-
headers: multipartRequest.headers,
50-
encodingCompletion: { encodingResult in
51-
switch encodingResult {
52-
case .success(var request, _, _):
53-
cancellationSource.token.register {
54-
request.cancel()
55-
}
56-
57-
if let progressHandler = multipartRequest.progressHandler {
58-
request = request.uploadProgress { progress in
59-
progressHandler(progress)
60-
}
61-
}
62-
request.responseJSON(completionHandler: { response in
63-
guard let httpResponse = response.response, let data = response.data else {
64-
AlamofireRequestExecutor.defineError(response.error, completion: completion)
65-
return
66-
}
67-
68-
completion(.success((httpResponse, data)))
69-
})
70-
71-
case .failure(let error):
72-
completion(.failure(error))
73-
}
74-
})
75-
43+
44+
manager.upload(
45+
multipartFormData: multipartRequest.multipartFormData,
46+
to: requestPath,
47+
method: multipartRequest.alamofireMethod,
48+
headers: multipartRequest.alamofireHeaders)
49+
.uploadProgress(queue: .main, closure: { progress in
50+
if let progressHandler = multipartRequest.progressHandler {
51+
progressHandler(progress)
52+
}
53+
})
54+
.responseJSON { response in
55+
switch response.result {
56+
case .success:
57+
guard
58+
let httpResponse = response.response,
59+
let data = response.data
60+
else {
61+
AlamofireRequestExecutor.defineError(response.error, completion: completion)
62+
63+
return
64+
}
65+
66+
completion(.success((httpResponse, data)))
67+
68+
case .failure(let error):
69+
completion(.failure(error))
70+
}
71+
}
72+
7673
return cancellationSource
7774
}
7875

@@ -85,7 +82,7 @@ open class AlamofireRequestExecutor: RequestExecutor {
8582
method: downloadRequest.alamofireMethod,
8683
parameters: downloadRequest.parameters,
8784
encoding: downloadRequest.alamofireEncoding,
88-
headers: downloadRequest.headers,
85+
headers: downloadRequest.alamofireHeaders,
8986
to: destination(for: destinationPath)
9087
)
9188

@@ -96,9 +93,12 @@ open class AlamofireRequestExecutor: RequestExecutor {
9693
}
9794

9895
request.responseData { response in
99-
guard let httpResponse = response.response, let data = response.result.value else {
100-
AlamofireRequestExecutor.defineError(response.error, completion: completion)
101-
return
96+
guard
97+
let httpResponse = response.response,
98+
let data = response.value
99+
else {
100+
AlamofireRequestExecutor.defineError(response.error, completion: completion)
101+
return
102102
}
103103

104104
completion(.success((httpResponse, data)))
@@ -118,11 +118,11 @@ open class AlamofireRequestExecutor: RequestExecutor {
118118
.removingPercentEncoding!
119119
}
120120

121-
private func destination(for url: URL?) -> DownloadRequest.DownloadFileDestination? {
121+
private func destination(for url: URL?) -> DownloadRequest.Destination? {
122122
guard let url = url else {
123123
return nil
124124
}
125-
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
125+
let destination: DownloadRequest.Destination = { _, _ in
126126
return (url, [.removePreviousFile, .createIntermediateDirectories])
127127
}
128128

0 commit comments

Comments
 (0)