#include <QtCore/qcoreapplication.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qdir.h>
#include <QtCore/qfile.h>
#include <QtCore/qtimer.h>
#include <QtCore/qstandardpaths.h>
#include <RestLink/restlink.h>
{
static QDir folder(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) +
'/' + api->
name());
if (!folder.exists())
folder.mkpath(".");
if (url.count('/') > 1)
folder.mkpath(url.section("/", 1, -2));
QFile *file = new QFile(folder.path() + url + ".json", api);
QTextStream out(stdout);
const QByteArray data = reply->
readBody();
if (file->open(QIODevice::WriteOnly|QIODevice::Text)) {
file->write(data);
file->flush();
file->close();
file->deleteLater();
}
out << data.size() << " byte(s) downloaded" << Qt::endl;
} else {
out << "Oops ! - something weired happened" << Qt::endl;
}
int count = api->property("count").toInt() - 1;
if (count == 0)
qApp->quit();
else
api->setProperty("count", count);
});
}
{
QList<Request> requests;
requests.append("/configuration");
requests.append("/discover/movie");
requests.append("/discover/tv");
{
Request request(
"/search/company");
requests.append(request);
}
{
Request request(
"/movie/{movie_id}");
requests.append(request);
}
for (
const Request &request : std::as_const(requests))
runRequest(request, api);
api->setProperty("count", requests.size());
}
class Interceptor : public RequestInterceptor
{
public:
Request intercept(RequestHandler::Method method,
const Request &request,
const Body &body)
override
{
return request;
}
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QLoggingCategory::setFilterRules("restlink.info=true");
api.
setUrl(QUrl(
"http://api.themoviedb.org/3"));
Interceptor interceptor;
api.addRequestInterceptor(&interceptor);
run(&api);
return app.exec();
}
void get(const Request &request, const ApiRunCallback &callback)
Makes a GET request and calls the provided callback upon completion.
Definition apibase.cpp:82
Represents an API interface that manages configuration, requests, and parameters.
Definition api.h:15
Q_SLOT void setBearerToken(const QString &token)
Definition api.cpp:190
Q_SLOT void setUrl(const QUrl &url)
Sets the URL of the API.
Definition api.cpp:158
QString name
Definition api.h:17
Q_SLOT void setVersion(const QVersionNumber &version)
Sets the version of the API.
Definition api.cpp:129
Q_SLOT void setName(const QString &name)
Sets the name of the API.
Definition api.cpp:100
Represents the body of an HTTP request.
Definition body.h:27
A class for storing and managing HTTP request information.
Definition request.h:25
QString urlPath() const
Constructs the URL path for the request based on its parameters and endpoint.
Definition request.cpp:288
void addQueryParameter(const QString &name, const QVariant &value)
Add the value of a query parameter.
Definition requestinterface.cpp:215
void setPathParameter(const QString &name, const QVariant &value)
Sets the value of a path parameter.
Definition requestinterface.cpp:87
virtual QByteArray readBody()
Reads the raw response body as a QByteArray.
Definition responsebase.cpp:146
Represents a response to an API request in the RestLink framework.
Definition response.h:23
bool isSuccess() const
Checks if the request was successful.
Definition response.h:56
QString httpReasonPhrase
Retrieves the HTTP reason phrase of the response.
Definition response.h:33
QString endpoint
Definition response.h:25
bool hasHttpStatusCode
Definition response.h:31
int networkError
Definition response.h:36
bool hasNetworkError
Checks if a network error occurred during the request.
Definition response.h:35
int httpStatusCode
Retrieves the HTTP status code of the response.
Definition response.h:32
QString networkErrorString
Definition response.h:37
Definition abstractrequesthandler.cpp:11
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import RestLink 2.0
// Let's test RestLink in QML using TMDB API
ApplicationWindow {
id: win
// If okay, TMDB V3 is our window title
title: tmdb.name + " V" + tmdb.version
// Mobile like !
width: 320
height: 640
// Be visible :)
visible: true
header: TextField {
id: query
text: "Halo"
placeholderText: "Query..."
// Run request with the new query on return
onAccepted: request.run()
}
ListView {
id: view
delegate: MediaDelegate {
readonly property var media: model
// Yeah, i'm lazy, just followed TDMB API documentation ;)
poster: (modelData.poster_path ? modelData.poster_path : "")
title: (modelData.title ? modelData.title : modelData.name)
overview: (modelData.overview ? modelData.overview : "")
popularity: (modelData.popularity ? modelData.popularity : 0.0)
year: 2025 // Something is wrong here
accentColor: "pink"
width: ListView.view.width
height: 96
}
spacing: 5
anchors.fill: parent
anchors.margins: 9
}
Request {
id: request
// Just to mention, it's already the default
method: Request.Get // Http method
// Documentation on: https://developer.themoviedb.org
endpoint: "/search/multi"
// Don't forget it !
api: tmdb
RequestParameter {
name: "query"
value: query.text
// Just to mention, it's already the default
type: RequestParameter.UrlQuery
}
// We just log the full request Url
// We do it here cause response object got created just before this signal has been emitted
onStarted: console.log(response.url)
// Let's understand what server said !
onFinished: function() {
if (response.success) {
// Parsing JSON
var json = JSON.parse(response.body);
// Filling view with JSON response
view.model = json.results;
// Checking compression header, have you built RestLink with ZLIB ?
// gzip and defalte supported without manual intervention
console.log("Compression: " + response.header('Content-Encoding'));
} else if (response.hasHttpStatusCode) {
// Logging HTTP error
console.log("HTTP " + response.httpStatusCode + ' ' + response.httpReasonPhrase);
} else if (response.hasNetworkError) {
// Logging network error
console.log("Network Error " + response.networkError + ' ' + response.networkErrorString);
} else {
// Logging unknown error
console.log("Unknown error occured");
}
}
}
Api {
id: tmdb
name: "TMDB" // Optional
version: "3" // Optional, yeah it's a string
url: "https://api.themoviedb.org/3" // Mandatory
// This will automatically add the Authorization header for you,
// just put your Bearer token without 'Bearer' prefix
// You can set the token in CMake cache variable of the same name so no need to
// change something here directly, that will avoid some problems with versioning tools
bearerToken: TMDB_BEARER_TOKEN
// By default, system locale used, but we switch to US English okay ?
locale: Qt.locale("en_US")
// As a lazy man, i don't want to add these parameters on each requests
// so i add them here for reuse.
ApiParameter {
name: "language"
// value is used as format, this will use the locale Api property to generate language code
// format examples: '.' for en, '-' for en-US and '_' for en_US
value: "." // Here en will be generated is locale is en_US, TMDB API requirement
// Correspond to the RestLink::Parameter::Locale flag
locale: true // Makes the value of this property as language code taken from Api locale property
}
ApiParameter {
name: "adult"
value: false // i'm a child :)
}
ApiParameter {
name: "Authorization"
value: "Bearer ..."
// By default, the parameter is a url query parameter, we don't want this
type: ApiParameter.Header // Yes, it's a header !
// We don't need it so we disable without removing, nice euh ?
enabled: false // finally we don't need this since Api already manages it
}
}
}
// Written with ♥️ by Amadou Wanie Benjamain !