Sane C++ Libraries
C++ Platform Abstraction Libraries
HttpClient.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Async/Async.h"
5#include "../Containers/SmallVector.h"
6#include "../Strings/String.h"
7namespace SC
8{
10struct HttpClient;
11} // namespace SC
12
15
18
21{
26 [[nodiscard]] Result get(AsyncEventLoop& loop, StringView url);
27
29
31 [[nodiscard]] StringView getResponse() const;
32
33 [[nodiscard]] Result setCustomDebugName(const StringView debugName)
34 {
35 return Result(customDebugName.assign(debugName));
36 }
37
38 private:
39 void onConnected(AsyncSocketConnect::Result& result);
40 void onAfterSend(AsyncSocketSend::Result& result);
41 void onAfterRead(AsyncSocketReceive::Result& result);
42
44
45 String customDebugName;
46
47 // TODO: can we find a way to put all async requests in a single tagged union when they're not used in parallel?
48 AsyncSocketConnect connectAsync;
49 AsyncSocketSend sendAsync;
50 AsyncSocketReceive receiveAsync;
51 SocketDescriptor clientSocket;
52 AsyncEventLoop* eventLoop = nullptr;
53};
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition: Async.h:933
Helper holding CompletionData for a specific AsyncRequest-derived class.
Definition: Async.h:246
Starts a socket connect operation, connecting to a remote endpoint.
Definition: Async.h:501
Callback result for AsyncSocketReceive.
Definition: Async.h:607
Starts a socket receive operation, receiving bytes from a remote endpoint.
Definition: Async.h:595
Starts a socket send operation, sending bytes to a remote endpoint.
Definition: Async.h:539
Wraps function pointers, member functions and lambdas without ever allocating.
Definition: Function.h:50
Http async client.
Definition: HttpClient.h:21
Result get(AsyncEventLoop &loop, StringView url)
Setups this client to execute a GET request on the given url.
Delegate< HttpClient & > callback
The callback that is called after GET operation succeeded.
Definition: HttpClient.h:28
StringView getResponse() const
Get the response StringView sent by the server.
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
A Vector that can hold up to N elements inline and > N on heap.
Definition: SmallVector.h:27
Low-level OS socket handle.
Definition: SocketDescriptor.h:154
A non-modifiable owning string with associated encoding.
Definition: String.h:30
bool assign(StringView sv)
Assigns a StringView to this String, replacing existing contents.
Definition: String.h:175
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47