-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added APIGateway and S3Control Middleware (#261)
- Added APIGatewayMiddleware: If an Accept header doesn't already exist adds one with value "application/json". This is required to get error codes back - Added S3ControlMiddleware: Needed to do virtualAddressFixup
- Loading branch information
1 parent
6059bfc
commit 5ed9d17
Showing
6 changed files
with
68 additions
and
10 deletions.
There are no files selected for viewing
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
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
12 changes: 12 additions & 0 deletions
12
Sources/AWSSDKSwift/Extensions/APIGateway/APIGatewayMiddleware.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,12 @@ | ||
import AWSSDKSwiftCore | ||
|
||
public struct APIGatewayMiddleware: AWSServiceMiddleware { | ||
public func chain(request: AWSRequest) throws -> AWSRequest { | ||
var request = request | ||
// have to set Accept header to application/json otherwise errors are not returned correctly | ||
if request.httpHeaders["Accept"] == nil { | ||
request.httpHeaders["Accept"] = "application/json" | ||
} | ||
return request | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Sources/AWSSDKSwift/Extensions/S3Control/S3ControlMiddleware.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,35 @@ | ||
import struct Foundation.CharacterSet | ||
import struct Foundation.URL | ||
import struct Foundation.URLComponents | ||
import struct Foundation.URLQueryItem | ||
import AWSSDKSwiftCore | ||
|
||
public struct S3ControlMiddleware: AWSServiceMiddleware { | ||
|
||
public init () {} | ||
|
||
/// edit request before sending to S3 | ||
public func chain(request: AWSRequest) throws -> AWSRequest { | ||
var request = request | ||
|
||
virtualAddressFixup(request: &request) | ||
|
||
return request | ||
} | ||
|
||
func virtualAddressFixup(request: inout AWSRequest) { | ||
/// process URL into form ${account-id}.s3control.${region}.amazon.com | ||
guard let accountId = request.httpHeaders["x-amz-account-id"] else { return } | ||
guard let host = request.url.host else { return } | ||
let urlHost = "\(accountId).\(host)" | ||
var urlPath = "\(urlHost)\(request.url.path)" | ||
// add percent encoding back into path as converting from URL to String has removed it | ||
urlPath = urlPath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? urlPath | ||
var urlString = "\(request.url.scheme ?? "https")://\(urlPath)" | ||
if let query = request.url.query { | ||
urlString += "?\(query)" | ||
} | ||
request.url = URL(string: urlString)! | ||
} | ||
} | ||
|
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
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