Audacity 3.2.0
NetworkUtils.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 NetworkUtils.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
11
12#include "NetworkUtils.h"
13
14#include <algorithm>
15
16#include "OAuthService.h"
17#include "ServiceConfig.h"
18
19#include "IResponse.h"
20#include "Request.h"
21
22#include "TranslatableString.h"
23
25{
26using namespace audacity::network_manager;
27
28namespace
29{
31{
34
35 if (code == HttpCode::PaymentRequired)
37
38 if (code == HttpCode::Unauthorized)
40
41 if (code == HttpCode::Forbidden)
43
44 if (code == HttpCode::NotFound)
46
47 if (code == HttpCode::PayloadTooLarge)
49
50 if (code == HttpCode::Gone)
52
53 if (code > 500)
55
57}
58
60{
61 const auto code = response.getError();
62
63 if (code == NetworkError::NoError)
65
66 if (code == NetworkError::OperationCancelled)
68
69 if (
70 code == NetworkError::ConnectionFailed ||
71 code == NetworkError::ConnectionRefused ||
72 code == NetworkError::HostNotFound ||
73 code == NetworkError::ProxyConnectionFailed ||
74 code == NetworkError::ProxyNotFound ||
75 code == NetworkError::RemoteHostClosed ||
78
79 if (code == NetworkError::HTTPError)
80 return GetResultCodeFromHttpCode(response.getHTTPCode());
81
83}
84} // namespace
85
87{
88 const auto resultCode = GuessResultCode(response);
89
90 if (resultCode == SyncResultCode::Success)
91 return { resultCode,
92 readBody ? response.readAll<std::string>() : std::string {} };
93
94 if (response.getError() != NetworkError::HTTPError)
95 return { resultCode, response.getErrorString() };
96
97 return { resultCode,
98 std::string("HTTP ") + std::to_string(response.getHTTPCode()) +
99 std::string("\n") + response.readAll<std::string>() };
100}
101
103{
104 const auto language = GetServiceConfig().GetAcceptLanguageValue();
105
106 if (!language.empty())
107 request.setHeader(
109
110 auto& oauthService = GetOAuthService();
111
112 if (oauthService.HasAccessToken())
113 request.setHeader(
114 common_headers::Authorization, oauthService.GetAccessToken());
115}
116
118{
119 return code == SyncResultCode::Cancelled ||
126}
127
129{
130 BytesTransferred = bytesTransferred;
131 return *this;
132}
133
135{
136 BlocksTransferred = blocksTransferred;
137 return *this;
138}
139
141TransferStats::SetProjectFilesTransferred(int64_t projectFilesTransferred)
142{
143 ProjectFilesTransferred = projectFilesTransferred;
144 return *this;
145}
146
148{
149 TransferDuration = transferDuration;
150 return *this;
151}
152
153} // namespace audacity::cloud::audiocom
Declare an interface for HTTP response.
Declare a class for constructing HTTP requests.
std::string GetAcceptLanguageValue() const
Returns the preferred language.
Interface, that provides access to the data from the HTTP response.
Definition: IResponse.h:113
virtual std::string getErrorString() const =0
virtual unsigned getHTTPCode() const noexcept=0
virtual NetworkError getError() const noexcept=0
Request & setHeader(const std::string &name, std::string value)
Definition: Request.cpp:46
SyncResultCode GuessResultCode(IResponse &response) noexcept
bool IsUploadRecoverable(SyncResultCode code)
void SetCommonHeaders(Request &request)
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
ResponseResult GetResponseResult(IResponse &response, bool readBody)
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.
TransferStats & SetBytesTransferred(int64_t bytesTransferred)
TransferStats & SetProjectFilesTransferred(int64_t projectFilesTransferred)
TransferStats & SetBlocksTransferred(int64_t blocksTransferred)
TransferStats & SetTransferDuration(Duration transferDuration)
std::chrono::milliseconds Duration
Definition: NetworkUtils.h:27