/thirdparty/liblastfm2/src/ws/ws.h

http://github.com/tomahawk-player/tomahawk · C Header · 149 lines · 73 code · 25 blank · 51 comment · 0 complexity · 03f9954004ac5184eb8f3997fb0b2c43 MD5 · raw file

  1. /*
  2. Copyright 2009 Last.fm Ltd.
  3. - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4. This file is part of liblastfm.
  5. liblastfm is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. liblastfm is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with liblastfm. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef LASTFM_WS_H
  17. #define LASTFM_WS_H
  18. #include <lastfm/global.h>
  19. #include <QDateTime>
  20. #include <QMap>
  21. #include <QNetworkReply>
  22. #include <stdexcept>
  23. #ifdef Q_CC_MSVC
  24. // ms admits its lousy compiler doesn't care about throw declarations
  25. #pragma warning( disable : 4290 )
  26. #endif
  27. namespace lastfm
  28. {
  29. /** if you don't set one, we create our own, our own is pretty good
  30. * for instance, it auto detects proxy settings on windows and mac
  31. * We take ownership of the NAM, do not delete it out from underneath us!
  32. * So don't keep any other pointers to this around in case you accidently
  33. * call delete on them :P */
  34. LASTFM_DLLEXPORT void setNetworkAccessManager( QNetworkAccessManager* nam );
  35. LASTFM_DLLEXPORT QNetworkAccessManager* nam();
  36. namespace ws
  37. {
  38. /** both of these are provided when you register at http://last.fm/api */
  39. LASTFM_DLLEXPORT extern const char* SharedSecret;
  40. LASTFM_DLLEXPORT extern const char* ApiKey;
  41. /** you need to set this for scrobbling to work (for now)
  42. * Also the AuthenticatedUser class uses it */
  43. LASTFM_DLLEXPORT extern QString Username;
  44. /** Some webservices require authentication. See the following
  45. * documentation:
  46. * http://www.last.fm/api/authentication
  47. * http://www.last.fm/api/desktopauth
  48. * You have to authenticate and then assign to SessionKey, liblastfm does
  49. * not do that for you. Also we do not store this. You should store this!
  50. * You only need to authenticate once, and that key lasts forever!
  51. */
  52. LASTFM_DLLEXPORT extern QString SessionKey;
  53. enum Error
  54. {
  55. NoError = 1, // because last.fm error numbers start at 2
  56. /** numbers follow those at http://last.fm/api/ */
  57. InvalidService = 2,
  58. InvalidMethod,
  59. AuthenticationFailed,
  60. InvalidFormat,
  61. InvalidParameters,
  62. InvalidResourceSpecified,
  63. OperationFailed,
  64. InvalidSessionKey,
  65. InvalidApiKey,
  66. ServiceOffline,
  67. SubscribersOnly,
  68. Reserved13,
  69. Reserved14,
  70. Reserved15,
  71. /** Last.fm sucks.
  72. * There may be an error in networkError(), or this may just be some
  73. * internal error completing your request.
  74. * Advise the user to try again in a _few_minutes_.
  75. * For some cases, you may want to try again yourself, at this point
  76. * in the API you will have to. Eventually we will discourage this and
  77. * do it for you, as we don't want to strain Last.fm's servers
  78. */
  79. TryAgainLater = 16,
  80. Reserved17,
  81. Reserved18,
  82. Reserved19,
  83. NotEnoughContent = 20,
  84. NotEnoughMembers,
  85. NotEnoughFans,
  86. NotEnoughNeighbours,
  87. /** Last.fm fucked up, or something mangled the response on its way */
  88. MalformedResponse = 100,
  89. /** call QNetworkReply::error() as it's nothing to do with us */
  90. UnknownError
  91. };
  92. LASTFM_DLLEXPORT QString host();
  93. /** the map needs a method entry, as per http://last.fm/api */
  94. LASTFM_DLLEXPORT QUrl url( QMap<QString, QString> );
  95. LASTFM_DLLEXPORT QNetworkReply* get( QMap<QString, QString> );
  96. /** generates api sig, includes api key, and posts, don't add the api
  97. * key yourself as well--it'll break */
  98. LASTFM_DLLEXPORT QNetworkReply* post( QMap<QString, QString>, bool sessionKey = true );
  99. class ParseError : public std::runtime_error
  100. {
  101. Error e;
  102. QString m_message;
  103. public:
  104. explicit ParseError( Error e, QString message )
  105. :std::runtime_error("lastfm::ws::Error"), e(e), m_message(message)
  106. {}
  107. Error enumValue() const { return e; }
  108. QString message() const { return m_message; }
  109. ~ParseError() throw() {;}
  110. };
  111. /** returns the expiry date of this HTTP response */
  112. LASTFM_DLLEXPORT QDateTime expires( QNetworkReply* );
  113. }
  114. }
  115. inline QDebug operator<<( QDebug d, QNetworkReply::NetworkError e )
  116. {
  117. return d << lastfm::qMetaEnumString<QNetworkReply>( e, "NetworkError" );
  118. }
  119. #define LASTFM_WS_HOSTNAME "ws.audioscrobbler.com"
  120. #endif