I'm using grpc-swift and the ClientInterceptor
to add an auth token to requests. I have implemented the delegate method for send
as follows
override func send(_ part: GRPCClientRequestPart<Request>, promise: EventLoopPromise<()>?, context: ClientInterceptorContext<Request, Response>) {
guard case .metadata(var headers) = part else {
return context.send(part, promise: promise)
}
let p = context.eventLoop.makePromise(of: String.self)
p.completeWithTask {
"My Token" // In reality this is actually an async function like `await myActor.idToken`
}
p.futureResult.whenSuccess { token in
print(context.eventLoop.inEventLoop) // This returns `true`
headers.add(name: "Authorization", value: "Bearer \(token)")
context.send(.metadata(headers), promise: promise)
}
}
I need to read the values from an actor
which requires async/await
to get the values hence the use of creating a promise and then p.completeWithTask
.
The problem I am facing is when doing this and then calling the context.send(_,promise:)
is that I receive an error back saying Invalid state: unable to write message
, looking at the docs it says /// An invalid state was encountered. This is a serious implementation error.
, I'm clearly doing something very wrong here but for the life of me I am not sure what?
If this is used without promises/futures it all works fine. I'm not sure if I am using the p.completeWithTask
& p.futureResult.whenSuccess
correctly here?