-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split out some files and clean up error handling in foundation client
- Loading branch information
Showing
3 changed files
with
151 additions
and
81 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...gins/Core/AWSPluginsCore/Utils/CustomHttpClientEngine/ClientRuntimeFoundationBridge.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import ClientRuntime | ||
|
||
extension Foundation.URLRequest { | ||
init(sdkRequest: ClientRuntime.SdkHttpRequest) throws { | ||
guard let url = sdkRequest.endpoint.url else { | ||
throw FoundationHTTPClientError.invalidRequestURL(sdkRequest: sdkRequest) | ||
} | ||
self.init(url: url) | ||
httpMethod = sdkRequest.method.rawValue | ||
|
||
for header in sdkRequest.headers.headers { | ||
for value in header.value { | ||
addValue(value, forHTTPHeaderField: header.name) | ||
} | ||
} | ||
|
||
switch sdkRequest.body { | ||
case .data(let data): httpBody = data | ||
case .stream(let stream): httpBody = stream.toBytes().getData() | ||
case .none: break | ||
} | ||
} | ||
} | ||
|
||
extension ClientRuntime.HttpResponse { | ||
private static func headers( | ||
from allHeaderFields: [AnyHashable: Any] | ||
) -> ClientRuntime.Headers { | ||
var headers = Headers() | ||
for header in allHeaderFields { | ||
switch (header.key, header.value) { | ||
case let (key, value) as (String, String): | ||
headers.add(name: key, value: value) | ||
case let (key, values) as (String, [String]): | ||
headers.add(name: key, values: values) | ||
default: continue | ||
} | ||
} | ||
return headers | ||
} | ||
|
||
convenience init(httpURLResponse: HTTPURLResponse, data: Data) throws { | ||
let headers = Self.headers(from: httpURLResponse.allHeaderFields) | ||
let body = HttpBody.stream(ByteStream.from(data: data)) | ||
|
||
guard let statusCode = HttpStatusCode(rawValue: httpURLResponse.statusCode) else { | ||
// This shouldn't happen, but `HttpStatusCode` only exposes a failable | ||
// `init`. The alternative here is force unwrapping, but we can't | ||
// make the decision to crash here on behalf on consuming applications. | ||
throw FoundationHTTPClientError.unexpectedStatusCode( | ||
statusCode: httpURLResponse.statusCode | ||
) | ||
} | ||
self.init(headers: headers, body: body, statusCode: statusCode) | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...s/Core/AWSPluginsCore/Utils/CustomHttpClientEngine/FoundationHTTPClientEngine+Error.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
import ClientRuntime | ||
|
||
struct FoundationHTTPClientError: AmplifyError { | ||
let errorDescription: ErrorDescription | ||
let recoverySuggestion: RecoverySuggestion | ||
let underlyingError: Error? | ||
|
||
// protocol requirement | ||
init( | ||
errorDescription: ErrorDescription, | ||
recoverySuggestion: RecoverySuggestion, | ||
error: Error | ||
) { | ||
self.errorDescription = errorDescription | ||
self.recoverySuggestion = recoverySuggestion | ||
self.underlyingError = error | ||
} | ||
} | ||
|
||
extension FoundationHTTPClientError { | ||
init( | ||
errorDescription: ErrorDescription, | ||
recoverySuggestion: RecoverySuggestion, | ||
error: Error? | ||
) { | ||
self.errorDescription = errorDescription | ||
self.recoverySuggestion = recoverySuggestion | ||
self.underlyingError = error | ||
} | ||
|
||
static func invalidRequestURL(sdkRequest: ClientRuntime.SdkHttpRequest) -> Self { | ||
.init( | ||
errorDescription: """ | ||
The SdkHttpRequest generated by ClientRuntime doesn't include a valid URL | ||
- \(sdkRequest) | ||
""", | ||
recoverySuggestion: """ | ||
Please open an issue at https://github.com/aws-amplify/amplify-swift | ||
with the contents of this error message. | ||
""", | ||
error: nil | ||
) | ||
} | ||
|
||
static func invalidURLResponse(urlRequest: URLResponse) -> Self { | ||
.init( | ||
errorDescription: """ | ||
The URLResponse received is not an HTTPURLResponse | ||
- \(urlRequest) | ||
""", | ||
recoverySuggestion: """ | ||
Please open an issue at https://github.com/aws-amplify/amplify-swift | ||
with the contents of this error message. | ||
""", | ||
error: nil | ||
) | ||
} | ||
|
||
static func unexpectedStatusCode(statusCode: Int) -> Self { | ||
.init( | ||
errorDescription: """ | ||
The status code received isn't a valid `HttpStatusCode` value. | ||
- status code: \(statusCode) | ||
""", | ||
recoverySuggestion: """ | ||
Please open an issue at https://github.com/aws-amplify/amplify-swift | ||
with the contents of this error message. | ||
""", | ||
error: nil | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters