| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <chrono> |
| 7 |
#include <functional> |
| 8 |
#include <map> |
| 9 |
#include <memory> |
| 10 |
#include <optional> |
| 11 |
#include <span> |
| 12 |
#include <string> |
| 13 |
#include <string_view> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include "Common/CommonTypes.h" |
| 17 |
|
| 18 |
namespace Common |
| 19 |
{ |
| 20 |
class HttpRequest final |
| 21 |
{ |
| 22 |
public: |
| 23 |
enum class AllowedReturnCodes : u8 |
| 24 |
{ |
| 25 |
Ok_Only, |
| 26 |
All |
| 27 |
}; |
| 28 |
|
| 29 |
|
| 30 |
using ProgressCallback = std::function<bool(s64 dltotal, s64 dlnow, s64 ultotal, s64 ulnow)>; |
| 31 |
|
| 32 |
explicit HttpRequest(std::chrono::milliseconds timeout_ms = std::chrono::milliseconds{3000}, |
| 33 |
ProgressCallback callback = nullptr); |
| 34 |
~HttpRequest(); |
| 35 |
bool IsValid() const; |
| 36 |
|
| 37 |
using Response = std::optional<std::vector<u8>>; |
| 38 |
using Headers = std::map<std::string, std::optional<std::string>>; |
| 39 |
|
| 40 |
struct Multiform |
| 41 |
{ |
| 42 |
std::string name; |
| 43 |
std::string data; |
| 44 |
}; |
| 45 |
|
| 46 |
void SetCookies(const std::string& cookies); |
| 47 |
void UseIPv4(); |
| 48 |
void FollowRedirects(long max = 1); |
| 49 |
s32 GetLastResponseCode() const; |
| 50 |
std::string EscapeComponent(const std::string& string); |
| 51 |
std::string GetHeaderValue(std::string_view name) const; |
| 52 |
Response Get(const std::string& url, const Headers& headers = {}, |
| 53 |
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only); |
| 54 |
Response Post(const std::string& url, std::span<const u8> payload, const Headers& headers = {}, |
| 55 |
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only); |
| 56 |
Response Post(const std::string& url, std::string_view payload, const Headers& headers = {}, |
| 57 |
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only); |
| 58 |
|
| 59 |
Response PostMultiform(const std::string& url, std::span<Multiform> multiform, |
| 60 |
const Headers& headers = {}, |
| 61 |
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only); |
| 62 |
|
| 63 |
private: |
| 64 |
class Impl; |
| 65 |
std::unique_ptr<Impl> m_impl; |
| 66 |
}; |
| 67 |
} |