proxy-wasm-rust-sdk icon indicating copy to clipboard operation
proxy-wasm-rust-sdk copied to clipboard

How to distinguish between multiple callbacks?

Open njfanxun opened this issue 2 years ago • 2 comments

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?

njfanxun avatar Dec 08 '22 09:12 njfanxun

on_http_call_response contains token_id which should match the returned token_id from dispatch_http_call.

Is that not the case?

PiotrSikora avatar Dec 19 '22 01:12 PiotrSikora

@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 :)

antonengelhardt avatar Jun 18 '23 11:06 antonengelhardt