Skip to content

Commit

Permalink
Change endpoints to enable self-hosted server usage capability. (#65)
Browse files Browse the repository at this point in the history
* Change endpoints to enable self-hosted server usage capability.
If no changes were made to conf files - app will use librum server.

* Fixed formatting

* remove word.remove compatibility changes

* add IGNORE_SSL_ERRORS settings field

* Update authentication_access.cpp

add forgotten quotes

* Refactored

* Fixed calling 'setupGlobalSettings' twice

* moved reply->readAll() after error handling if block

* Fixed formatting

---------

Co-authored-by: Oleg Proskurin <proskur@altlinux.org>
Co-authored-by: DavidLazarescu <prtnprvtmail@protonmail.com>
  • Loading branch information
3 people authored Nov 5, 2023
1 parent 3f048b6 commit cf51f35
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 72 deletions.
54 changes: 24 additions & 30 deletions src/infrastructure/data/endpoints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,56 @@
namespace infrastructure::data
{

inline const QString domain { "https://api.librumreader.com" };
inline const QString freeBooksDomain { "https://freebooks.librumreader.com" };

// Authentication
inline const QString authenticationEndpoint { domain +
"/authentication/login" };
inline const QString registrationEndpoint { domain +
"/authentication/register" };
inline const QString authenticationEndpoint { "/authentication/login" };
inline const QString registrationEndpoint { "/authentication/register" };
inline const QString checkIfEmailConfirmedEndpoint {
domain + "/authentication/checkIfEmailConfirmed"
"/authentication/checkIfEmailConfirmed"
};

// App Info
inline const QString latestAppVersionEndpoint { domain +
"/appinfo/latest-version" };
inline const QString latestAppVersionEndpoint { "/appinfo/latest-version" };

inline const QString binaryDownloadEndpoint {
"https://librumstorageaccount.blob.core.windows.net/releases"
};

// AI endpoint
inline const QString aiCompletionEndpoint { domain + "/Ai/complete" };
inline const QString aiCompletionEndpoint { "/Ai/complete" };

// Library storage
inline const QString bookCreationEndpoint { domain + "/book" };
inline const QString bookUpdateEndpoint { domain + "/book" };
inline const QString bookDeletionEndpoint { domain + "/book" };
inline const QString booksMetadataGetEndpoint { domain + "/book" };
inline const QString uploadBookDataEndpoint { domain + "/book/bookData" };
inline const QString downloadBookDataEndpoint { domain + "/book/bookData" };
inline const QString getBookCoverEndpoint { domain + "/book/cover" };
inline const QString changeBookCoverEndpoint { domain + "/book/cover" };
inline const QString deleteBookCoverEndpoint { domain + "/book/cover" };
inline const QString getUsedBookStorageEndpoint { domain +
"/book/usedBookStorage" };
inline const QString bookCreationEndpoint { "/book" };
inline const QString bookUpdateEndpoint { "/book" };
inline const QString bookDeletionEndpoint { "/book" };
inline const QString booksMetadataGetEndpoint { "/book" };
inline const QString uploadBookDataEndpoint { "/book/bookData" };
inline const QString downloadBookDataEndpoint { "/book/bookData" };
inline const QString getBookCoverEndpoint { "/book/cover" };
inline const QString changeBookCoverEndpoint { "/book/cover" };
inline const QString deleteBookCoverEndpoint { "/book/cover" };
inline const QString getUsedBookStorageEndpoint { "/book/usedBookStorage" };

// Free books storage
inline const QString getFreeBooksMetadataEndpoint { freeBooksDomain +
"/books" };

// User storage
inline const QString userGetEndpoint { domain + "/user" };
inline const QString userPatchEndpoint { domain + "/user" };
inline const QString userDeleteEndpoint { domain + "/user" };
inline const QString userForgotPasswordEndpoint { domain +
"/user/forgotPassword" };
inline const QString userChangePasswordEndpoint { domain + "/user" };
inline const QString userProfilePictureEndpoint { domain +
"/user/profilePicture" };
inline const QString userGetEndpoint { "/user" };
inline const QString userPatchEndpoint { "/user" };
inline const QString userDeleteEndpoint { "/user" };
inline const QString userForgotPasswordEndpoint { "/user/forgotPassword" };
inline const QString userChangePasswordEndpoint { "/user" };
inline const QString userProfilePictureEndpoint { "/user/profilePicture" };

// Dictionary API
inline const QString dictionaryDefinitionEndpoint {
"https://en.wiktionary.org/api/rest_v1/page/definition"
};

// Tag storage
inline const QString tagUpdateEndpoint { domain + "/tag" };
inline const QString tagDeletionEndpoint { domain + "/tag" };
inline const QString tagUpdateEndpoint { "/tag" };
inline const QString tagDeletionEndpoint { "/tag" };

} // namespace infrastructure::data
11 changes: 9 additions & 2 deletions src/infrastructure/persistance/ai_explanation_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
namespace infrastructure::persistence
{

AiExplanationAccess::AiExplanationAccess()
{
QSettings settings;
domain = settings.value("serverHost").toString();
}

void AiExplanationAccess::getExplanation(const QString& authToken,
const QString& query,
const QString& mode)
{
auto request = createRequest(data::aiCompletionEndpoint, authToken);
auto request =
createRequest(domain + data::aiCompletionEndpoint, authToken);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
QNetworkRequest::AlwaysNetwork);

Expand Down Expand Up @@ -93,4 +100,4 @@ QNetworkRequest AiExplanationAccess::createRequest(QUrl url,
return result;
}

} // namespace infrastructure::persistence
} // namespace infrastructure::persistence
6 changes: 5 additions & 1 deletion src/infrastructure/persistance/ai_explanation_access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QObject>
#include <QSettings>
#include "i_ai_explanation_access.hpp"

