Skip to content

Commit 843990d

Browse files
authored
Merge pull request #6 from Hexaville/prorosum-http-client
replace http client from URLSession to Prorsum.HTTPClient
2 parents 9dbb01b + f7d1bbd commit 843990d

File tree

5 files changed

+82
-89
lines changed

5 files changed

+82
-89
lines changed

Sources/HexavilleAuth/OAuth/OAuth1.swift

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public enum OAuth1Error: Error {
2323
extension OAuth1Error: CustomStringConvertible {
2424
public var description: String {
2525
switch self {
26+
case .couldNotGenerateSignature:
27+
return "couldNotGenerateSignature"
28+
29+
case .invalidAuthrozeURL(let url):
30+
return "invalidAuthrozeURL: \(url)"
31+
32+
case .missingRequiredParameters(let param):
33+
return "missingRequiredParameter: \(param)"
34+
35+
case .accessTokenIsMissingInSession:
36+
return "accessTokenIsMissingInSession"
37+
2638
case .verifyFailed(let req, let res):
2739
return stringify(code: "verifyFailed", request: req, response: res)
2840

@@ -31,9 +43,6 @@ extension OAuth1Error: CustomStringConvertible {
3143

3244
case .failedToGetRequestToken(let req, let res):
3345
return stringify(code: "failedToGetRequestToken", request: req, response: res)
34-
35-
default:
36-
return "\(self)"
3746
}
3847
}
3948

@@ -123,22 +132,23 @@ public class OAuth1 {
123132

124133
params["oauth_signature"] = sig
125134

126-
var urlRequest = URLRequest(url: URL(string: requestTokenUrl)!)
127-
urlRequest.httpMethod = "POST"
128-
129-
urlRequest.addValue(OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters), forHTTPHeaderField: "Authorization")
130-
131-
let (response, data) = try URLSession.shared.resumeSync(with: urlRequest)
135+
let authorizationValue = OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters)
132136

133-
let bodyDictionary = OAuth1.parse(bodyData: data)
137+
let request = Request(
138+
method: .post,
139+
url: URL(string: requestTokenUrl)!,
140+
headers: ["Authorization": authorizationValue]
141+
)
142+
let client = try HTTPClient(url: request.url)
143+
try client.open()
144+
let response = try client.request(request)
134145

135146
guard (200..<300).contains(response.statusCode) else {
136-
throw OAuth1Error.failedToGetRequestToken(
137-
urlRequest.transform(),
138-
response.transform(withBodyData: data)
139-
)
147+
throw OAuth1Error.failedToGetRequestToken(request, response)
140148
}
141149

150+
let bodyDictionary = OAuth1.parse(bodyData: response.body.asData())
151+
142152
guard let oauthToken = bodyDictionary["oauth_token"] else {
143153
throw OAuth1Error.missingRequiredParameters("oauth_token")
144154
}
@@ -189,21 +199,23 @@ public class OAuth1 {
189199

190200
params["oauth_signature"] = sig
191201

192-
var urlRequest = URLRequest(url: URL(string: verifyURL)!)
193-
urlRequest.httpMethod = "GET"
202+
let authrozationString = OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters)
194203

195-
urlRequest.addValue(OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters), forHTTPHeaderField: "Authorization")
204+
let request = Request(
205+
method: .get,
206+
url: URL(string: verifyURL)!,
207+
headers: ["Authorization": authrozationString]
208+
)
196209

197-
let (response, data) = try URLSession.shared.resumeSync(with: urlRequest)
210+
let client = try HTTPClient(url: request.url)
211+
try client.open()
212+
let response = try client.request(request)
198213

199214
guard (200..<300).contains(response.statusCode) else {
200-
throw OAuth1Error.verifyFailed(
201-
urlRequest.transform(),
202-
response.transform(withBodyData: data)
203-
)
215+
throw OAuth1Error.verifyFailed(request, response)
204216
}
205217

206-
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] ?? [:]
218+
return try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as? [String: Any] ?? [:]
207219
}
208220

209221
public func getAccessToken(request: Request, requestToken: RequestToken) throws -> Credential {
@@ -239,21 +251,23 @@ public class OAuth1 {
239251

240252
params["oauth_signature"] = sig
241253

242-
var urlRequest = URLRequest(url: URL(string: urlString)!)
243-
urlRequest.httpMethod = "POST"
254+
let authrozationString = OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters)
244255

245-
urlRequest.addValue(OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters), forHTTPHeaderField: "Authorization")
256+
let request = Request(
257+
method: .post,
258+
url: URL(string: urlString)!,
259+
headers: ["Authorization": authrozationString]
260+
)
246261

