-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added osu! API
- Loading branch information
Showing
19 changed files
with
451 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "clientCredentialsFlow.h" | ||
|
||
QJsonObject ClientCredentialsFlow::getToken(QString clientId, QString clientSecret, QString oAuthUrl) | ||
{ | ||
qDebug() << "[ClientCredentialsFlow] getToken"; | ||
QNetworkAccessManager manager; | ||
QNetworkRequest request((QUrl(oAuthUrl))); | ||
|
||
request.setHeader( | ||
QNetworkRequest::ContentTypeHeader, | ||
"application/x-www-form-urlencoded" | ||
); | ||
request.setHeader( | ||
QNetworkRequest::UserAgentHeader, | ||
"Miraya" | ||
); | ||
|
||
QUrlQuery params; | ||
params.addQueryItem("client_id", clientId); | ||
params.addQueryItem("client_secret", clientSecret); | ||
params.addQueryItem("grant_type", "client_credentials"); | ||
params.addQueryItem("scope", "public"); | ||
|
||
QNetworkReply *reply = manager.post(request, params.toString(QUrl::FullyEncoded).toUtf8()); | ||
|
||
QEventLoop loop; | ||
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); | ||
loop.exec(); | ||
|
||
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); | ||
|
||
qDebug() << "[ClientCredentialsFlow] HTTP Status code: " << httpStatusCode; | ||
if (httpStatusCode == 200) { | ||
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll()); | ||
QJsonObject obj = doc.object(); | ||
return obj; | ||
} | ||
qDebug() << "[ClientCredentialsFlow] Failed to get token"; | ||
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll()); | ||
qDebug() << QString(doc.toJson(QJsonDocument::Compact)); | ||
return doc.object(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef CLIENTCREDENTIALSFLOW_H | ||
#define CLIENTCREDENTIALSFLOW_H | ||
|
||
#include <QUrl> | ||
#include <QDebug> | ||
#include <QEventLoop> | ||
#include <QNetworkAccessManager> | ||
#include <QNetworkRequest> | ||
#include <QNetworkReply> | ||
#include <QUrlQuery> | ||
#include <QJsonDocument> | ||
#include <QJsonObject> | ||
|
||
class ClientCredentialsFlow | ||
{ | ||
public: | ||
static QJsonObject getToken( | ||
QString clientId, | ||
QString clientSecret, | ||
QString oAuthUrl = QString("https://osu.ppy.sh/oauth/token") | ||
); | ||
}; | ||
|
||
#endif // CLIENTCREDENTIALSFLOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "osuapi.h" | ||
|
||
OsuApi::OsuApi() | ||
{ | ||
qDebug() << "[OsuApi] Init"; | ||
QSettings settings; | ||
clientId = settings.value("osuapi/clientId").toString(); | ||
clientSecret = settings.value("osuapi/clientSecret").toString(); | ||
oAuthUrl = "https://osu.ppy.sh/oauth/token"; | ||
|
||
if (!(clientId.isEmpty() || clientSecret.isEmpty())) { | ||
token = ClientCredentialsFlow::getToken(clientId, clientSecret, oAuthUrl); | ||
} | ||
else { | ||
qDebug() << "[OsuApi] Client ID or Client Secret are empty"; | ||
} | ||
} | ||
|
||
QJsonObject OsuApi::getBeatmapInfo(int beatmapId) | ||
{ | ||
// TODO: A lot can be split into smaller functions | ||
// TODO: also, the QEventLoop is making everything synchronous, which is suboptimal to say the least. | ||
qDebug() << "[OsuApi] getBeatmapInfo"; | ||
QNetworkAccessManager manager; | ||
QUrl url(QString("https://osu.ppy.sh/api/v2/beatmaps/%1").arg(beatmapId)); | ||
|
||
QNetworkRequest request(url); | ||
|
||
QString accessToken = token["access_token"].toString(); | ||
|
||
request.setRawHeader( | ||
QByteArray("Authorization"), | ||
(QString("Bearer %1").arg(accessToken)).toUtf8() | ||
); | ||
|
||
QNetworkReply *reply = manager.get(request); | ||
|
||
QEventLoop loop; | ||
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); | ||
loop.exec(); | ||
|
||
qDebug() << "[OsuApi] get request done"; | ||
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); | ||
qDebug() << "[OsuApi] HTTP Status code: " << httpStatusCode; | ||
if (httpStatusCode == 200) { | ||
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll()); | ||
QJsonObject obj = doc.object(); | ||
qDebug() << "[OsuApi] Beatmap info: " << obj; | ||
return obj; | ||
} | ||
qDebug() << "[OsuApi] Error: " << reply->errorString(); | ||
return QJsonObject(); | ||
} | ||
|
||
bool OsuApi::isValid() | ||
{ | ||
return !(clientId.isEmpty() || clientSecret.isEmpty()); | ||
} |
Oops, something went wrong.