namespace infrastructure::persistence
Expand All @@ -16,6 +17,8 @@ class AiExplanationAccess : public adapters::IAiExplanationAccess
Q_OBJECT

public:
AiExplanationAccess();

void getExplanation(const QString& authToken, const QString& query,
const QString& mode) override;

Expand All @@ -24,6 +27,7 @@ class AiExplanationAccess : public adapters::IAiExplanationAccess
QDateTime m_lastRequestStartTime;

QNetworkAccessManager m_networkAccessManager;
QString domain;
};

} // namespace infrastructure::persistence
} // namespace infrastructure::persistence
2 changes: 1 addition & 1 deletion src/infrastructure/persistance/api_error_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ inline int logErrorMessage(QNetworkReply* reply, QString actionName)
return getErrorCode(replyContent);
}

} // namespace infrastructure::persistence::api_error_helper
} // namespace infrastructure::persistence::api_error_helper
2 changes: 1 addition & 1 deletion src/infrastructure/persistance/app_info_access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class AppInfoAccess : public adapters::IAppInfoAccess
QNetworkAccessManager m_networkAccessManager;
};

} // namespace infrastructure::persistence
} // namespace infrastructure::persistence
41 changes: 35 additions & 6 deletions src/infrastructure/persistance/authentication_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ using namespace adapters::dtos;
namespace infrastructure::persistence
{

AuthenticationAccess::AuthenticationAccess()
{
QSettings settings;
domain = settings.value("serverHost").toString();
}

void AuthenticationAccess::authenticateUser(const LoginDto& loginDto)
{
auto request = createRequest(data::authenticationEndpoint);
auto request = createRequest(domain + data::authenticationEndpoint);

QJsonObject jsonObject;
jsonObject["email"] = loginDto.email;
Expand All @@ -20,6 +26,27 @@ void AuthenticationAccess::authenticateUser(const LoginDto& loginDto)

auto reply = m_networkAccessManager.post(request, data);

// handle ssl errors
connect(reply, &QNetworkReply::sslErrors, this,
[reply](const QList<QSslError>& errors)
{
qWarning() << "SSL Errors: " << reply->errorString();
for(int i = 0; i < errors.count(); ++i)
{
qWarning() << errors[i].errorString();
}

QSettings settings;
if(settings.value("selfHosted").toString() == "true")
reply->ignoreSslErrors();
});

connect(reply, &QNetworkReply::errorOccurred, this,
[reply](QNetworkReply::NetworkError)
{
qWarning() << "Error " << reply->errorString();
});


// Handle authentication result and release the reply's memory
connect(reply, &QNetworkReply::finished, this,
Expand All @@ -35,14 +62,16 @@ void AuthenticationAccess::authenticateUser(const LoginDto& loginDto)
return;
}

emit authenticationFinished(reply->readAll());
reply->setReadBufferSize(1000);
QString token = QString::fromUtf8(reply->readAll());
emit authenticationFinished(token);
reply->deleteLater();
});
}

