Skip to content

Commit

Permalink
Add generic request function in api.h
Browse files Browse the repository at this point in the history
Resolves libcpr#1117
  • Loading branch information
GitSparTV committed Sep 30, 2024
1 parent 225b745 commit 430ab6d
Showing 1 changed file with 35 additions and 0 deletions.
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

0 comments on commit 430ab6d

Please sign in to comment.