Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to distinguish between multiple callbacks? #182

Open
njfanxun opened this issue Dec 8, 2022 · 2 comments
Open

How to distinguish between multiple callbacks? #182

njfanxun opened this issue Dec 8, 2022 · 2 comments

Comments

@njfanxun
Copy link

njfanxun commented Dec 8, 2022

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?

@PiotrSikora
Copy link
Contributor

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

Is that not the case?

@antonengelhardt
Copy link

antonengelhardt commented Jun 18, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants