~ubuntu-branches/ubuntu/precise/libmusicbrainz/precise-updates

« back to all changes in this revision

Viewing changes to include/musicbrainz4/Query.h

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-02-01 17:18:10 UTC
  • Revision ID: package-import@ubuntu.com-20120201171810-jzz90w51dx6shdr1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* --------------------------------------------------------------------------
 
2
 
 
3
   libmusicbrainz4 - Client library to access MusicBrainz
 
4
 
 
5
   Copyright (C) 2011 Andrew Hawkins
 
6
 
 
7
   This file is part of libmusicbrainz4.
 
8
 
 
9
   This library is free software; you can redistribute it and/or
 
10
   modify it under the terms of v2 of the GNU Lesser General Public
 
11
   License as published by the Free Software Foundation.
 
12
 
 
13
   libmusicbrainz4 is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
   Lesser General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
     $Id: Query.h 13266 2011-08-25 12:56:47Z adhawkins $
 
22
 
 
23
----------------------------------------------------------------------------*/
 
24
 
 
25
#ifndef _MUSICBRAINZ4_QUERY_H
 
26
#define _MUSICBRAINZ4_QUERY_H
 
27
 
 
28
#include "musicbrainz4/ReleaseList.h"
 
29
#include "musicbrainz4/Metadata.h"
 
30
 
 
31
#include "musicbrainz4/xmlParser.h"
 
32
 
 
33
#include <string>
 
34
#include <map>
 
35
#include <vector>
 