247-
let (response, data) = try URLSession.shared.resumeSync(with: urlRequest)
262+
let client = try HTTPClient(url: request.url)
263+
try client.open()
264+
let response = try client.request(request)
248265

249266
guard (200..<300).contains(response.statusCode) else {
250-
throw OAuth1Error.failedToGetAccessToken(
251-
urlRequest.transform(),
252-
response.transform(withBodyData: data)
253-
)
267+
throw OAuth1Error.failedToGetAccessToken(request, response)
254268
}
255269

256-
return try Credential(withDictionary: OAuth1.parse(bodyData: data))
270+
return try Credential(withDictionary: OAuth1.parse(bodyData: response.body.asData()))
257271
}
258272
}
259273

Sources/HexavilleAuth/OAuth/OAuth2.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,26 @@ public class OAuth2 {
6868
"redirect_uri=\(self.callbackURL.absoluteURL()!.absoluteString)"
6969
]
7070

71-
var request = URLRequest(url: url)
72-
request.httpMethod = "POST"
73-
request.addValue("application/json", forHTTPHeaderField: "Accept")
74-
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
75-
request.httpBody = body.joined(separator: "&").data
71+
let request = Request(
72+
method: .post,
73+
url: url,
74+
headers: [
75+
"Accept": "application/json",
76+
"Content-Type": "application/x-www-form-urlencoded"
77+
],
78+
body: body.joined(separator: "&").data
79+
)
7680

77-
let (response, bodyData) = try URLSession.shared.resumeSync(with: request)
81+
let client = try HTTPClient(url: request.url)
82+
try client.open()
83+
let response = try client.request(request)
7884

7985
guard (200..<300).contains(response.statusCode) else {
80-
throw HexavilleAuthError.responseError(response.transform(withBodyData: bodyData))
86+
throw HexavilleAuthError.responseError(response)
8187
}
8288

8389
do {
84-
let bodyDictionary = try JSONSerialization.jsonObject(with: bodyData, options: []) as! [String: Any]
90+
let bodyDictionary = try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as! [String: Any]
8591
return try Credential(withDictionary: bodyDictionary)
8692
} catch {
8793
throw error

Sources/HexavilleAuth/Providers/OAuth2/FacebookAuthorizationProvider.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ public struct FacebookAuthorizationProvider: OAuth2AuthorizationProvidable {
3838

3939
public func authorize(request: Request) throws -> (Credential, LoginUser) {
4040
let credential = try self.getAccessToken(request: request)
41-
let url = URL(string: "https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=\(credential.accessToken)")!
42-
let (response, data) = try URLSession.shared.resumeSync(with: URLRequest(url: url))
41+
42+
let request = Request(
43+
method: .get,
44+
url: URL(string: "https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=\(credential.accessToken)")!
45+
)
46+
47+
let client = try HTTPClient(url: request.url)
48+
try client.open()
49+
let response = try client.request(request)
4350

4451
guard (200..<300).contains(response.statusCode) else {
45-
throw HexavilleAuthError.responseError(response.transform(withBodyData: data))
52+
throw HexavilleAuthError.responseError(response)
4653
}
4754

48-
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
55+
guard let json = try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as? [String: Any] else {
4956
throw FacebookAuthorizationProviderError.bodyShouldBeAJSON
5057
}
5158

Sources/HexavilleAuth/Providers/OAuth2/GithubAuthorizationProvider.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ public struct GithubAuthorizationProvider: OAuth2AuthorizationProvidable {
3838

3939
public func authorize(request: Request) throws -> (Credential, LoginUser) {
4040
let credential = try self.getAccessToken(request: request)
41-
let url = URL(string: "https://api.github.com/user?access_token=\(credential.accessToken)")!
42-
let (response, data) = try URLSession.shared.resumeSync(with: URLRequest(url: url))
41+
42+
let request = Request(
43+
method: .get,
44+
url: URL(string: "https://api.github.com/user?access_token=\(credential.accessToken)")!
45+
)
46+
47+
let client = try HTTPClient(url: request.url)
48+
try client.open()
49+
let response = try client.request(request)
4350

4451
guard (200..<300).contains(response.statusCode) else {
45-
throw HexavilleAuthError.responseError(response.transform(withBodyData: data))
52+
throw HexavilleAuthError.responseError(response)
4653
}
4754

48-
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
55+
guard let json = try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as? [String: Any] else {
4956
throw GithubAuthorizationProviderError.bodyShouldBeAJSON
5057
}
5158

Sources/HexavilleAuth/URLSession.swift

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

0 commit comments

Comments
 (0)