mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-06 14:16:32 +02:00
Add API 2.0 / Control API skeleton
This commit is contained in:
@@ -107,7 +107,11 @@ INCLUDE_DIRECTORIES(
|
||||
)
|
||||
|
||||
IF( QXTWEB_FOUND )
|
||||
LIST(APPEND tomahawkSources web/Api_v1.cpp)
|
||||
LIST(APPEND tomahawkSources
|
||||
web/Api_v1.cpp
|
||||
web/Api_v2.cpp
|
||||
web/Api_v2_0.cpp
|
||||
)
|
||||
LIST(APPEND LINK_LIBRARIES ${QXTWEB_LIBRARIES})
|
||||
INCLUDE_DIRECTORIES(${QXTWEB_INCLUDE_DIRS})
|
||||
ENDIF()
|
||||
|
@@ -4,6 +4,7 @@
|
||||
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
|
||||
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
|
||||
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -38,6 +39,7 @@
|
||||
#include "network/Servent.h"
|
||||
#include "network/DbSyncConnection.h"
|
||||
#include "web/Api_v1.h"
|
||||
#include "web/Api_v2.h"
|
||||
#include "SourceList.h"
|
||||
#include "ViewManager.h"
|
||||
#include "ShortcutHandler.h"
|
||||
@@ -490,36 +492,80 @@ TomahawkApp::initHTTP()
|
||||
delete m_httpv1_session.data();
|
||||
if ( !m_httpv1_connector.isNull() )
|
||||
delete m_httpv1_connector.data();
|
||||
if ( !m_httpv2_session.isNull() )
|
||||
delete m_httpv2_session.data();
|
||||
if ( !m_httpv2_connector.isNull() )
|
||||
delete m_httpv2_connector.data();
|
||||
return;
|
||||
}
|
||||
|
||||
if ( m_httpv1_session )
|
||||
{
|
||||
tLog() << "HTTPd session already exists, returning";
|
||||
return;
|
||||
tLog() << "HTTPd session for API v1.0 already exists, not starting again";
|
||||
}
|
||||
|
||||
m_httpv1_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() );
|
||||
m_httpv1_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector );
|
||||
if ( m_httpv1_session.isNull() || m_httpv1_connector.isNull() )
|
||||
else
|
||||
{
|
||||
if ( !m_httpv1_session.isNull() )
|
||||
delete m_httpv1_session.data();
|
||||
if ( !m_httpv1_connector.isNull() )
|
||||
delete m_httpv1_connector.data();
|
||||
tLog() << "Failed to start HTTPd, could not create object";
|
||||
return;
|
||||
m_httpv1_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() );
|
||||
m_httpv1_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector );
|
||||
if ( m_httpv1_session.isNull() || m_httpv1_connector.isNull() )
|
||||
{
|
||||
if ( !m_httpv1_session.isNull() )
|
||||
delete m_httpv1_session.data();
|
||||
if ( !m_httpv1_connector.isNull() )
|
||||
delete m_httpv1_connector.data();
|
||||
tLog() << "Failed to start HTTPd for API v1.0, could not create object";
|
||||
}
|
||||
else
|
||||
{
|
||||
m_httpv1_session.data()->setPort( 60210 ); //TODO config
|
||||
m_httpv1_session.data()->setListenInterface( QHostAddress::LocalHost );
|
||||
m_httpv1_session.data()->setConnector( m_httpv1_connector.data() );
|
||||
|
||||
Api_v1* api = new Api_v1( m_httpv1_session.data() );
|
||||
m_httpv1_session.data()->setStaticContentService( api );
|
||||
|
||||
tLog() << "Starting HTTPd for API v1.0 on" << m_httpv1_session.data()->listenInterface().toString() << m_httpv1_session.data()->port();
|
||||
m_httpv1_session.data()->start();
|
||||
}
|
||||
}
|
||||
|
||||
m_httpv1_session.data()->setPort( 60210 ); //TODO config
|
||||
m_httpv1_session.data()->setListenInterface( QHostAddress::LocalHost );
|
||||
m_httpv1_session.data()->setConnector( m_httpv1_connector.data() );
|
||||
if ( !m_httpv2_session.isNull() )
|
||||
{
|
||||
tLog() << "HTTPd session for API v2.0 already exists, not starting again";
|
||||
}
|
||||
else
|
||||
{
|
||||
m_httpv2_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() );
|
||||
m_httpv2_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector );
|
||||
if ( m_httpv2_session.isNull() || m_httpv2_connector.isNull() )
|
||||
{
|
||||
if ( !m_httpv2_session.isNull() )
|
||||
delete m_httpv2_session.data();
|
||||
if ( !m_httpv2_connector.isNull() )
|
||||
delete m_httpv2_connector.data();
|
||||
tLog() << "Failed to start HTTPd for API v2.0, could not create object";
|
||||
}
|
||||
else
|
||||
{
|
||||
m_httpv2_session->setPort( 60211 ); //TODO config
|
||||
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
|
||||
m_httpv2_session.data()->setListenInterface( QHostAddress::Any );
|
||||
#else
|
||||
m_httpv2_session.data()->setListenInterface( QHostAddress::AnyIPv6 );
|
||||
#endif
|
||||
|
||||
Api_v1* api = new Api_v1( m_httpv1_session.data() );
|
||||
m_httpv1_session.data()->setStaticContentService( api );
|
||||
m_httpv2_session->setConnector( m_httpv2_connector.data() );
|
||||
|
||||
tLog() << "Starting HTTPd on" << m_httpv1_session.data()->listenInterface().toString() << m_httpv1_session.data()->port();
|
||||
m_httpv1_session.data()->start();
|
||||
Api_v2* api = new Api_v2( m_httpv2_session.data() );
|
||||
m_httpv2_session->setStaticContentService( api );
|
||||
|
||||
tLog() << "Starting HTTPd for API v2.0 on" << m_httpv2_session->listenInterface().toString() << m_httpv2_session->port();
|
||||
if ( !m_httpv2_session->start() )
|
||||
{
|
||||
tLog() << "Failed to start HTTPd for API v2.0, could not bind to interface/port";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -150,6 +150,8 @@ private:
|
||||
|
||||
QPointer< QxtHttpServerConnector > m_httpv1_connector;
|
||||
QPointer< QxtHttpSessionManager > m_httpv1_session;
|
||||
QPointer< QxtHttpServerConnector > m_httpv2_connector;
|
||||
QPointer< QxtHttpSessionManager > m_httpv2_session;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE( PairList )
|
||||
|
55
src/tomahawk/web/Api_v2.cpp
Normal file
55
src/tomahawk/web/Api_v2.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Api_v2.h"
|
||||
|
||||
#include <QxtWeb/QxtWebPageEvent>
|
||||
|
||||
Api_v2::Api_v2( QxtAbstractWebSessionManager* sm, QObject* parent )
|
||||
: QxtWebSlotService( sm, parent )
|
||||
, m_apiv2_0( this )
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Api_v2::api( QxtWebRequestEvent *event, const QString &version, const QString &method )
|
||||
{
|
||||
if ( version == "2.0" )
|
||||
{
|
||||
if ( !QMetaObject::invokeMethod( &m_apiv2_0, method.toAscii().constData(), Q_ARG( QxtWebRequestEvent*, event ) ) )
|
||||
{
|
||||
// Method call failed, report failure
|
||||
sendPlain404( event, QString( "Method \"%1\" for API 2.0 not found" ).arg( method ), "Method in API 2.0 not found" );
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
sendPlain404( event, QString( "Unknown API version %1" ).arg( version ), "API version not found" );
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Api_v2::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.toAscii().constData();
|
||||
postEvent( e );
|
||||
}
|
46
src/tomahawk/web/Api_v2.h
Normal file
46
src/tomahawk/web/Api_v2.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef API_V2_H
|
||||
#define API_V2_H
|
||||
|
||||
#include "Api_v2_0.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <QxtWeb/QxtWebSlotService>
|
||||
|
||||
class Api_v2 : public QxtWebSlotService
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Api_v2( QxtAbstractWebSessionManager* sm, QObject* parent = 0 );
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
/**
|
||||
* All api (non-UI) calls with go to /api/<version>/method
|
||||
*/
|
||||
void api( QxtWebRequestEvent* event, const QString& version, const QString& method );
|
||||
private:
|
||||
void sendPlain404( QxtWebRequestEvent* event, const QString& message, const QString& statusmessage );
|
||||
Api_v2_0 m_apiv2_0;
|
||||
};
|
||||
|
||||
#endif // API_V2_H
|
36
src/tomahawk/web/Api_v2_0.cpp
Normal file
36
src/tomahawk/web/Api_v2_0.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Api_v2_0.h"
|
||||
|
||||
#include <QxtWeb/QxtWebPageEvent>
|
||||
#include <QxtWeb/QxtWebSlotService>
|
||||
|
||||
Api_v2_0::Api_v2_0( QxtWebSlotService *parent )
|
||||
: QObject( parent )
|
||||
, m_service( parent )
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Api_v2_0::ping( QxtWebRequestEvent *event )
|
||||
{
|
||||
QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, "pong" );
|
||||
e->contentType = "text/plain";
|
||||
m_service->postEvent( e );
|
||||
}
|
46
src/tomahawk/web/Api_v2_0.h
Normal file
46
src/tomahawk/web/Api_v2_0.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef API_V2_0_H
|
||||
#define API_V2_0_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QxtWebRequestEvent;
|
||||
class QxtWebSlotService;
|
||||
|
||||
class Api_v2_0 : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Api_v2_0( QxtWebSlotService *parent = 0 );
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
/**
|
||||
* Simple test to check for API 2.0 support.
|
||||
*
|
||||
* This call needs no authentication.
|
||||
*/
|
||||
void ping( QxtWebRequestEvent* event );
|
||||
private:
|
||||
QxtWebSlotService* m_service;
|
||||
};
|
||||
|
||||
#endif // API_V2_0_H
|
Reference in New Issue
Block a user