void AuthenticationAccess::registerUser(const RegisterDto& registerDto)
{
auto request = createRequest(data::registrationEndpoint);
auto request = createRequest(domain + data::registrationEndpoint);

QJsonObject jsonObject;
jsonObject["firstName"] = registerDto.firstName;
Expand Down Expand Up @@ -77,8 +106,8 @@ void AuthenticationAccess::registerUser(const RegisterDto& registerDto)

void AuthenticationAccess::checkIfEmailConfirmed(const QString& email)
{
auto request =
createRequest(data::checkIfEmailConfirmedEndpoint + "/" + email);
auto request = createRequest(domain + data::checkIfEmailConfirmedEndpoint +
"/" + email);

auto reply = m_networkAccessManager.get(request);

Expand Down Expand Up @@ -116,4 +145,4 @@ QNetworkRequest AuthenticationAccess::createRequest(QUrl url)
return result;
}

} // namespace infrastructure::persistence
} // namespace infrastructure::persistence
6 changes: 5 additions & 1 deletion src/infrastructure/persistance/authentication_access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QObject>
#include <QSettings>
#include <memory>
#include "i_authentication_access.hpp"

Expand All @@ -18,6 +19,8 @@ class AuthenticationAccess : public adapters::IAuthenticationAccess
Q_OBJECT

public:
AuthenticationAccess();

void authenticateUser(const adapters::dtos::LoginDto& loginDto) override;
void registerUser(const adapters::dtos::RegisterDto& registerDto) override;
void checkIfEmailConfirmed(const QString& email) override;
Expand All @@ -26,6 +29,7 @@ class AuthenticationAccess : public adapters::IAuthenticationAccess
QNetworkRequest createRequest(QUrl url);

QNetworkAccessManager m_networkAccessManager;
QString domain;
};

} // namespace infrastructure::persistence
} // namespace infrastructure::persistence
27 changes: 18 additions & 9 deletions src/infrastructure/persistance/library_storage_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@
namespace infrastructure::persistence
{

LibraryStorageAccess::LibraryStorageAccess()
{
QSettings settings;
domain = settings.value("serverHost").toString();
}

void LibraryStorageAccess::createBook(const QString& authToken,
const QJsonObject& jsonBook)
{
auto request = createRequest(data::bookCreationEndpoint, authToken);
auto request =
createRequest(domain + data::bookCreationEndpoint, authToken);

QJsonDocument jsonDocument(jsonBook);
QByteArray data = jsonDocument.toJson(QJsonDocument::Compact);
Expand Down Expand Up @@ -58,7 +65,8 @@ void LibraryStorageAccess::createBook(const QString& authToken,
void LibraryStorageAccess::deleteBook(const QString& authToken,
const QUuid& uuid)
{
auto request = createRequest(data::bookDeletionEndpoint, authToken);
auto request =
createRequest(domain + data::bookDeletionEndpoint, authToken);

QJsonArray bookArray;
bookArray.append(QJsonValue::fromVariant(uuid));
Expand Down Expand Up @@ -86,7 +94,7 @@ void LibraryStorageAccess::deleteBook(const QString& authToken,
void LibraryStorageAccess::updateBook(const QString& authToken,
const QJsonObject& jsonBook)
{
auto request = createRequest(data::bookUpdateEndpoint, authToken);
auto request = createRequest(domain + data::bookUpdateEndpoint, authToken);

QJsonDocument jsonDocument(jsonBook);
QByteArray data = jsonDocument.toJson(QJsonDocument::Compact);
Expand Down Expand Up @@ -138,7 +146,7 @@ void LibraryStorageAccess::uploadBookCover(const QString& authToken,
bookCover->append(imagePart);


QUrl endpoint = data::changeBookCoverEndpoint + "/" + stringUuid;
QUrl endpoint = domain + data::changeBookCoverEndpoint + "/" + stringUuid;
auto request = createRequest(endpoint, authToken);

// Reset the ContentTypeHeader since it will be set by the multipart
Expand Down Expand Up @@ -168,7 +176,7 @@ void LibraryStorageAccess::uploadBookCover(const QString& authToken,
void LibraryStorageAccess::deleteBookCover(const QString& authToken,
const QUuid& uuid)
{
QUrl endpoint = data::deleteBookCoverEndpoint + "/" +
QUrl endpoint = domain + data::deleteBookCoverEndpoint + "/" +
uuid.toString(QUuid::WithoutBraces);
auto request = createRequest(endpoint, authToken);

Expand All @@ -193,7 +201,8 @@ void LibraryStorageAccess::deleteBookCover(const QString& authToken,

void LibraryStorageAccess::getBooksMetaData(const QString& authToken)
{
auto request = createRequest(data::booksMetadataGetEndpoint, authToken);
auto request =
createRequest(domain + data::booksMetadataGetEndpoint, authToken);
auto reply = m_networkAccessManager.get(request);

connect(reply, &QNetworkReply::finished, this,
Expand All @@ -215,7 +224,7 @@ void LibraryStorageAccess::downloadCoverForBook(const QString& authToken,
const QUuid& uuid)
{
QString uuidString = uuid.toString(QUuid::WithoutBraces);
QString endpoint = data::getBookCoverEndpoint + "/" + uuidString;
QString endpoint = domain + data::getBookCoverEndpoint + "/" + uuidString;
auto request = createRequest(endpoint, authToken);
auto reply = m_networkAccessManager.get(request);

Expand All @@ -241,7 +250,7 @@ void LibraryStorageAccess::downloadCoverForBook(const QString& authToken,
void LibraryStorageAccess::downloadBookMedia(const QString& authToken,
const QUuid& uuid)
{
auto endpoint = data::downloadBookDataEndpoint + "/" +
auto endpoint = domain + data::downloadBookDataEndpoint + "/" +
uuid.toString(QUuid::WithoutBraces);
auto request = createRequest(endpoint, authToken);
auto reply = m_networkAccessManager.get(request);
Expand Down Expand Up @@ -322,7 +331,7 @@ void LibraryStorageAccess::uploadBookMedia(const QString& uuid,
}


QUrl endpoint = data::uploadBookDataEndpoint + "/" + uuid;
QUrl endpoint = domain + data::uploadBookDataEndpoint + "/" + uuid;
auto request = createRequest(endpoint, authToken);

// Reset the ContentTypeHeader since it will be set by the multipart
Expand Down
4 changes: 4 additions & 0 deletions src/infrastructure/persistance/library_storage_access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QSettings>
#include <QString>
#include "i_library_storage_access.hpp"

Expand All @@ -15,6 +16,8 @@ class LibraryStorageAccess : public adapters::ILibraryStorageAccess
Q_OBJECT

public:
LibraryStorageAccess();

void createBook(const QString& authToken,
const QJsonObject& jsonBook) override;
void deleteBook(const QString& authToken, const QUuid& uuid) override;
Expand All @@ -40,6 +43,7 @@ private slots:


QNetworkAccessManager m_networkAccessManager;
QString domain;
};

} // namespace infrastructure::persistence
Loading

0 comments on commit cf51f35

Please sign in to comment.