#ifndef CPR_API_H #define CPR_API_H #include #include #include #include #include #include "cpr/async.h" #include "cpr/async_wrapper.h" #include "cpr/auth.h" #include "cpr/bearer.h" #include "cpr/cprtypes.h" #include "cpr/multipart.h" #include "cpr/multiperform.h" #include "cpr/payload.h" #include "cpr/response.h" #include "cpr/session.h" #include namespace cpr { using AsyncResponse = AsyncWrapper; namespace priv { template void set_option_internal(Session& session, CurrentType&& current_option) { session.SetOption(std::forward(current_option)); } template <> inline void set_option_internal(Session& session, Header&& current_option) { // Header option was already provided -> Update previous header session.UpdateHeader(std::forward
(current_option)); } template void set_option_internal(Session& session, CurrentType&& current_option, Ts&&... ts) { set_option_internal(session, std::forward(current_option)); if (std::is_same::value) { set_option_internal(session, std::forward(ts)...); } else { set_option_internal(session, std::forward(ts)...); } } template void set_option(Session& session, Ts&&... ts) { set_option_internal(session, std::forward(ts)...); } // Idea: https://stackoverflow.com/a/19060157 template void apply_set_option_internal(Session& session, Tuple&& t, std::index_sequence) { set_option(session, std::get(std::forward(t))...); } // Idea: https://stackoverflow.com/a/19060157 template void apply_set_option(Session& session, Tuple&& t) { using Indices = std::make_index_sequence>::value>; apply_set_option_internal(session, std::forward(t), Indices()); } template void setup_multiperform_internal(MultiPerform& multiperform, T&& t) { std::shared_ptr session = std::make_shared(); apply_set_option(*session, t); multiperform.AddSession(session); } template void setup_multiperform_internal(MultiPerform& multiperform, T&& t, Ts&&... ts) { std::shared_ptr session = std::make_shared(); apply_set_option(*session, t); multiperform.AddSession(session); setup_multiperform_internal(multiperform, std::forward(ts)...); } template void setup_multiperform(MultiPerform& multiperform, Ts&&... ts) { setup_multiperform_internal(multiperform, std::forward(ts)...); } using session_action_t = cpr::Response (cpr::Session::*)(); template void setup_multiasync(std::vector>& responses, T&& parameters) { std::shared_ptr cancellation_state = std::make_shared(false); std::function execFn{[cancellation_state](T params) { if (cancellation_state->load()) { return Response{}; } cpr::Session s{}; s.SetCancellationParam(cancellation_state); apply_set_option(s, std::forward(params)); return std::invoke(SessionAction, s); }}; responses.emplace_back(GlobalThreadPool::GetInstance()->Submit(std::move(execFn), std::forward(parameters)), std::move(cancellation_state)); } template void setup_multiasync(std::vector>& responses, T&& head, Ts&&... tail) { setup_multiasync(responses, std::forward(head)); if constexpr (sizeof...(Ts) > 0) { setup_multiasync(responses, std::forward(tail)...); } } } // namespace priv // Get methods template Response Get(Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Get(); } // Get async methods template AsyncResponse GetAsync(Ts... ts) { return cpr::async([](Ts... ts_inner) { return Get(std::move(ts_inner)...); }, std::move(ts)...); } // Get callback methods template // NOLINTNEXTLINE(fuchsia-trailing-return) auto GetCallback(Then then, Ts... ts) { return cpr::async([](Then then_inner, Ts... ts_inner) { return then_inner(Get(std::move(ts_inner)...)); }, std::move(then), std::move(ts)...); } // Post methods template Response Post(Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Post(); } // Post async methods template AsyncResponse PostAsync(Ts... ts) { return cpr::async([](Ts... ts_inner) { return Post(std::move(ts_inner)...); }, std::move(ts)...); } // Post callback methods template // NOLINTNEXTLINE(fuchsia-trailing-return) auto PostCallback(Then then, Ts... ts) { return cpr::async([](Then then_inner, Ts... ts_inner) { return then_inner(Post(std::move(ts_inner)...)); }, std::move(then), std::move(ts)...); } // Put methods template Response Put(Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Put(); } // Put async methods template AsyncResponse PutAsync(Ts... ts) { return cpr::async([](Ts... ts_inner) { return Put(std::move(ts_inner)...); }, std::move(ts)...); } // Put callback methods template // NOLINTNEXTLINE(fuchsia-trailing-return) auto PutCallback(Then then, Ts... ts) { return cpr::async([](Then then_inner, Ts... ts_inner) { return then_inner(Put(std::move(ts_inner)...)); }, std::move(then), std::move(ts)...); } // Head methods template Response Head(Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Head(); } // Head async methods template AsyncResponse HeadAsync(Ts... ts) { return cpr::async([](Ts... ts_inner) { return Head(std::move(ts_inner)...); }, std::move(ts)...); } // Head callback methods template // NOLINTNEXTLINE(fuchsia-trailing-return) auto HeadCallback(Then then, Ts... ts) { return cpr::async([](Then then_inner, Ts... ts_inner) { return then_inner(Head(std::move(ts_inner)...)); }, std::move(then), std::move(ts)...); } // Delete methods template Response Delete(Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Delete(); } // Delete async methods template AsyncResponse DeleteAsync(Ts... ts) { return cpr::async([](Ts... ts_inner) { return Delete(std::move(ts_inner)...); }, std::move(ts)...); } // Delete callback methods template // NOLINTNEXTLINE(fuchsia-trailing-return) auto DeleteCallback(Then then, Ts... ts) { return cpr::async([](Then then_inner, Ts... ts_inner) { return then_inner(Delete(std::move(ts_inner)...)); }, std::move(then), std::move(ts)...); } // Options methods template Response Options(Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Options(); } // Options async methods template AsyncResponse OptionsAsync(Ts... ts) { return cpr::async([](Ts... ts_inner) { return Options(std::move(ts_inner)...); }, std::move(ts)...); } // Options callback methods template // NOLINTNEXTLINE(fuchsia-trailing-return) auto OptionsCallback(Then then, Ts... ts) { return cpr::async([](Then then_inner, Ts... ts_inner) { return then_inner(Options(std::move(ts_inner)...)); }, std::move(then), std::move(ts)...); } // Patch methods template Response Patch(Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Patch(); } // Patch async methods template AsyncResponse PatchAsync(Ts... ts) { return cpr::async([](Ts... ts_inner) { return Patch(std::move(ts_inner)...); }, std::move(ts)...); } // Patch callback methods template // NOLINTNEXTLINE(fuchsia-trailing-return) auto PatchCallback(Then then, Ts... ts) { return cpr::async([](Then then_inner, Ts... ts_inner) { return then_inner(Patch(std::move(ts_inner)...)); }, std::move(then), std::move(ts)...); } // Download methods template Response Download(std::ofstream& file, Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Download(file); } // Download async method template AsyncResponse DownloadAsync(fs::path local_path, Ts... ts) { return AsyncWrapper{std::async( std::launch::async, [](fs::path local_path_, Ts... ts_) { std::ofstream f(local_path_.c_str()); return Download(f, std::move(ts_)...); }, std::move(local_path), std::move(ts)...)}; } // Download with user callback template Response Download(const WriteCallback& write, Ts&&... ts) { Session session; priv::set_option(session, std::forward(ts)...); return session.Download(write); } // Multi requests template std::vector MultiGet(Ts&&... ts) { MultiPerform multiperform; priv::setup_multiperform(multiperform, std::forward(ts)...); return multiperform.Get(); } template std::vector MultiDelete(Ts&&... ts) { MultiPerform multiperform; priv::setup_multiperform(multiperform, std::forward(ts)...); return multiperform.Delete(); } template std::vector MultiPut(Ts&&... ts) { MultiPerform multiperform; priv::setup_multiperform(multiperform, std::forward(ts)...); return multiperform.Put(); } template std::vector MultiHead(Ts&&... ts) { MultiPerform multiperform; priv::setup_multiperform(multiperform, std::forward(ts)...); return multiperform.Head(); } template std::vector MultiOptions(Ts&&... ts) { MultiPerform multiperform; priv::setup_multiperform(multiperform, std::forward(ts)...); return multiperform.Options(); } template std::vector MultiPatch(Ts&&... ts) { MultiPerform multiperform; priv::setup_multiperform(multiperform, std::forward(ts)...); return multiperform.Patch(); } template std::vector MultiPost(Ts&&... ts) { MultiPerform multiperform; priv::setup_multiperform(multiperform, std::forward(ts)...); return multiperform.Post(); } template std::vector> MultiGetAsync(Ts&&... ts) { std::vector> ret{}; priv::setup_multiasync<&cpr::Session::Get>(ret, std::forward(ts)...); return ret; } template std::vector> MultiDeleteAsync(Ts&&... ts) { std::vector> ret{}; priv::setup_multiasync<&cpr::Session::Delete>(ret, std::forward(ts)...); return ret; } template std::vector> MultiHeadAsync(Ts&&... ts) { std::vector> ret{}; priv::setup_multiasync<&cpr::Session::Head>(ret, std::forward(ts)...); return ret; } template std::vector> MultiOptionsAsync(Ts&&... ts) { std::vector> ret{}; priv::setup_multiasync<&cpr::Session::Options>(ret, std::forward(ts)...); return ret; } template std::vector> MultiPatchAsync(Ts&&... ts) { std::vector> ret{}; priv::setup_multiasync<&cpr::Session::Patch>(ret, std::forward(ts)...); return ret; } template std::vector> MultiPostAsync(Ts&&... ts) { std::vector> ret{}; priv::setup_multiasync<&cpr::Session::Post>(ret, std::forward(ts)...); return ret; } template std::vector> MultiPutAsync(Ts&&... ts) { std::vector> ret{}; priv::setup_multiasync<&cpr::Session::Put>(ret, std::forward(ts)...); return ret; } } // namespace cpr #endif