36
 
 
37
/**
 
38
 * @mainpage libmusicbrainz4
 
39
 *
 
40
 * This is the documentation for libmusicbrainz4, a library for retrieving data from
 
41
 * the <a target="_blank" href="http://musicbrainz.org">MusicBrainz</a> service.
 
42
 *
 
43
 * The main entry point to the library is the MusicBrainz4::CQuery object.
 
44
 *
 
45
 * For details of the C interface, see the documentation for the file mb4_c.h.
 
46
 *
 
47
 * Please report any issues with this library at
 
48
 * <a target="_blank" href="http://tickets.musicbrainz.org/">http://tickets.musicbrainz.org/</a>.
 
49
 *
 
50
 * A brief example showing how to lookup a list of releases matching a disc id
 
51
 *
 
52
@code
 
53
MusicBrainz4::CQuery Query("cdlookupexample-1.0");
 
54
 
 
55
try
 
56
{
 
57
        MusicBrainz4::CMetadata Metadata=Query.Query("discid",DiscID);
 
58
        if (Metadata.Disc() && Metadata.Disc()->ReleaseList())
 
59
        {
 
60
                MusicBrainz4::CReleaseList *ReleaseList=Metadata.Disc()->ReleaseList();
 
61
 
 
62
                std::cout << "Found " << ReleaseList->NumItems() << " release(s)" << std::endl;
 
63
 
 
64
                for (int count=0;count<ReleaseList->NumItems();count++)
 
65
                {
 
66
                        MusicBrainz4::CRelease *Release=ReleaseList->Item(count);
 
67
 
 
68
                        std::cout << "Basic release: " << std::endl << *Release << std::endl;
 
69
 
 
70
                        //The releases returned from LookupDiscID don't contain full information
 
71
 
 
72
                        MusicBrainz4::CQuery::tParamMap Params;
 
73
                        Params["inc"]="artists labels recordings release-groups url-rels discids artist-credits";
 
74
 
 
75
                        Metadata=Query.Query("release",Release.ID(),"",Params);
 
76
                        if (Metadata.Release())
 
77
                        {
 
78
                                MusicBrainz4::CRelease *FullRelease=Metadata.Release();
 
79
 
 
80
                                std::cout << *FullRelease << std::endl;
 
81
                        }
 
82
                }
 
83
        }
 
84
}
 
85
 
 
86
catch (MusicBrainz4::CConnectionError& Error)
 
87
{
 
88
        std::cout << "Connection Exception: '" << Error.what() << "'" << std::endl;
 
89
        std::cout << "LastResult: " << Query.LastResult() << std::endl;
 
90
        std::cout << "LastHTTPCode: " << Query.LastHTTPCode() << std::endl;
 
91
        std::cout << "LastErrorMessage: " << Query.LastErrorMessage() << std::endl;
 
92
}
 
93
 
 
94
catch (MusicBrainz4::CTimeoutError& Error)
 
95
{
 
96
        std::cout << "Timeout Exception: '" << Error.what() << "'" << std::endl;
 
97
        std::cout << "LastResult: " << Query.LastResult() << std::endl;
 
98
        std::cout << "LastHTTPCode: " << Query.LastHTTPCode() << std::endl;
 
99
        std::cout << "LastErrorMessage: " << Query.LastErrorMessage() << std::endl;
 
100
}
 
101
 
 
102
catch (MusicBrainz4::CAuthenticationError& Error)
 
103
{
 
104
        std::cout << "Authentication Exception: '" << Error.what() << "'" << std::endl;
 
105
        std::cout << "LastResult: " << Query.LastResult() << std::endl;
 
106
        std::cout << "LastHTTPCode: " << Query.LastHTTPCode() << std::endl;
 
107
        std::cout << "LastErrorMessage: " << Query.LastErrorMessage() << std::endl;
 
108
}
 
109
 
 
110
catch (MusicBrainz4::CFetchError& Error)
 
111
{
 
112
        std::cout << "Fetch Exception: '" << Error.what() << "'" << std::endl;
 
113
        std::cout << "LastResult: " << Query.LastResult() << std::endl;
 
114
        std::cout << "LastHTTPCode: " << Query.LastHTTPCode() << std::endl;
 
115
        std::cout << "LastErrorMessage: " << Query.LastErrorMessage() << std::endl;
 
116
}
 
117
 
 
118
catch (MusicBrainz4::CRequestError& Error)
 
119
{
 
120
        std::cout << "Request Exception: '" << Error.what() << "'" << std::endl;
 
121
        std::cout << "LastResult: " << Query.LastResult() << std::endl;
 
122
        std::cout << "LastHTTPCode: " << Query.LastHTTPCode() << std::endl;
 
123
        std::cout << "LastErrorMessage: " << Query.LastErrorMessage() << std::endl;
 
124
}
 
125
 
 
126
catch (MusicBrainz4::CResourceNotFoundError& Error)
 
127
{
 
128
        std::cout << "ResourceNotFound Exception: '" << Error.what() << "'" << std::endl;
 
129
        std::cout << "LastResult: " << Query.LastResult() << std::endl;
 
130
        std::cout << "LastHTTPCode: " << Query.LastHTTPCode() << std::endl;
 
131
        std::cout << "LastErrorMessage: " << Query.LastErrorMessage() << std::endl;
 
132
}
 
133
@endcode
 
134
 */
 
135
 
 
136
namespace MusicBrainz4
 
