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

Interceptors are single-shot #1036

Closed
AndreGleichner opened this issue Apr 5, 2024 · 0 comments · Fixed by #1038
Closed

Interceptors are single-shot #1036

AndreGleichner opened this issue Apr 5, 2024 · 0 comments · Fixed by #1038
Labels

Comments

@AndreGleichner
Copy link
Contributor

AndreGleichner commented Apr 5, 2024

Description

Even the most trivial use of AddInterceptor leads only to interceptors being called for a single request. Unfortunately the current implementation drops interceptors from the queue once they are called, see Session::intercept(). My understanding of the purpose of having a Session object is to being able to perform multiple calls to e.g. Get() and thus my assumption was that all added interceptors stay active for the session lifetime and are called for any number of Get() calls.
I was trying to implement some resilience policies, retry mainly, as the documentation states "...which can then monitor, modify and repeat requests". I was assuming that intercept() could call proceed() multiple times to repeat requests, but that was a non-starter.

Example/How to Reproduce

class LogInterceptor : public cpr::Interceptor
{
public:
    cpr::Response intercept(cpr::Session& session) override
    {
        auto r = proceed(session);
        std::cout << "In LogInterceptor" << std::endl;
        return r;
    }
};
void main()
{
    cpr::Session session;
    session.SetUrl("https://httpstat.us/200");
    session.AddInterceptor(std::make_shared<LogInterceptor>());
    auto r = session.Get();
    // This is required to get the interceptor also called during next Get()
    // session.AddInterceptor(std::make_shared<LogInterceptor>());
    r = session.Get();
}

Possible Fix

To fix the simple case of keeping interceptors active for any number of requests you may "just" not pop interceptors out of the queue and instead iterate over them.
To implement the promised "can repeat requests" fully (calling all interceptors) it may require quite some effort. Maybe calling only down-level interceptors in repeated requests should-just-work (TM) if fixing the above.

Where did you get it from?

vcpkg

Additional Context/Your Environment

  • OS: Windows 11
  • Version: 1.10.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants