Audacity 3.2.0
IResponse.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file IResponse.h
6 @brief Declare an interface for HTTP response.
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11#pragma once
12
13#include <string>
14#include <cstdint>
15#include <vector>
16#include <functional>
17
18#include "NetworkManagerApi.h"
19
20namespace audacity
21{
22namespace network_manager
23{
24
25class Request;
26class HeadersList;
27class CookiesList;
28
29enum class NetworkError
30{
31 NoError,
32 BadURL,
37 Timeout,
45};
46
47
50{
51public:
52 using RequestCallback = std::function<void (IResponse*)>;
55 using ProgressCallback = std::function<void(int64_t current, int64_t expected)>;
56
57 virtual ~IResponse () = default;
58
59 virtual bool isFinished () const noexcept = 0;
60
61 virtual unsigned getHTTPCode () const noexcept = 0;
62
63 virtual NetworkError getError () const noexcept = 0;
64 virtual std::string getErrorString () const = 0;
65
66 virtual bool headersReceived () const noexcept = 0;
67
68 virtual bool hasHeader (const std::string& headerName) const noexcept = 0;
69 virtual std::string getHeader (const std::string& headerName) const = 0;
70
71 virtual const HeadersList& getHeaders () const noexcept = 0;
72 virtual const CookiesList& getCookies () const noexcept = 0;
73
74 virtual const Request& getRequest () const noexcept = 0;
75 virtual std::string getURL () const = 0;
76
77 virtual void abort () noexcept = 0;
78
79 virtual void setOnDataReceivedCallback (RequestCallback callback) = 0;
80 virtual void setRequestFinishedCallback (RequestCallback callback) = 0;
81
83 virtual void setDownloadProgressCallback(ProgressCallback callback) = 0;
85 virtual void setUploadProgressCallback(ProgressCallback callback) = 0;
86
87 // The total bytes available to read by readData
88 virtual uint64_t getBytesAvailable () const noexcept = 0;
89
90 // Reads at max maxBytesCount into the buffer, returns the actual count of bytes read
91 virtual uint64_t readData (void* buffer, uint64_t maxBytesCount) = 0;
92
93 template<typename RetVal = std::vector<uint8_t>>
94 RetVal readAll ()
95 {
96 RetVal result;
97
98 constexpr uint64_t bufferSize = 4 * 1024;
99 uint8_t buffer[bufferSize];
100
101 while (uint64_t bytesRead = readData (buffer, bufferSize))
102 {
103 using PtrType = typename RetVal::pointer;
104
105 PtrType begin = reinterpret_cast<PtrType>(buffer);
106 PtrType end = reinterpret_cast<PtrType>(buffer + bytesRead);
107
108 result.insert (result.end (), begin, end);
109
110 if (bytesRead < bufferSize)
111 break;
112 }
113
114 return result;
115 }
116};
117
118}
119}
Declare macros for the Network Manager library DLL API.
#define NETWORK_MANAGER_API
A class, representing a list of HTTP cookies.
A class, representing a list of HTTP headers.
Class to construct the HTTP request.
Interface, that provides access to the data from the HTTP response.
Definition: IResponse.h:50
std::function< void(int64_t current, int64_t expected)> ProgressCallback
Definition: IResponse.h:55
virtual bool isFinished() const noexcept=0
std::function< void(IResponse *)> RequestCallback
Definition: IResponse.h:52
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
auto begin(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:150
STL namespace.