Skip to content

Commit

Permalink
Merge pull request #106 from adam-fowler/response-middleware
Browse files Browse the repository at this point in the history
Added Middleware editing of response bodies
  • Loading branch information
adam-fowler authored Aug 17, 2019
2 parents 9c8c87d + 6910e59 commit 094c48f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
11 changes: 8 additions & 3 deletions Sources/AWSSDKSwiftCore/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct AWSClient {

let partitionEndpoint: String?

public let middlewares: [AWSRequestMiddleware]
public let middlewares: [AWSServiceMiddleware]

public var possibleErrorTypes: [AWSErrorType.Type]

Expand All @@ -68,7 +68,7 @@ public struct AWSClient {

public static let eventGroup: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

public init(accessKeyId: String? = nil, secretAccessKey: String? = nil, sessionToken: String? = nil, region givenRegion: Region?, amzTarget: String? = nil, service: String, serviceProtocol: ServiceProtocol, apiVersion: String, endpoint: String? = nil, serviceEndpoints: [String: String] = [:], partitionEndpoint: String? = nil, middlewares: [AWSRequestMiddleware] = [], possibleErrorTypes: [AWSErrorType.Type]? = nil) {
public init(accessKeyId: String? = nil, secretAccessKey: String? = nil, sessionToken: String? = nil, region givenRegion: Region?, amzTarget: String? = nil, service: String, serviceProtocol: ServiceProtocol, apiVersion: String, endpoint: String? = nil, serviceEndpoints: [String: String] = [:], partitionEndpoint: String? = nil, middlewares: [AWSServiceMiddleware] = [], possibleErrorTypes: [AWSErrorType.Type]? = nil) {
let credential: CredentialProvider
if let accessKey = accessKeyId, let secretKey = secretAccessKey {
credential = Credential(accessKeyId: accessKey, secretAccessKey: secretKey, sessionToken: sessionToken)
Expand Down Expand Up @@ -495,12 +495,17 @@ extension AWSClient {

try validateCode(response: response, members: Output._members)

let responseBody = try validateBody(
var responseBody = try validateBody(
for: response,
payloadPath: Output.payloadPath,
members: Output._members
)

// do we need to fix up the response before processing it
for middleware in middlewares {
responseBody = try middleware.chain(responseBody: responseBody)
}

let decoder = DictionaryDecoder()

var responseHeaders: [String: String] = [:]
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSSDKSwiftCore/Encoder/XMLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ fileprivate class _XMLDecoder : Decoder {
}

func unbox(_ element : XML.Element, as type: String.Type) throws -> String {
guard let unboxValue = element.stringValue else { throw DecodingError._typeMismatch(at: codingPath, expectation: Bool.self, reality: element.stringValue ?? "nil") }
guard let unboxValue = element.stringValue else { throw DecodingError._typeMismatch(at: codingPath, expectation: String.self, reality: element.stringValue ?? "nil") }
return unboxValue
}

Expand Down
16 changes: 13 additions & 3 deletions Sources/AWSSDKSwiftCore/Message/AWSRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ import NIO
import NIOTLS
import NIOHTTP1

public protocol AWSRequestMiddleware {
public protocol AWSServiceMiddleware {
func chain(request: AWSRequest) throws -> AWSRequest
func chain(responseBody: Body) throws -> Body
}

public extension AWSServiceMiddleware {
func chain(request: AWSRequest) throws -> AWSRequest {
return request
}
func chain(responseBody: Body) throws -> Body {
return responseBody
}
}

extension URL {
Expand Down Expand Up @@ -51,9 +61,9 @@ public struct AWSRequest {
public let httpMethod: String
public var httpHeaders: [String: Any?] = [:]
public var body: Body
public let middlewares: [AWSRequestMiddleware]
public let middlewares: [AWSServiceMiddleware]

public init(region: Region = .useast1, url: URL, serviceProtocol: ServiceProtocol, service: String, amzTarget: String? = nil, operation: String, httpMethod: String, httpHeaders: [String: Any?] = [:], body: Body = .empty, middlewares: [AWSRequestMiddleware] = []) {
public init(region: Region = .useast1, url: URL, serviceProtocol: ServiceProtocol, service: String, amzTarget: String? = nil, operation: String, httpMethod: String, httpHeaders: [String: Any?] = [:], body: Body = .empty, middlewares: [AWSServiceMiddleware] = []) {
self.region = region
self.url = url
self.serviceProtocol = serviceProtocol
Expand Down

0 comments on commit 094c48f

Please sign in to comment.