-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AWSEditHeadersMiddleware Middleware for adding, replacing, removing headers. Also moved some code about. * Fix Sendable error in swift 5.4 * comment update
- Loading branch information
1 parent
9dd1eab
commit 089fa02
Showing
6 changed files
with
193 additions
and
88 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
54 changes: 54 additions & 0 deletions
54
Sources/SotoCore/Message/Middleware/EditHeadersMiddleware.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,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Soto for AWS open source project | ||
// | ||
// Copyright (c) 2022 the Soto project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Soto project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import NIOHTTP1 | ||
|
||
/// Middleware for editing header values sent to AWS service. | ||
public struct AWSEditHeadersMiddleware: AWSServiceMiddleware { | ||
public enum HeaderEdit { | ||
case add(name: String, value: String) | ||
case replace(name: String, value: String) | ||
case remove(name: String) | ||
} | ||
|
||
let edits: [HeaderEdit] | ||
|
||
init(_ edits: [HeaderEdit]) { | ||
self.edits = edits | ||
} | ||
|
||
init(_ edits: HeaderEdit...) { | ||
self.init(edits) | ||
} | ||
|
||
public func chain(request: AWSRequest, context: AWSMiddlewareContext) throws -> AWSRequest { | ||
var request = request | ||
for edit in self.edits { | ||
switch edit { | ||
case .add(let name, let value): | ||
request.httpHeaders.add(name: name, value: value) | ||
case .replace(let name, let value): | ||
request.httpHeaders.replaceOrAdd(name: name, value: value) | ||
case .remove(let name): | ||
request.httpHeaders.remove(name: name) | ||
} | ||
} | ||
return request | ||
} | ||
} | ||
|
||
#if compiler(>=5.6) | ||
extension AWSEditHeadersMiddleware: Sendable {} | ||
extension AWSEditHeadersMiddleware.HeaderEdit: Sendable {} | ||
#endif |
98 changes: 98 additions & 0 deletions
98
Sources/SotoCore/Message/Middleware/LoggingMiddleware.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,98 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Soto for AWS open source project | ||
// | ||
// Copyright (c) 2017-2022 the Soto project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Soto project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Logging | ||
import NIOHTTP1 | ||
|
||
/// Middleware that outputs the contents of requests being sent to AWS and the contents of the responses received. | ||
public struct AWSLoggingMiddleware: AWSServiceMiddleware { | ||
#if compiler(>=5.6) | ||
public typealias LoggingFunction = @Sendable (String) -> Void | ||
#else | ||
public typealias LoggingFunction = (String) -> Void | ||
#endif | ||
/// initialize AWSLoggingMiddleware | ||
/// - parameters: | ||
/// - log: Function to call with logging output | ||
public init(log: @escaping LoggingFunction = { print($0) }) { | ||
self.log = { log($0()) } | ||
} | ||
|
||
/// initialize AWSLoggingMiddleware to use Logger | ||
/// - Parameters: | ||
/// - logger: Logger to use | ||
/// - logLevel: Log level to output at | ||
public init(logger: Logger, logLevel: Logger.Level = .info) { | ||
self.log = { logger.log(level: logLevel, "\($0())") } | ||
} | ||
|
||
func getBodyOutput(_ body: Body) -> String { | ||
var output = "" | ||
switch body { | ||
case .xml(let element): | ||
output += "\n " | ||
output += element.description | ||
case .json(let buffer): | ||
output += "\n " | ||
output += buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) ?? "Failed to convert JSON response to UTF8" | ||
case .raw(let payload): | ||
output += "raw (\(payload.size?.description ?? "unknown") bytes)" | ||
case .text(let string): | ||
output += "\n \(string)" | ||
case .empty: | ||
output += "empty" | ||
} | ||
return output | ||
} | ||
|
||
func getHeadersOutput(_ headers: HTTPHeaders) -> String { | ||
if headers.count == 0 { | ||
return "[]" | ||
} | ||
var output = "[" | ||
for header in headers { | ||
output += "\n \(header.name) : \(header.value)" | ||
} | ||
return output + "\n ]" | ||
} | ||
|
||
/// output request | ||
public func chain(request: AWSRequest, context: AWSMiddlewareContext) throws -> AWSRequest { | ||
self.log( | ||
"Request:\n" + | ||
" \(request.operation)\n" + | ||
" \(request.httpMethod) \(request.url)\n" + | ||
" Headers: \(self.getHeadersOutput(request.httpHeaders))\n" + | ||
" Body: \(self.getBodyOutput(request.body))" | ||
) | ||
return request | ||
} | ||
|
||
/// output response | ||
public func chain(response: AWSResponse, context: AWSMiddlewareContext) throws -> AWSResponse { | ||
self.log( | ||
"Response:\n" + | ||
" Status : \(response.status.code)\n" + | ||
" Headers: \(self.getHeadersOutput(HTTPHeaders(response.headers.map { ($0, "\($1)") })))\n" + | ||
" Body: \(self.getBodyOutput(response.body))" | ||
) | ||
return response | ||
} | ||
|
||
#if compiler(>=5.6) | ||
let log: @Sendable (@autoclosure () -> String) -> Void | ||
#else | ||
let log: (@autoclosure () -> String) -> Void | ||
#endif | ||
} |
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