137
{
 
138
        class CQueryPrivate;
 
139
 
 
140
        /**
 
141
         * @brief Main object for generating queries to MusicBrainz
 
142
         *
 
143
         * This object is the main entry point for the library, generating a query to the
 
144
         * MusicBrainz service, and parsing the results. The resultant objects can be used
 
145
         * to retrieve information appropriate to the query.
 
146
         *
 
147
         * For information on generating queries and the expected responses, see the
 
148
         * documentation for the
 
149
         * <a target="_blank" href="http://musicbrainz.org/doc/XML_Web_Service/Version_2">web service</a>.
 
150
         *
 
151
         * For information on search syntax, see the documentation for
 
152
         * <a target="_blank" href="http://wiki.musicbrainz.org/Text_Search_Syntax">search syntax</a>.
 
153
         *
 
154
         * @b Note It is the responsibility of the caller to validate any pointers returned
 
155
         * from the library. It is valid for a pointer to be NULL if the information was not
 
156
         * present in the response from the MusicBrainz service.
 
157
         *
 
158
         * @b Note The ownership of any pointers returned from the C++ interfaces remains
 
159
         * with the library. The caller should not delete any pointer returned from the
 
160
         * library. Users of the C library should take note of the documentation for each
 
161
         * individual function in mb4_c.h
 
162
         */
 
163
        class CQuery
 
164
        {
 
165
        public:
 
166
                typedef std::map<std::string,std::string> tParamMap;
 
167
 
 
168
                /**
 
169
                 * @brief Enumerated type for query status
 
170
                 *
 
171
                 * Enumerated type for query status
 
172
                 */
 
173
                enum tQueryResult
 
174
                {
 
175
                        eQuery_Success=0,
 
176
                        eQuery_ConnectionError,
 
177
                        eQuery_Timeout,
 
178
                        eQuery_AuthenticationError,
 
179
                        eQuery_FetchError,
 
180
                        eQuery_RequestError,
 
181
                        eQuery_ResourceNotFound
 
182
                };
 
183
 
 
184
                /**
 
185
                 * @brief Constructor for MusicBrainz::CQuery object
 
186
                 *
 
187
                 * This is the constructor for the MusicBrainz::CQuery object.
 
188
                 *
 
189
                 * @param UserAgent User agent to use in any queries and submissions. The format
 
190
                 *              is @c "application-version", where application is your application's name
 
191
                 *              and version is a version number which may not contain a '-' character.
 
192
                 * @param Server Server to be used (defaults to musicbrainz.org if not specified)
 
193
                 * @param Port Port to use (defaults to 80 if not specified)
 
194
                 *
 
195
                 */
 
196
 
 
197
                CQuery(const std::string& UserAgent, const std::string& Server="musicbrainz.org", int Port=80);
 
198
 
 
199
                ~CQuery();
 
200
 
 
201
                /**
 
202
                 * @brief Set the user name
 
203
                 *
 
204
                 * Set the user name to use when authenticating to the MusicBrainz service
 
205
                 *
 
206
                 * @param UserName Username to use
 
207
                 */
 
208
 
 
209
                void SetUserName(const std::string& UserName);
 
210
 
 
211
                /**
 
212
                 * @brief Set the password
 
213
                 *
 
214
                 * Set the password to use when authenticating to the MusicBrainz service
 
215
                 *
 
216
                 * @param Password Password to use
 
217
                 */
 
218
 
 
219
                void SetPassword(const std::string& Password);
 
220
 
 
221
                /**
 
222
                 * @brief Set proxy server
 
223
                 *
 
224
                 * Set the proxy server to use for queries. @b Note The http_proxy environment variable
 
225
                 * will be used to set a 'default' proxy server. Calls to this method will override any
 
226
                 * proxy settings set by the http_proxy environment variable.
 
227
                 *
 
228
                 * @param ProxyHost Proxy server to use
 
229
                 */
 
230
 
 
231
                void SetProxyHost(const std::string& ProxyHost);
 
232
 
 
233
                /**
 
234
                 * @brief Set proxy server port
 
235
                 *
 
236
                 * Set the proxy server port to use for queries. @b Note The http_proxy environment variable
 
237
                 * will be used to set a 'default' proxy server. Calls to this method will override any
 
238
                 * proxy settings set by the http_proxy environment variable.
 
239
                 *
 
240
                 * @param ProxyPort Proxy port to use
 
241
                 */
 
242
 
 
243
                void SetProxyPort(int ProxyPort);
 
244
 
 
245
                /**
 
246
                 * @brief Set proxy server user name
 
247
                 *
 
248
                 * Set the user name to use when authenticating to the proxy server. @b Note The http_proxy
 
249
                 * environment variable will be used to set a 'default' proxy server. Calls to this method
 
250
                 * will override any proxy settings set by the http_proxy environment variable.
 
251
                 *
 
252
                 * @param ProxyUserName Proxy user name to use
 
253
                 */
 
254
 
 
255
                void SetProxyUserName(const std::string& ProxyUserName);
 
256
 
 
257
                /**
 
258
                 * @brief Set proxy server password
 
259
                 *
 
260
                 * Set the password to use when authenticating to the proxy server. @b Note The http_proxy
 
261
                 * environment variable will be used to set a 'default' proxy server. Calls to this method
 
262
                 * will override any proxy settings set by the http_proxy environment variable.
 
263
                 *
 
264
                 * @param ProxyPassword Proxy password to use
 
265
                 */
 
266
 
 
267
                void SetProxyPassword(const std::string& ProxyPassword);
 
268
 
 
269
                /**
 
270
                 * @brief Return a list of releases that match a disc ID
 
271
                 *
 
272
                 * Request a list of releases matching the specified disc ID.
 
273
                 *
 
274
                 * @param DiscID Disc id to match
 
275
                 *
 
276
                 * @return MusicBrainz4::CReleaseList
 
277
                 */
 
278
 
 
279
                CReleaseList LookupDiscID(const std::string& DiscID);
 
280
 
 
281
                /**
 
282
                 * @brief Return full information about a release
 
283
                 *
 
284
                 * Query for detailed information about a specific release
 
285
                 *
 
286
                 * @param ReleaseID MusicBrainz release ID to lookup
 
287
                 *
 
288
                 * @return MusicBrainz::CRelease object
 
289
                 *
 
290
                 * @throw CConnectionError An error occurred connecting to the web server
 
291
                 * @throw CTimeoutError A timeout occurred when connecting to the web server
 
292
                 * @throw CAuthenticationError An authentication error occurred
 
293
                 * @throw CFetchError An error occurred fetching data
 
294
                 * @throw CRequestError The request was invalid
 
295
                 * @throw CResourceNotFoundError The requested resource was not found
 
296
                 */
 
297
 
 
298
                CRelease LookupRelease(const std::string& ReleaseID);
 
299
 
 
300
                /**
 
301
                 * @brief Perform a generic query
 
302
                 *
 
303
                 * Performs a generic query.
 
304
                 *
 
305
                 * Assuming the following parameters are set:
 
306
                 *
 
307
                 * "param1" = "p1v1 p1v2 p1v3"<br>
 
308
                 * "param2" = "p2v1"<br>
 
309
                 * "param3" = ""<br>
 
310
                 *
 
311
                 * The following query will be generated:
 
312
                 *
 
313
                 * /ws/2/Entity/ID/Resource?param1=p1v1+p1v2+p1v3&param2=p2v1&param3
 
314
                 *
 
315
                 * If any of ID or Resource are empty, those components will be omitted from the query.
 
316
                 *
 
317
                 * For full details about generating queries, see the
 
318
                 * <a target="_blank" href="http://musicbrainz.org/doc/XML_Web_Service/Version_2">web service</a>
 
319
                 * documentation.
 
320
                 *
 
321
                 * @param Entity Entity to lookup (e.g. artist, release, discid)
 
322
                 * @param ID The MusicBrainz ID of the entity
 
323
                 * @param Resource The resource (currently only used for collections)
 
324
                 * @param Params Map of parameters to add to the query (e.g. inc)
 
325
                 *
 
326
                 * @return MusicBrainz4::CMetadata object
 
327
                 *
 
328
                 * @throw CConnectionError An error occurred connecting to the web server
 
329
                 * @throw CTimeoutError A timeout occurred when connecting to the web server
 
330
                 * @throw CAuthenticationError An authentication error occurred
 
331
                 * @throw CFetchError An error occurred fetching data
 
332
                 * @throw CRequestError The request was invalid
 
333
                 * @throw CResourceNotFoundError The requested resource was not found
 
334
                 */
 
335
 
 
336
                CMetadata Query(const std::string& Entity,const std::string& ID="",const std::string& Resource="",const tParamMap& Params=tParamMap());
 
337
 
 
338
                /**
 
339
                 * @brief Add entries to the specified collection
 
340
                 *
 
341
                 * Add a list of releases to the specified collection.
 
342
                 *
 
343
                 * @param CollectionID The MusicBrainz ID of the collection to add entries to
 
344
                 * @param Entries List of MusicBrainz Release IDs to add to the collection
 
345
                 *
 
346
                 * @return true if successful, false otherwise
 
347
                 *
 
348
                 * @throw CConnectionError An error occurred connecting to the web server
 
349
                 * @throw CTimeoutError A timeout occurred when connecting to the web server
 
350
                 * @throw CAuthenticationError An authentication error occurred
 
351
                 * @throw CFetchError An error occurred fetching data
 
352
                 * @throw CRequestError The request was invalid
 
353
                 * @throw CResourceNotFoundError The requested resource was not found
 
354
                 */
 
355
 
 
356
                bool AddCollectionEntries(const std::string& CollectionID, const std::vector<std::string>& Entries);
 
357
 
 
358
                /**
 
359
                 * @brief Delete entries from the specified collection
 
360
                 *
 
361
                 * Delete a list of releases from the specified collection.
 
362
                 *
 
363
                 * @param CollectionID The MusicBrainz ID of the collection to delete entries from
 
364
                 * @param Entries List of MusicBrainz Release IDs to delete from the collection
 
365
                 *
 
366
                 * @return true if successful, false otherwise
 
367
                 *
 
368
                 * @throw CConnectionError An error occurred connecting to the web server
 
369
                 * @throw CTimeoutError A timeout occurred when connecting to the web server
 
370
                 * @throw CAuthenticationError An authentication error occurred
 
371
                 * @throw CFetchError An error occurred fetching data
 
372
                 * @throw CRequestError The request was invalid
 
373
                 * @throw CResourceNotFoundError The requested resource was not found
 
374
                 */
 
375
 
 
376
                bool DeleteCollectionEntries(const std::string& CollectionID, const std::vector<std::string>& Entries);
 
377
 
 
378
                /**
 
379
                 * @brief Return result of the last query
 
380
                 *
 
381
                 * Return the result of the last query
 
382
                 *
 
383
                 * @return Result of last query
 
384
                 */
 
385
 
 
386
                CQuery::tQueryResult LastResult() const;
 
387
 
 
388
                /**
 
389
                 * @brief Return HTTP code of the last query
 
390
                 *
 
391
                 * Return the HTTP code of the last query
 
392
                 *
 
393
                 * @return HTTP code of last query
 
394
                 */
 
395
                int LastHTTPCode() const;
 
396
 
 
397
                /**
 
398
                 * @brief Return error message from the last query
 
399
                 *
 
400
                 * Return the error message from the last query
 
401
                 *
 
402
                 * @return Error message from last query
 
403
                 */
 
404
                std::string LastErrorMessage() const;
 
405
 
 
406
                /**
 
407
                 * @brief Return the library version
 
408
                 *
 
409
                 * Return the library version
 
410
                 *
 
411
                 * @return Library version
 
412
                 */
 
413
                std::string Version() const;
 
414
 
 
415
        private:
 
416
                CQueryPrivate * const m_d;
 
417
 
 
418
                CMetadata PerformQuery(const std::string& Query);
 
419
                void WaitRequest() const;
 
420
                std::string UserAgent() const;
 
421
                bool EditCollection(const std::string& CollectionID, const std::vector<std::string>& Entries, const std::string& Action);
 
422
                std::string URIEscape(const std::string& URI);
 
423
                std::string URLEncode(const std::map<std::string,std::string>& Params);
 
424
        };
 
425
}
 
426
 
 
427
#endif