Skip to content

Commit

Permalink
Update orchestrator for generic input/output in interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed Apr 30, 2024
1 parent 4f196e0 commit 66d995a
Show file tree
Hide file tree
Showing 38 changed files with 319 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public struct IdempotencyTokenMiddleware<OperationStackInput, OperationStackOutp
}

extension IdempotencyTokenMiddleware: HttpInterceptor {
public func modifyBeforeSerialization(context: some MutableInput<AttributesType>) async throws {
if let input: OperationStackInput = context.getInput() {
let withToken = addToken(input: input, attributes: context.getAttributes())
context.updateInput(updated: withToken)
}
public typealias InputType = OperationStackInput
public typealias OutputType = OperationStackOutput

public func modifyBeforeSerialization(context: some MutableInput<InputType, AttributesType>) async throws {
let withToken = addToken(input: context.getInput(), attributes: context.getAttributes())
context.updateInput(updated: withToken)
}
}
78 changes: 27 additions & 51 deletions Sources/ClientRuntime/Interceptor/AnyInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,45 @@
/// This doesn't conform to Interceptor because the stored closures accept a concrete
/// DefaultInterceptorContext, not the boxed `some InterceptorContext`, which can't be
/// used in closure types.
internal struct AnyInterceptor<InputType, OutputType, RequestType, ResponseType, AttributesType: HasAttributes> {
internal struct AnyInterceptor<
InputType,
OutputType,
RequestType: RequestMessage,
ResponseType: ResponseMessage,
AttributesType: HasAttributes
> {
internal typealias InterceptorContextType = DefaultInterceptorContext<
InputType, OutputType, RequestType, ResponseType, AttributesType
>
internal typealias InterceptorFn = (InterceptorContextType) async throws -> Void

private var readBeforeExecution: InterceptorFn
private var modifyBeforeSerialization: InterceptorFn
private var readBeforeSerialization: InterceptorFn
private var readAfterSerialization: InterceptorFn
private var modifyBeforeRetryLoop: InterceptorFn
private var readBeforeAttempt: InterceptorFn
private var modifyBeforeSigning: InterceptorFn
private var readBeforeSigning: InterceptorFn
private var readAfterSigning: InterceptorFn
private var modifyBeforeTransmit: InterceptorFn
private var readBeforeTransmit: InterceptorFn
private var readAfterTransmit: InterceptorFn
private var modifyBeforeDeserialization: InterceptorFn
private var readBeforeDeserialization: InterceptorFn
private var readAfterDeserialization: InterceptorFn
private var modifyBeforeAttemptCompletion: InterceptorFn
private var readAfterAttempt: InterceptorFn
private var modifyBeforeCompletion: InterceptorFn
private var readAfterExecution: InterceptorFn
private var readBeforeExecution: InterceptorFn?
private var modifyBeforeSerialization: InterceptorFn?
private var readBeforeSerialization: InterceptorFn?
private var readAfterSerialization: InterceptorFn?
private var modifyBeforeRetryLoop: InterceptorFn?
private var readBeforeAttempt: InterceptorFn?
private var modifyBeforeSigning: InterceptorFn?
private var readBeforeSigning: InterceptorFn?
private var readAfterSigning: InterceptorFn?
private var modifyBeforeTransmit: InterceptorFn?
private var readBeforeTransmit: InterceptorFn?
private var readAfterTransmit: InterceptorFn?
private var modifyBeforeDeserialization: InterceptorFn?
private var readBeforeDeserialization: InterceptorFn?
private var readAfterDeserialization: InterceptorFn?
private var modifyBeforeAttemptCompletion: InterceptorFn?
private var readAfterAttempt: InterceptorFn?
private var modifyBeforeCompletion: InterceptorFn?
private var readAfterExecution: InterceptorFn?

internal init<I: Interceptor>(interceptor: I)
where
I.InputType == InputType,
I.OutputType == OutputType,
I.RequestType == RequestType,
I.ResponseType == ResponseType,
I.AttributesType == AttributesType
{
I.AttributesType == AttributesType {
self.readBeforeExecution = interceptor.readBeforeExecution(context:)
self.modifyBeforeSerialization = interceptor.modifyBeforeSerialization(context:)
self.readBeforeSerialization = interceptor.readBeforeSerialization(context:)
Expand Down Expand Up @@ -111,17 +116,12 @@ internal struct AnyInterceptor<InputType, OutputType, RequestType, ResponseType,
self.readAfterExecution = readAfterExecution
}

<<<<<<< HEAD
internal func modifyBeforeSerialization(context: InterceptorContextType) async throws {
try await self.modifyBeforeSerialization(context)
=======
internal func readBeforeExecution(context: InterceptorContextType) async throws {
try await self.readBeforeExecution?(context)
}

internal func modifyBeforeSerialization(context: InterceptorContextType) async throws {
try await self.modifyBeforeSerialization?(context)
>>>>>>> 6fca2117 (feat: add operation orchestrator)
}

internal func readBeforeSerialization(context: InterceptorContextType) async throws {
Expand All @@ -133,23 +133,15 @@ internal struct AnyInterceptor<InputType, OutputType, RequestType, ResponseType,
}

internal func modifyBeforeRetryLoop(context: InterceptorContextType) async throws {
<<<<<<< HEAD
try await self.modifyBeforeRetryLoop(context)
=======
try await self.modifyBeforeRetryLoop?(context)
>>>>>>> 6fca2117 (feat: add operation orchestrator)
}

internal func readBeforeAttempt(context: InterceptorContextType) async throws {
try await self.readBeforeAttempt?(context)
}

internal func modifyBeforeSigning(context: InterceptorContextType) async throws {
<<<<<<< HEAD
try await self.modifyBeforeSigning(context)
=======
try await self.modifyBeforeSigning?(context)
>>>>>>> 6fca2117 (feat: add operation orchestrator)
}

internal func readBeforeSigning(context: InterceptorContextType) async throws {
Expand All @@ -161,11 +153,7 @@ internal struct AnyInterceptor<InputType, OutputType, RequestType, ResponseType,
}

internal func modifyBeforeTransmit(context: InterceptorContextType) async throws {
<<<<<<< HEAD
try await self.modifyBeforeTransmit(context)
=======
try await self.modifyBeforeTransmit?(context)
>>>>>>> 6fca2117 (feat: add operation orchestrator)
}

internal func readBeforeTransmit(context: InterceptorContextType) async throws {
Expand All @@ -177,11 +165,7 @@ internal struct AnyInterceptor<InputType, OutputType, RequestType, ResponseType,
}

internal func modifyBeforeDeserialization(context: InterceptorContextType) async throws {
<<<<<<< HEAD
try await self.modifyBeforeDeserialization(context)
=======
try await self.modifyBeforeDeserialization?(context)
>>>>>>> 6fca2117 (feat: add operation orchestrator)
}

internal func readBeforeDeserialization(context: InterceptorContextType) async throws {
Expand All @@ -193,23 +177,15 @@ internal struct AnyInterceptor<InputType, OutputType, RequestType, ResponseType,
}

internal func modifyBeforeAttemptCompletion(context: InterceptorContextType) async throws {
<<<<<<< HEAD
try await self.modifyBeforeAttemptCompletion(context)
=======
try await self.modifyBeforeAttemptCompletion?(context)
>>>>>>> 6fca2117 (feat: add operation orchestrator)
}

internal func readAfterAttempt(context: InterceptorContextType) async throws {
try await self.readAfterAttempt?(context)
}

internal func modifyBeforeCompletion(context: InterceptorContextType) async throws {
<<<<<<< HEAD
try await self.modifyBeforeCompletion(context)
=======
try await self.modifyBeforeCompletion?(context)
>>>>>>> 6fca2117 (feat: add operation orchestrator)
}

internal func readAfterExecution(context: InterceptorContextType) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
///
/// This object will be created before operation execution, and passed through each interceptor
/// hook in the execution pipeline.
public class DefaultInterceptorContext<InputType, OutputType, RequestType, ResponseType, AttributesType: HasAttributes>:
InterceptorContext {
public class DefaultInterceptorContext<
InputType,
OutputType,
RequestType: RequestMessage,
ResponseType: ResponseMessage,
AttributesType: HasAttributes
>: InterceptorContext {
private var attributes: AttributesType
private var input: InputType
private var request: RequestType?
Expand Down
Loading

0 comments on commit 66d995a

Please sign in to comment.