libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.59k stars 937 forks source link

Mutiperform Intercept #863

Closed COM8 closed 1 year ago

COM8 commented 1 year ago

This PR adds support for intercepts when using cpr::MultiPerform. Fixes #862.

Examples:

class LoggingInterceptor : public InterceptorMulti {
  public:
    std::vector<Response> intercept(MultiPerform& multi) {
        // Log the request URL
        std::cout << "Request url:  " << multi.GetSessions().front().first->GetFullRequestUrl(); << '\n';

        // Proceed the request and save the response
        std::vector<cpr::Response> response = proceed(multi);

        // Log response status code
        std::cout << "Response status code:  " << response.front().status_code << '\n';

        // Return the stored response
        return response;
    }
};

int main() {
    Url url{"https://example.com"};
    std::shared_ptr<Session> session = std::make_shared<Session>();
    session->SetUrl(url);

    MultiPerform multi;
    multi.AddSession(session);
    multi.AddInterceptor(std::make_shared<LoggingInterceptor>());

    std::vector<Response> response = multi.Get();
}
GloriousWizard commented 1 year ago

I get an error when I compile the example with cmake.

error: ‘class cpr::MultiPerform’ has no member named ‘front’
   28 |         std::cout << "Request url:  " << multi.front().GetFullRequestUrl() << '\n';
      |                                                ^~~~~1

This is how I included this PR.

FetchContent_Declare(
        cpr
        GIT_REPOSITORY https://github.com/libcpr/cpr.git
        GIT_TAG 155f3f44bb9add8a7e96a7b89e31c02771eb43a5
)
COM8 commented 1 year ago

Fixed the example code. It should be multi.GetSessions().front().first->GetFullRequestUrl();.

GloriousWizard commented 1 year ago

If more than one session is added to the multiPerform, only the first session gets printed out.

int main() {
    std::shared_ptr<cpr::Session> session_1 = std::make_shared<cpr::Session>();
    std::shared_ptr<cpr::Session> session_2 = std::make_shared<cpr::Session>();
    session_1->SetUrl("https://duckduckgo.com");
    session_2->SetUrl("https://google.com");

    cpr::MultiPerform multiPerform;
    multiPerform.AddInterceptor(std::make_shared<LoggingInterceptor>());
    multiPerform.AddSession(session_1);
    multiPerform.AddSession(session_2);

    std::vector<cpr::Response> responses = multiPerform.Get();
}

Output.

Request url:  https://duckduckgo.com
Response status code:  200
COM8 commented 1 year ago

This is expected since you print here only the first one:

std::cout << "Request url:  " << multi.GetSessions().front().first->GetFullRequestUrl(); << '\n';

You can iterate over multi.GetSessions() to print all.

GloriousWizard commented 1 year ago

You can iterate over multi.GetSessions() to print all.

You should put that in the documentation.

COM8 commented 1 year ago

Awesome! Then I will merge this PR as soon as I have time to update the docs accordingly.

COM8 commented 1 year ago

Docs are available here: https://github.com/libcpr/docs/pull/34