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

Add generic request function in api.h #1118

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions include/cpr/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ namespace cpr {

using AsyncResponse = AsyncWrapper<Response>;

enum class HTTPMethod {
GET,
HEAD,
POST,
PUT,
DELETE,
OPTIONS,
PATCH,
};

namespace priv {

template <bool processed_header, typename CurrentType>
Expand Down Expand Up @@ -386,6 +396,31 @@ std::vector<AsyncWrapper<Response, true>> MultiPutAsync(Ts&&... ts) {
return ret;
}

template <HTTPMethod method, typename... Ts>
Response Request(Ts&&... ts) {
cpr::Session session;
cpr::priv::set_option(session, std::forward<Ts>(ts)...);

if constexpr (method == HTTPMethod::GET) {
return session.Get();
} else if constexpr (method == HTTPMethod::HEAD) {
return session.Head();
} else if constexpr (method == HTTPMethod::POST) {
return session.Post();
} else if constexpr (method == HTTPMethod::PUT) {
return session.Put();
} else if constexpr (method == HTTPMethod::DELETE) {
return session.Delete();
} else if constexpr (method == HTTPMethod::OPTIONS) {
return session.Options();
} else if constexpr (method == HTTPMethod::PATCH) {
return session.Patch();
} else {
// Must be template dependent until DR 2518 (C++23)
static_assert(method == HTTPMethod::GET, "Unknown method");
}
}


} // namespace cpr

Expand Down
Loading