kcurl v0.1.10
Basic C++23 wrapper over libcurl
 
Loading...
Searching...
No Matches
http.hpp
Go to the documentation of this file.
1#pragma once
2#include "kcurl/easy.hpp"
4#include <string_view>
5
6namespace kcurl::http {
7enum class Verb : std::int8_t { Get, Post };
8
9struct Query {
10 std::string key{};
11 std::string value{};
12};
13
15struct Error {
19 [[nodiscard]] static auto from_response(Status status, std::string_view error_text) -> Error;
20
23 std::string text{};
24};
25
27struct Request {
29
31 std::string base_url{};
32
34 std::string user_agent{};
37 std::vector<Query> queries{};
40 std::vector<Query> headers{};
45};
46
50template <typename Type>
51struct Response;
52
53template <>
54struct Response<void> {
56 [[nodiscard]] auto rewrap_as_error(std::string_view const error_text) const -> Error {
57 return Error::from_response(status, error_text);
58 }
59
61};
62
63template <typename Type>
64struct Response {
66 template <typename T>
67 [[nodiscard]] auto rewrap_as(T payload) const -> Response<T> {
68 return Response<T>{.payload = std::move(payload), .status = status};
69 }
70
72 [[nodiscard]] auto rewrap_as_void() const -> Response<void> { return Response<void>{.status = status}; }
73
75 [[nodiscard]] auto rewrap_as_error(std::string_view const error_text) const -> Error {
76 return Error::from_response(status, error_text);
77 }
78
79 Type payload{};
81};
82
86template <typename Type>
87using Result = std::expected<Response<Type>, Error>;
88
92[[nodiscard]] auto escape(std::string_view text) -> std::string;
96[[nodiscard]] auto unescape(std::string_view escaped) -> std::string;
97
100[[nodiscard]] auto to_easy_request(Request request) -> easy::Request;
101
109[[nodiscard]] auto perform(easy::Request const& request) -> Result<ByteArray>;
110
114[[nodiscard]] inline auto fetch(Request request) -> Result<ByteArray> {
115 return http::perform(to_easy_request(std::move(request)));
116}
117} // namespace kcurl::http
Wrapper over a response status code.
Definition http_status.hpp:7
Definition http.hpp:6
auto to_easy_request(Request request) -> easy::Request
auto perform(easy::Request const &request) -> Result< ByteArray >
Perform easy::Request and interpret the easy::Result as an http::Result. This lower-level function is...
auto fetch(Request request) -> Result< ByteArray >
Primary http API.
Definition http.hpp:114
auto unescape(std::string_view escaped) -> std::string
Replace URL escapes with their source characters.
std::expected< Response< Type >, Error > Result
Result of a fetch operation. Result is a class template to enable user-side reuse for custom Reponse ...
Definition http.hpp:87
Verb
Definition http.hpp:7
auto escape(std::string_view text) -> std::string
Replace special characters to be URL-friendly.
CurlCode
Definition curl_code.hpp:6
Input parameter for perform().
Definition easy.hpp:18
Flag
Definition easy.hpp:19
Error of a fetch operation.
Definition http.hpp:15
Status status
Definition http.hpp:22
CurlCode curl_code
Definition http.hpp:21
std::string text
Definition http.hpp:23
static auto from_response(Status status, std::string_view error_text) -> Error
Definition http.hpp:9
std::string value
Definition http.hpp:11
std::string key
Definition http.hpp:10
Input parameter for fetch().
Definition http.hpp:27
Verb verb
Request method.
Definition http.hpp:42
std::vector< Query > headers
List of HTTP header queries, if any. Suffix a key with ':' to remove that default header.
Definition http.hpp:40
std::string base_url
URL to fetch. Must be a valid URL.
Definition http.hpp:31
Flag flags
Request flags.
Definition http.hpp:44
std::vector< Query > queries
List of HTTP queries, if any. Appended to base_url if verb == Verb::Get, else added as post fields.
Definition http.hpp:37
std::string user_agent
User agent to use, if any.
Definition http.hpp:34
auto rewrap_as_error(std::string_view const error_text) const -> Error
Definition http.hpp:56
Successful response of a fetch operation. Response is a class template to enable user-side reuse for ...
Definition http.hpp:64
auto rewrap_as_error(std::string_view const error_text) const -> Error
Definition http.hpp:75
Type payload
Definition http.hpp:79
Status status
Definition http.hpp:80
auto rewrap_as(T payload) const -> Response< T >
Definition http.hpp:67
auto rewrap_as_void() const -> Response< void >
Definition http.hpp:72