proxy-wasm-rust-sdk
proxy-wasm-rust-sdk copied to clipboard
How to distinguish between multiple callbacks?
impl HttpContext for my struct, in "on_http_call_response" function ,i how to distinguish multiple dispatch_http_call?
dispatch_http_call different address and response also not the same.
I need to parse the results, but one is xml and the other is json.
I only have one token_id,
so I can't know which request call returns.
Is there any good solution to offer?
on_http_call_response
contains token_id
which should match the returned token_id
from dispatch_http_call
.
Is that not the case?
@njfanxun token_id
works perfectly fine.
You can also use enums in Rust and control which response you are expecting. Let me show you:
pub struct SomeContext {
pub state State,
}
enum State {
State1,
State2,
}
impl HttpContext for SomeContext {
fn on_http_request_headers(&mut self, _: usize) -> Action {
match self.state {
State::State1 => {
// Call 1
self.dispatch_http_call(...);
}
State::State2 => {
// Call 2
self.dispatch_http_call(...);
}
}
}
}
impl Context for SomeContext {
fn on_http_call_response(&mut self, _: usize, _: usize, _: usize, _: usize) -> Action {
match self.state {
State::State1 => {
// Handle response 1
let body = self.get_http_call_response_body(...);
// Change state to State2
self.state = State::State2;
}
State::State2 => {
// Handle response 2
let body = self.get_http_call_response_body(...);
}
}
}
}
I haven't syntax checked this code so there might be some things that the compiler complains about but i am using this logic in my OIDC Filter implementation to get configuration from OpenID Config & JWKs Endpoints. Hope it helps :)