mirror of
https://github.com/stenzek/duckstation.git
synced 2026-07-11 01:24:11 +02:00
Qt: Add AsyncHTTPRequest class
This commit is contained in:
@@ -23,6 +23,8 @@ set(SRCS
|
||||
advancedsettingswidget.cpp
|
||||
advancedsettingswidget.h
|
||||
advancedsettingswidget.ui
|
||||
asynchttprequest.cpp
|
||||
asynchttprequest.h
|
||||
asyncpixmaploader.cpp
|
||||
asyncpixmaploader.h
|
||||
audiosettingswidget.cpp
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
// SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#include "asynchttprequest.h"
|
||||
|
||||
#include "core/host.h"
|
||||
|
||||
#include "util/http_cache.h"
|
||||
#include "util/http_downloader.h"
|
||||
|
||||
#include "common/error.h"
|
||||
#include "common/log.h"
|
||||
|
||||
#include "moc_asynchttprequest.cpp"
|
||||
|
||||
LOG_CHANNEL(Host);
|
||||
|
||||
AsyncHTTPRequest::AsyncHTTPRequest() : QObject()
|
||||
{
|
||||
}
|
||||
|
||||
AsyncHTTPRequest::~AsyncHTTPRequest() = default;
|
||||
|
||||
void AsyncHTTPRequest::get(std::string url, const void* owner, ProgressCallback* progress /* = nullptr */,
|
||||
HTTPDownloader::HeaderList additional_headers /* = */,
|
||||
std::optional<u16> timeout_seconds /* = */)
|
||||
{
|
||||
HTTPDownloader* const downloader = HTTPCache::GetDownloader(&m_error);
|
||||
if (!downloader)
|
||||
{
|
||||
emit requestComplete(HTTPDownloader::HTTP_STATUS_ERROR, m_error, m_content_type, m_data);
|
||||
deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
downloader->CreateRequest(
|
||||
std::move(url), owner,
|
||||
[this](s32 status_code, Error& error, std::string& content_type, HTTPDownloader::Request::Data& data) {
|
||||
m_status_code = status_code;
|
||||
m_error = std::move(error);
|
||||
m_content_type = std::move(content_type);
|
||||
m_data = std::move(data);
|
||||
QMetaObject::invokeMethod(this, &AsyncHTTPRequest::finishRequest, Qt::QueuedConnection);
|
||||
},
|
||||
progress, additional_headers, timeout_seconds);
|
||||
}
|
||||
|
||||
void AsyncHTTPRequest::post(std::string url, std::string post_data, const void* owner,
|
||||
ProgressCallback* progress /* = nullptr */,
|
||||
HTTPDownloader::HeaderList additional_headers /* = */,
|
||||
std::optional<u16> timeout_seconds /* = */)
|
||||
{
|
||||
HTTPDownloader* const downloader = HTTPCache::GetDownloader(&m_error);
|
||||
if (!downloader)
|
||||
{
|
||||
emit requestComplete(HTTPDownloader::HTTP_STATUS_ERROR, m_error, m_content_type, m_data);
|
||||
deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
downloader->CreatePostRequest(
|
||||
std::move(url), std::move(post_data), owner,
|
||||
[this](s32 status_code, Error& error, std::string& content_type, HTTPDownloader::Request::Data& data) {
|
||||
m_status_code = status_code;
|
||||
m_error = std::move(error);
|
||||
m_content_type = std::move(content_type);
|
||||
m_data = std::move(data);
|
||||
QMetaObject::invokeMethod(this, &AsyncHTTPRequest::finishRequest, Qt::QueuedConnection);
|
||||
},
|
||||
progress, additional_headers, timeout_seconds);
|
||||
}
|
||||
|
||||
void AsyncHTTPRequest::finishRequest()
|
||||
{
|
||||
emit requestComplete(m_status_code, m_error, m_content_type, m_data);
|
||||
deleteLater();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util/http_downloader.h"
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
class QPixmap;
|
||||
|
||||
class AsyncHTTPRequest final : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
AsyncHTTPRequest();
|
||||
~AsyncHTTPRequest() override;
|
||||
|
||||
void get(std::string url, const void* owner, ProgressCallback* progress = nullptr,
|
||||
HTTPDownloader::HeaderList additional_headers = {}, std::optional<u16> timeout_seconds = {});
|
||||
void post(std::string url, std::string post_data, const void* owner, ProgressCallback* progress = nullptr,
|
||||
HTTPDownloader::HeaderList additional_headers = {}, std::optional<u16> timeout_seconds = {});
|
||||
|
||||
Q_SIGNALS:
|
||||
void requestComplete(qint32 status_code, Error& error_message, std::string& content_type,
|
||||
HTTPDownloader::Request::Data& data);
|
||||
|
||||
private:
|
||||
void finishRequest();
|
||||
|
||||
s32 m_status_code = HTTPDownloader::HTTP_STATUS_ERROR;
|
||||
Error m_error;
|
||||
std::string m_content_type;
|
||||
HTTPDownloader::Request::Data m_data;
|
||||
};
|
||||
@@ -6,6 +6,7 @@
|
||||
<ClCompile Include="achievementsettingswidget.cpp" />
|
||||
<ClCompile Include="achievementlogindialog.cpp" />
|
||||
<ClCompile Include="advancedsettingswidget.cpp" />
|
||||
<ClCompile Include="asynchttprequest.cpp" />
|
||||
<ClCompile Include="asyncpixmaploader.cpp" />
|
||||
<ClCompile Include="audiosettingswidget.cpp" />
|
||||
<ClCompile Include="autoupdaterdialog.cpp" />
|
||||
@@ -84,6 +85,7 @@
|
||||
<QtMoc Include="colorpickerbutton.h" />
|
||||
<QtMoc Include="capturesettingswidget.h" />
|
||||
<QtMoc Include="asyncpixmaploader.h" />
|
||||
<QtMoc Include="asynchttprequest.h" />
|
||||
<ClInclude Include="controllersettingwidgetbinder.h" />
|
||||
<QtMoc Include="memoryeditorwindow.h" />
|
||||
<QtMoc Include="memoryviewwidget.h" />
|
||||
|
||||
@@ -53,6 +53,7 @@
|
||||
<ClCompile Include="capturesettingswidget.cpp" />
|
||||
<ClCompile Include="asyncpixmaploader.cpp" />
|
||||
<ClCompile Include="debuggingsettingswidget.cpp" />
|
||||
<ClCompile Include="asynchttprequest.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="qtutils.h" />
|
||||
@@ -118,6 +119,7 @@
|
||||
<QtMoc Include="capturesettingswidget.h" />
|
||||
<QtMoc Include="asyncpixmaploader.h" />
|
||||
<QtMoc Include="debuggingsettingswidget.h" />
|
||||
<QtMoc Include="asynchttprequest.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<QtUi Include="consolesettingswidget.ui" />
|
||||
|
||||
Reference in New Issue
Block a user