C++ edjLibrary SDK
Loading...
Searching...
No Matches
fetch.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <vector>
5#include <map>
6#include <string>
7
8#include "http.hpp"
9#include "stream.hpp"
10#include "error.hpp"
11
12namespace edjx {
13
15namespace fetch {
16
28
35
42
53 edjx::error::StreamError read_body(std::vector<uint8_t> & result);
54
61 };
62
75 uint32_t sd;
76
78 FetchResponsePending(uint32_t sd) : sd(sd) {}
79
88 };
89
94 struct HttpFetch {
104 std::vector<uint8_t> body;
105
114 version(http::HttpVersion::HTTP_11),
115 uri(uri),
116 method(method) {}
117
125
132
143 const std::string & header_name,
144 const std::string & value
145 );
146
157 const std::string & header_name,
158 const std::vector<std::string> & value
159 );
160
171 const std::string & header_name,
172 const std::string & value
173 );
174
184 const edjx::http::HttpHeaders & value
185 );
186
193
202 HttpFetch & set_body(const std::string & text);
203
212 HttpFetch & set_body(const std::vector<std::uint8_t> bytes);
213
219 const std::vector<uint8_t> & get_body() const;
220
230
250 };
251
252}}
Class that represents a URL.
Definition: http.hpp:67
Read stream class.
Definition: stream.hpp:120
This class represents a write stream.
Definition: stream.hpp:68
StreamError
Enum that describes values returned by streaming functions.
Definition: error.hpp:243
HttpError
Enum describing return values of functions that correspond to HTTP modules.
Definition: error.hpp:14
HttpVersion
Enum containing an HTTP version.
Definition: http.hpp:18
uint16_t HttpStatusCode
HTTP status code (e.g., value 200 means OK, value 404 means Not Found)
Definition: http.hpp:60
std::map< std::string, std::vector< std::string >, CaseInsensitiveKeys > HttpHeaders
A map of HttpHeaders where keys are case-insensitive header names.
Definition: http.hpp:54
HttpMethod
Enum containing a number of common HTTP methods.
Definition: http.hpp:29
Definition: error.hpp:5
A pending Fetch response.
Definition: fetch.hpp:73
FetchResponsePending(uint32_t sd)
Definition: fetch.hpp:78
edjx::error::HttpError get_fetch_response(FetchResponse &result)
Get the server's response.
uint32_t sd
Stream descriptor.
Definition: fetch.hpp:75
FetchResponsePending()
Definition: fetch.hpp:77
Response for HTTP fetch request, which may include body, headers, and status code.
Definition: fetch.hpp:21
const edjx::http::HttpHeaders & get_headers() const
Returns HTTP headers of the fetch response.
edjx::http::HttpStatusCode status
HTTP status code of the response.
Definition: fetch.hpp:23
edjx::stream::ReadStream read_stream
Body of the response as a stream.
Definition: fetch.hpp:27
edjx::http::HttpHeaders headers
HTTP headers of the response.
Definition: fetch.hpp:25
edjx::http::HttpStatusCode get_status_code() const
Returns the HTTP status code of the fetch response.
edjx::error::StreamError read_body(std::vector< uint8_t > &result)
Reads and returns the HTTP body of the fetch response.
edjx::stream::ReadStream & get_read_stream()
Returns the HTTP body of the fetch response as a read stream.
An HTTP fetch request includes body, headers, version, method, and URI.
Definition: fetch.hpp:94
edjx::error::HttpError send(FetchResponse &response)
Sends the request to the server, and returns after the response is received or an error occurs.
HttpFetch & set_headers(const edjx::http::HttpHeaders &value)
Set request headers to the given values.
HttpFetch & set_header(const std::string &header_name, const std::vector< std::string > &value)
Sets a request header to the given values.
const edjx::http::HttpHeaders & get_headers() const
Returns a const reference to the request header map.
HttpFetch & set_body(const std::string &text)
Sets the given [text] value as the body of the request.
HttpFetch & set_body(const std::vector< std::uint8_t > bytes)
Sets the given [bytes] value as the body of the request.
http::HttpVersion get_version() const
Gets the HTTP version of the request.
HttpFetch(http::Uri uri, http::HttpMethod method)
Creates a request with the given method and URI, version as HTTP/1.1, no headers, and no body.
Definition: fetch.hpp:113
HttpFetch & set_version(http::HttpVersion version)
Sets the HTTP version of the request.
http::HttpMethod method
Method for the request.
Definition: fetch.hpp:102
http::HttpVersion version
HTTP version of the request.
Definition: fetch.hpp:96
const std::vector< uint8_t > & get_body() const
Returns a constant reference to the body.
edjx::http::HttpHeaders headers
Headers for the request.
Definition: fetch.hpp:98
http::Uri uri
URI of the request.
Definition: fetch.hpp:100
HttpFetch & append_header(const std::string &header_name, const std::string &value)
Appends a value to the existing request header.
std::vector< uint8_t > body
Request body.
Definition: fetch.hpp:104
HttpFetch & set_header(const std::string &header_name, const std::string &value)
Sets a request header to the given value.
edjx::error::HttpError send_streaming(FetchResponsePending &response, edjx::stream::WriteStream &write_stream)
Starts streaming an HTTP Fetch request to the server. This method returns a FetchResponsePending obje...