Skip to content

Commit

Permalink
feat: Create dedicated client for HTTP requests
Browse files Browse the repository at this point in the history
使用单独的client发送http请求, 避免请求结果混淆

Log:
  • Loading branch information
myml authored and black-desk committed Jun 14, 2024
1 parent 76c9989 commit bf8489c
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 177 deletions.
14 changes: 5 additions & 9 deletions apps/ll-builder/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "linglong/builder/config.h"
#include "linglong/builder/linglong_builder.h"
#include "linglong/package/architecture.h"
#include "linglong/repo/client_factory.h"
#include "linglong/repo/config.h"
#include "linglong/utils/command/env.h"
#include "linglong/utils/configure.h"
Expand Down Expand Up @@ -400,15 +401,10 @@ int main(int argc, char **argv)
qCritical() << repoCfg.error();
return -1;
}

QNetworkAccessManager networkAccessManager;

linglong::api::client::ClientApi api;
api.setTimeOut(10 * 60 * 1000);
api.setNetworkAccessManager(&networkAccessManager);
api.setNewServerForAllOperations(QString::fromStdString(repoCfg->repos[repoCfg->defaultRepo]));

linglong::repo::OSTreeRepo repo(QString::fromStdString(builderCfg->repo), *repoCfg, api);
linglong::repo::ClientFactory clientFactory(repoCfg->repos[repoCfg->defaultRepo]);
linglong::repo::OSTreeRepo repo(QString::fromStdString(builderCfg->repo),
*repoCfg,
clientFactory);

auto containerBuidler = new linglong::runtime::ContainerBuilder(**ociRuntime);
containerBuidler->setParent(QCoreApplication::instance());
Expand Down
4 changes: 2 additions & 2 deletions apps/ll-cli/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ int main(int argc, char **argv)
printer = std::make_unique<Printer>();
}

linglong::api::client::ClientApi api;
auto config = linglong::repo::loadConfig(
{ LINGLONG_ROOT "/config.yaml", LINGLONG_DATA_DIR "/config.yaml" });
if (!config) {
qCritical() << config.error();
QCoreApplication::exit(-1);
return;
}
auto *repo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, api);
linglong::repo::ClientFactory clientFactory(config->repos[config->defaultRepo]);
auto *repo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, clientFactory);
repo->setParent(QCoreApplication::instance());

auto ociRuntimeCLI = qgetenv("LINGLONG_OCI_RUNTIME");
Expand Down
17 changes: 4 additions & 13 deletions apps/ll-package-manager/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ void withDBusDaemon()
return;
}

auto api = new linglong::api::client::ClientApi;
api->setTimeOut(5000);
api->setNewServerForAllOperations(
QString::fromStdString(config->repos.at(config->defaultRepo)));
api->setParent(QCoreApplication::instance());

auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, *api);
linglong::repo::ClientFactory clientFactory(config->repos[config->defaultRepo]);
auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, clientFactory);
ostreeRepo->setParent(QCoreApplication::instance());

auto packageManager =
Expand Down Expand Up @@ -82,12 +77,8 @@ void withoutDBusDaemon()
return;
}

auto api = new linglong::api::client::ClientApi;
api->setParent(QCoreApplication::instance());
api->setNewServerForAllOperations(
QString::fromStdString(config->repos.at(config->defaultRepo)));

auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, *api);
linglong::repo::ClientFactory clientFactory(config->repos[config->defaultRepo]);
auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, clientFactory);
ostreeRepo->setParent(QCoreApplication::instance());

auto packageManager =
Expand Down
2 changes: 2 additions & 0 deletions libs/linglong/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pfl_add_library(
src/linglong/package/version_range.h
src/linglong/repo/config.cpp
src/linglong/repo/config.h
src/linglong/repo/client_factory.h
src/linglong/repo/client_factory.cpp
src/linglong/repo/ostree_repo.cpp
src/linglong/repo/ostree_repo.h
src/linglong/runtime/container_builder.cpp
Expand Down
35 changes: 35 additions & 0 deletions libs/linglong/src/linglong/repo/client_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/

#include "client_factory.h"

namespace linglong::repo {

ClientFactory::ClientFactory(const QString &server)
: m_server(server)
{
}

ClientFactory::ClientFactory(const std::string &server)
: m_server(QString::fromStdString(server))
{
}

QSharedPointer<api::client::ClientApi> ClientFactory::createClient() const
{
auto api = QSharedPointer<linglong::api::client::ClientApi>::create();
api->setTimeOut(5000);
api->setNewServerForAllOperations(m_server);
api->setParent(QCoreApplication::instance());
return api;
}

void ClientFactory::setServer(QString server)
{
m_server = server;
}

} // namespace linglong::repo
32 changes: 32 additions & 0 deletions libs/linglong/src/linglong/repo/client_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/

#ifndef LINGLONG_SRC_MODULE_REPO_CLIENT_FACTORY_H_
#define LINGLONG_SRC_MODULE_REPO_CLIENT_FACTORY_H_
#include "ClientApi.h"

#include <QObject>
#include <QPoint>

namespace linglong::repo {

class ClientFactory : public QObject
{
Q_OBJECT
public:
ClientFactory(const QString &server);
ClientFactory(const std::string &server);

QSharedPointer<api::client::ClientApi> createClient() const;

void setServer(QString server);

private:
QString m_server;
};
} // namespace linglong::repo

#endif // LINGLONG_SRC_MODULE_REPO_CLIENT_FACTORY_H_
Loading

0 comments on commit bf8489c

Please sign in to comment.