From 062a09ccd76225b9eb0fad9a5f572432c29f2eb5 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Tue, 28 Jan 2014 21:38:00 +0000 Subject: [PATCH] Add (web) API 1.5 skeleton --- src/libtomahawk-playdarapi/Api_v1.cpp | 65 +++++++++++++++++++---- src/libtomahawk-playdarapi/Api_v1.h | 15 +++++- src/libtomahawk-playdarapi/Api_v1_5.cpp | 37 +++++++++++++ src/libtomahawk-playdarapi/Api_v1_5.h | 47 ++++++++++++++++ src/libtomahawk-playdarapi/CMakeLists.txt | 1 + 5 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 src/libtomahawk-playdarapi/Api_v1_5.cpp create mode 100644 src/libtomahawk-playdarapi/Api_v1_5.h diff --git a/src/libtomahawk-playdarapi/Api_v1.cpp b/src/libtomahawk-playdarapi/Api_v1.cpp index b58bf6e2e..dea66983b 100644 --- a/src/libtomahawk-playdarapi/Api_v1.cpp +++ b/src/libtomahawk-playdarapi/Api_v1.cpp @@ -28,6 +28,7 @@ #include "utils/Logger.h" #include "utils/TomahawkUtils.h" +#include "Api_v1_5.h" #include "Pipeline.h" #include "Result.h" #include "Source.h" @@ -41,9 +42,15 @@ using namespace TomahawkUtils; Api_v1::Api_v1( QxtAbstractWebSessionManager* sm, QObject* parent ) : QxtWebSlotService(sm, parent) + , m_api_v1_5( new Api_v1_5( this ) ) { } +Api_v1::~Api_v1() +{ + delete m_api_v1_5; +} + void Api_v1::auth_1( QxtWebRequestEvent* event, QString arg ) { @@ -155,23 +162,43 @@ Api_v1::auth_2( QxtWebRequestEvent* event, QString arg ) } -// all v1 api calls go to /api/ +/** + * Handle API calls. + * + * All v1.0 API (standard Playdar) calls go to /api/?method= + * All v1.5 API (simple remote control) calls go to /api/1.5/ + */ void -Api_v1::api( QxtWebRequestEvent* event ) +Api_v1::api( QxtWebRequestEvent* event, const QString& version, const QString& method, const QString& arg1, const QString& arg2, const QString& arg3 ) { tDebug( LOGVERBOSE ) << "HTTP" << event->url.toString(); - const QUrl& url = event->url; - if ( urlHasQueryItem( url, "method" ) ) - { - const QString method = urlQueryItemValue( url, "method" ); + if ( version.isEmpty() ) { + // We dealing with API 1.0 - if ( method == "stat" ) return stat( event ); - if ( method == "resolve" ) return resolve( event ); - if ( method == "get_results" ) return get_results( event ); + const QUrl& url = event->url; + if ( urlHasQueryItem( url, "method" ) ) + { + const QString method = urlQueryItemValue( url, "method" ); + + if ( method == "stat" ) return stat( event ); + if ( method == "resolve" ) return resolve( event ); + if ( method == "get_results" ) return get_results( event ); + } + + send404( event ); + } + else if ( version == "1.5" ) + { + if ( !QMetaObject::invokeMethod( m_api_v1_5, method.toLatin1().constData(), Q_ARG( QxtWebRequestEvent*, event ) ) ) + { + apiCallFailed(event, method); + } + } + else + { + sendPlain404( event, QString( "Unknown API version %1" ).arg( version ), "API version not found" ); } - - send404( event ); } @@ -226,6 +253,16 @@ Api_v1::send404( QxtWebRequestEvent* event ) postEvent( wpe ); } +void +Api_v1::sendPlain404( QxtWebRequestEvent* event, const QString& message, const QString& statusmessage ) +{ + QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, message.toUtf8() ); + e->contentType = "text/plain"; + e->status = 404; + e->statusMessage = statusmessage.toLatin1().constData(); + postEvent( e ); +} + void Api_v1::stat( QxtWebRequestEvent* event ) @@ -447,3 +484,9 @@ Api_v1::index( QxtWebRequestEvent* event ) { send404( event ); } + +void +Api_v1::apiCallFailed( QxtWebRequestEvent* event, const QString& method ) +{ + sendPlain404( event, QString( "Method \"%1\" for API 1.5 not found" ).arg( method ), "Method in API 1.5 not found" ); +} diff --git a/src/libtomahawk-playdarapi/Api_v1.h b/src/libtomahawk-playdarapi/Api_v1.h index 25c2cccda..843c388d7 100644 --- a/src/libtomahawk-playdarapi/Api_v1.h +++ b/src/libtomahawk-playdarapi/Api_v1.h @@ -39,6 +39,8 @@ #include #include +class Api_v1_5; + namespace Tomahawk { class Result; @@ -51,6 +53,7 @@ Q_OBJECT public: Api_v1( QxtAbstractWebSessionManager* sm, QObject* parent = 0 ); + virtual ~Api_v1(); public slots: // authenticating uses /auth_1 @@ -59,7 +62,12 @@ public slots: void auth_2( QxtWebRequestEvent* event, QString unused = QString() ); // all v1 api calls go to /api/ - void api( QxtWebRequestEvent* event ); + void api( QxtWebRequestEvent* event, + const QString& version = QString(), + const QString& method = QString(), + const QString& arg1 = QString(), + const QString& arg2 = QString(), + const QString& arg3 = QString() ); // request for stream: /sid/ void sid( QxtWebRequestEvent* event, QString unused = QString() ); @@ -78,11 +86,16 @@ public slots: void index( QxtWebRequestEvent* event ); +protected: + void apiCallFailed( QxtWebRequestEvent* event, const QString& method ); + void sendPlain404( QxtWebRequestEvent* event, const QString& message, const QString& statusmessage ); + private: void processSid( QxtWebRequestEvent* event, Tomahawk::result_ptr&, QSharedPointer< QIODevice >& ); QxtWebRequestEvent* m_storedEvent; QSharedPointer< QIODevice > m_ioDevice; + Api_v1_5* m_api_v1_5; }; #endif diff --git a/src/libtomahawk-playdarapi/Api_v1_5.cpp b/src/libtomahawk-playdarapi/Api_v1_5.cpp new file mode 100644 index 000000000..ac3864b1e --- /dev/null +++ b/src/libtomahawk-playdarapi/Api_v1_5.cpp @@ -0,0 +1,37 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2014, Uwe L. Korn + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "Api_v1_5.h" + +#include "Api_v1.h" + +Api_v1_5::Api_v1_5( Api_v1* parent ) + : QObject( parent ) + , m_service( parent ) +{ +} + + +void +Api_v1_5::ping( QxtWebRequestEvent* event ) +{ + QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, "pong" ); + e->contentType = "text/plain"; + m_service->postEvent( e ); +} + diff --git a/src/libtomahawk-playdarapi/Api_v1_5.h b/src/libtomahawk-playdarapi/Api_v1_5.h new file mode 100644 index 000000000..6a0c5edb5 --- /dev/null +++ b/src/libtomahawk-playdarapi/Api_v1_5.h @@ -0,0 +1,47 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2014, Uwe L. Korn + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef API_V1_5_H +#define API_V1_5_H + +#include + +class Api_v1; +class QxtWebRequestEvent; + +class Api_v1_5 : public QObject +{ + Q_OBJECT +public: + Api_v1_5( Api_v1* parent = 0 ); + +signals: + +public slots: + /** + * Simple test to check for API 1.5 support. + * + * This call needs no authentication. + */ + void ping( QxtWebRequestEvent* event ); + +private: + Api_v1* m_service; +}; + +#endif // API_V1_5_H diff --git a/src/libtomahawk-playdarapi/CMakeLists.txt b/src/libtomahawk-playdarapi/CMakeLists.txt index 8afd25c43..bf251ab6f 100644 --- a/src/libtomahawk-playdarapi/CMakeLists.txt +++ b/src/libtomahawk-playdarapi/CMakeLists.txt @@ -3,6 +3,7 @@ set(TOMAHAWK_PLAYDARAPI_LIBRARY_TARGET tomahawk-playdarapi) list(APPEND ${TOMAHAWK_PLAYDARAPI_LIBRARY_TARGET}_SOURCES Api_v1.cpp + Api_v1_5.cpp PlaydarApi.cpp )