forked from ScottIsAFool/StoreClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreApiClient.cs
More file actions
582 lines (464 loc) · 22.9 KB
/
StoreApiClient.cs
File metadata and controls
582 lines (464 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Serialization;
using AdvancedREI.Net.Http.Compression;
using StoreClient.Entities;
using StoreClient.Entities.Zune;
namespace StoreClient
{
public class StoreApiClient : IStoreApiClient
{
private const string DefaultLocale = "en-US";
/// <summary>
/// Gets the HTTP client.
/// </summary>
/// <value>
/// The HTTP client.
/// </value>
public HttpClient HttpClient { get; private set; }
/// <summary>
/// Gets or sets the locale.
/// </summary>
/// <value>
/// The locale.
/// </value>
public string Locale { get; set; }
/// <summary>
/// Gets or sets which Windows Phone store to search against (either WP7 or WP8). Defaults to WP8.
/// </summary>
/// <value>
/// The windows phone store.
/// </value>
public Store WindowsPhoneStore { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="StoreApiClient" /> class.
/// </summary>
/// <param name="handler">The handler.</param>
public StoreApiClient(HttpMessageHandler handler)
{
HttpClient = new HttpClient(handler);
Locale = DefaultLocale;
}
/// <summary>
/// Initializes a new instance of the <see cref="StoreApiClient" /> class with default HttpHandler using compression
/// </summary>
public StoreApiClient()
{
//HttpClient = new HttpClient(new CompressedHttpClientHandler());
HttpClient = new HttpClient();
Locale = DefaultLocale;
}
/// <summary>
/// Searches the async.
/// </summary>
/// <param name="searchQuery">The search query.</param>
/// <param name="locale">sets the locale to do the search on. If null or empty, it uses what's set for Locale</param>
/// <param name="includeAlbums">if set to <c>true</c> [include albums].</param>
/// <param name="includeArtists">if set to <c>true</c> [include artists].</param>
/// <param name="includeTracks">if set to <c>true</c> [include tracks].</param>
/// <param name="includePodcasts">if set to <c>true</c> [include podcasts].</param>
/// <param name="includeApps">if set to <c>true</c> [include apps].</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">SearchQuery cannot be null or empty</exception>
public async Task<SearchResult> SearchAsync(string searchQuery, string locale = null, bool includeAlbums = true, bool includeArtists = true, bool includeTracks = true, bool includePodcasts = true, bool includeApps = true)
{
if (string.IsNullOrEmpty(searchQuery))
{
throw new NullReferenceException("SearchQuery cannot be null or empty");
}
string url;
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var searchResult = new SearchResult();
if (includeAlbums)
{
url = string.Format(Constants.SearchUrlMusicFormatSecondary, locale, "album", searchQuery);
var alXml = await HttpClient.GetStringAsync(url);
var alResults = ParseXml<ZuneAlbumSearch.feed>(alXml);
if (alResults.entry != null)
{
foreach (var result in alResults.entry)
{
searchResult.Albums.Add(new Album(result));
}
}
}
if (includeArtists)
{
url = string.Format(Constants.SearchUrlMusicFormatSecondary, locale, "artist", searchQuery);
var arXml = await HttpClient.GetStringAsync(url);
var arResults = ParseXml<ZuneArtistSearch.feed>(arXml);
if (arResults.entry != null)
{
foreach (var result in arResults.entry)
{
searchResult.Artists.Add(new Artist(result));
}
}
}
if (includeTracks)
{
url = string.Format(Constants.SearchUrlMusicFormatSecondary, locale, "track", searchQuery);
var trXml = await HttpClient.GetStringAsync(url);
var trResults = ParseXml<ZuneTrack.feed>(trXml);
if (trResults.entry != null)
{
foreach (var result in trResults.entry)
{
searchResult.Tracks.Add(new Track(result));
}
}
}
if (includeApps)
{
var storeFormat = WindowsPhoneStore == Store.WindowsPhone8 ? Constants.SearchUrlAppFormat : Constants.SearchUrlAppFormat7;
url = string.Format(storeFormat, locale, Uri.EscapeUriString(searchQuery));
var apXml = await HttpClient.GetStringAsync(url);
var apResults = ParseXml<ZuneAppSearch.feed>(apXml);
if (apResults.entry != null)
{
foreach (var result in apResults.entry)
{
searchResult.StoreApps.Add(new StoreApp(result));
}
}
}
return searchResult;
}
/// <summary>
/// Gets the artist info async.
/// </summary>
/// <param name="artistId">The artist id.</param>
/// <param name="locale">sets the locale to do the search on. If null or empty, it uses what's set for Locale</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">Artist Id cannot be null or empty</exception>
public async Task<Artist> GetArtistInfoAsync(string artistId, string locale = null)
{
if (string.IsNullOrEmpty(artistId))
{
throw new NullReferenceException("Artist Id cannot be null or empty");
}
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
// Get the main artist information
var url = string.Format(Constants.ArtistUrlFormat, locale, artistId, "");
var xml = await HttpClient.GetStringAsync(url);
var mainInfoResult = ParseXml<ZuneArtistSearch.feedEntry>(xml);
var result = new Artist(mainInfoResult);
// Now get the biography information
url += "biography";
xml = await HttpClient.GetStringAsync(url);
var biographyResult = ParseXml<ZuneArtist.entry>(xml);
result.Content = biographyResult.content.Value;
return result;
}
/// <summary>
/// Gets the albums for artist async.
/// </summary>
/// <param name="artistId">The artist id.</param>
/// <param name="locale">sets the locale to do the search on. If null or empty, it uses what's set for Locale</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">Artist Id cannot be null or empty</exception>
public async Task<AlbumCollection> GetAlbumsForArtistAsync(string artistId, string locale = null)
{
if (string.IsNullOrEmpty(artistId))
{
throw new NullReferenceException("Artist Id cannot be null or empty");
}
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var url = string.Format(Constants.ArtistUrlFormat, locale, artistId, "albums");
var xml = await HttpClient.GetStringAsync(url);
var result = ParseXml<ZuneAlbumSearch.albumFeed>(xml);
var returnList = new AlbumCollection(result.entry.Select(r => new Album(r)).ToList());
return returnList;
}
/// <summary>
/// Gets the album info async.
/// </summary>
/// <param name="albumId">The album id.</param>
/// <param name="locale">sets the locale to do the search on. If null or empty, it uses what's set for Locale</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">Album Id cannot be null or empty</exception>
public async Task<Album> GetAlbumInfoAsync(string albumId, string locale = null)
{
if (string.IsNullOrEmpty(albumId))
{
throw new NullReferenceException("Album Id cannot be null or empty");
}
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var url = string.Format(Constants.AlbumUrlFormat, locale, albumId);
var xml = await HttpClient.GetStringAsync(url);
var result = ParseXml<ZuneAlbum.feed>(xml);
return new Album(result);
}
/// <summary>
/// Gets the apps list async.
/// </summary>
/// <param name="searchBy">The search by.</param>
/// <param name="costType">Type of the cost.</param>
/// <param name="includeGames">if set to <c>true</c> [include games].</param>
/// <param name="categoryId">The category id.</param>
/// <param name="pageNumber">The page number.</param>
/// <param name="locale">The locale.</param>
/// <returns></returns>
public async Task<StoreAppCollection> GetAppsListAsync(SearchBy searchBy = SearchBy.Popular,
CostType costType = CostType.Free,
bool includeGames = false,
string categoryId = null,
//int maxNumberOfAppsToReturn = 20,
int pageNumber = 1,
string locale = null)
{
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
if (costType == CostType.Free || costType == CostType.Paid || costType==CostType.FreeAndPaid)
{
var url = CreateAppListUrl(searchBy, costType, includeGames, pageNumber, locale, categoryId);
var xml = await HttpClient.GetStringAsync(url);
var storeResults = ParseXml<ZuneAppSearch.appListFeed>(xml);
var results = new StoreAppCollection(storeResults.entry.Select(x => new StoreApp(x)).ToList());
results.ProcessLinks(storeResults.link);
return results;
}
else
{
var url = CreateAppListUrl(searchBy, CostType.Free, includeGames, pageNumber, locale);
var xml = await HttpClient.GetStringAsync(url);
var storeResults = ParseXml<ZuneAppSearch.appListFeed>(xml);
var results = new StoreAppCollection(storeResults.entry.Select(x => new StoreApp(x)).ToList());
url = CreateAppListUrl(searchBy, CostType.Paid, includeGames, pageNumber, locale);
xml = await HttpClient.GetStringAsync(url);
storeResults = ParseXml<ZuneAppSearch.appListFeed>(xml);
results.AddRange(storeResults.entry.Select(x => new StoreApp(x)));
return results;
}
}
/// <summary>
/// Gets the app categories async.
/// </summary>
/// <param name="locale">The locale.</param>
/// <returns></returns>
public async Task<CategoryCollection> GetAppCategoriesAsync(string locale = null)
{
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var url = string.Format(Constants.CategoryListUrlFormat, locale);
var xml = await HttpClient.GetStringAsync(url);
var results = ParseXml<ZuneCategories.feed>(xml);
if (results.entry == null) return new CategoryCollection(new List<Category>());
var returnList = new CategoryCollection(results.entry.Select(x => new Category(x)).ToList());
return returnList;
}
/// <summary>
/// Gets the music genres async.
/// </summary>
/// <param name="locale">The locale.</param>
/// <returns></returns>
public async Task<CategoryCollection> GetMusicGenresAsync(string locale = null)
{
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var url = string.Format(Constants.GenreListUrlFormat, locale);
var xml = await HttpClient.GetStringAsync(url);
var result = ParseXml<ZuneCategories.feed>(xml);
if (result.entry == null) return new CategoryCollection(new List<Category>());
var resultList = new CategoryCollection(result.entry.Select(x => new Category(x)).ToList());
return resultList;
}
/// <summary>
/// Gets the albums by genre.
/// </summary>
/// <param name="genreId">The genre id.</param>
/// <param name="musicSearchBy">The music search by.</param>
/// <param name="marker">The next/previous marker to get more items</param>
/// <param name="locale">The locale.</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">Genre Id cannot be null or empty</exception>
public async Task<AlbumCollection> GetAlbumsByGenreAsync(string genreId, MusicSearchBy musicSearchBy = MusicSearchBy.SalesRank, string marker = "afterMarker=CgAAAA%3d%3d", string locale = null)
{
if (string.IsNullOrEmpty(genreId))
{
throw new NullReferenceException("Genre Id cannot be null or empty");
}
//http://catalog.zune.net/v3.2/{0}/music/genre/{1}/{2}?orderby={3}&chunksize=10
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var url = string.Format(Constants.ItemsByGenreUrlFormat, locale, genreId, "albums", musicSearchBy.ToString(), marker);
var xml = await HttpClient.GetStringAsync(url);
var result = ParseXml<ZuneAlbumSearch.feed>(xml);
if (result.entry == null) return new AlbumCollection(new List<Album>());
var resultList = new AlbumCollection(result.entry.Select(x => new Album(x)).ToList());
resultList.ProcessLinks(result.link);
return resultList;
}
/// <summary>
/// Gets the artists by genre.
/// </summary>
/// <param name="genreId">The genre id.</param>
/// <param name="musicSearchBy">The music search by.</param>
/// <param name="marker">The next/previous marker to get next/previous items</param>
/// <param name="locale">The locale.</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">Genre Id cannot be null or empty</exception>
public async Task<ArtistCollection> GetArtistsByGenreAsync(string genreId, MusicSearchBy musicSearchBy = MusicSearchBy.SalesRank, string marker = "afterMarker=CgAAAA%3d%3d", string locale = null)
{
if (string.IsNullOrEmpty(genreId))
{
throw new NullReferenceException("Genre Id cannot be null or empty");
}
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var url = string.Format(Constants.ItemsByGenreUrlFormat, locale, genreId, "artists", musicSearchBy.ToString(), marker);
var xml = await HttpClient.GetStringAsync(url);
var result = ParseXml<ZuneArtistSearch.feed>(xml);
if (result.entry == null) return new ArtistCollection(new List<Artist>());
var resultList = new ArtistCollection(result.entry.Select(x => new Artist(x)).ToList());
resultList.ProcessLinks(result.link);
return resultList;
}
/// <summary>
/// Gets the tracks by genre.
/// </summary>
/// <param name="genreId">The genre id.</param>
/// <param name="musicSearchBy">The music search by.</param>
/// <param name="marker">The next/previous marker to get next/previous items</param>
/// <param name="locale">The locale.</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">Genre Id cannot be null or empty</exception>
public async Task<TrackCollection> GetTracksByGenreAsync(string genreId, MusicSearchBy musicSearchBy = MusicSearchBy.SalesRank, string marker = "afterMarker=CgAAAA%3d%3d", string locale = null)
{
if (string.IsNullOrEmpty(genreId))
{
throw new NullReferenceException("Genre Id cannot be null or empty");
}
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var url = string.Format(Constants.ItemsByGenreUrlFormat, locale, genreId, "tracks", musicSearchBy.ToString(), marker);
var xml = await HttpClient.GetStringAsync(url);
var result = ParseXml<ZuneTrackSearch.feed>(xml);
if (result.entry == null) return new TrackCollection(new List<Track>());
var resultList = new TrackCollection(result.entry.Select(x => new Track(x)).ToList());
resultList.ProcessLinks(result.link);
return resultList;
}
/// <summary>
/// Gets the app info async.
/// </summary>
/// <param name="appId">The app id.</param>
/// <param name="locale">sets the locale to do the search on. If null or empty, it uses what's set for Locale</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">Album Id cannot be null or empty</exception>
public async Task<StoreApp> GetAppInfoAsync(string appId, string locale = null)
{
if (string.IsNullOrEmpty(appId))
{
throw new NullReferenceException("Album Id cannot be null or empty");
}
locale = string.IsNullOrEmpty(locale) ? Locale : locale;
var storeFormat = WindowsPhoneStore == Store.WindowsPhone8 ? Constants.AppUrlFormat : Constants.AppUrlFormat7;
var url = string.Format(storeFormat, appId, locale);
var xml = await HttpClient.GetStringAsync(url);
var result = ParseXml<ZuneApp.feed>(xml);
return new StoreApp(result);
}
/// <summary>
/// Creates the artist's background URL.
/// </summary>
/// <param name="artist">The artist.</param>
/// <param name="screenSize">Size of the screen.</param>
/// <returns></returns>
public string CreateArtistBackgroundUrl(Artist artist, ScreenSize screenSize)
{
return CreateArtistBackgroundUrl(artist.Id, screenSize);
}
/// <summary>
/// Creates the artist's background URL.
/// </summary>
/// <param name="artistId">The artist id.</param>
/// <param name="screenSize">Size of the screen.</param>
/// <returns></returns>
public string CreateArtistBackgroundUrl(string artistId, ScreenSize screenSize)
{
return string.Format(Constants.ArtistBackgroundUrlFormat, Locale, artistId, screenSize.ToEnumString());
}
/// <summary>
/// Creates the album art URL.
/// </summary>
/// <param name="album">The album.</param>
/// <returns></returns>
public string CreateAlbumArtUrl(Album album)
{
return CreateAlbumArtUrl(album.ImageId);
}
/// <summary>
/// Creates the album art URL.
/// </summary>
/// <param name="albumImageId">The album image id.</param>
/// <returns></returns>
public string CreateAlbumArtUrl(string albumImageId)
{
return string.Format(Constants.AlbumArtUrlFormat, Locale, albumImageId);
}
/// <summary>
/// Creates the app image URL.
/// </summary>
/// <param name="imageId">The image id.</param>
/// <param name="imageType">The image type.</param>
/// <returns></returns>
public string CreateAppImageUrl(string imageId, ImageType imageType)
{
var formatType = "";
switch (imageType)
{
case ImageType.IconLarge:
formatType = "icon_large";
break;
case ImageType.IconSmall:
formatType = "icon_small";
break;
case ImageType.Screenshot:
formatType = "screenshot";
break;
case ImageType.ScreenshotThumbnail:
formatType = "screenshot_thumbnail";
break;
case ImageType.BackgroundArt:
formatType = "background_art";
break;
case ImageType.WindowsPhoneStoreMedium:
formatType = "ws_icon_large";
break;
case ImageType.WindowsPhoneStoreSmall:
formatType = "ws_icon_small";
break;
case ImageType.WindowsPhoneStoreLarge:
formatType = "ws_icon_medium"; // For some reason, the medium image is bigger than the large image. Meh.
break;
}
return string.Format(Constants.ScreenshotUrlFormat, imageId, formatType);
}
private string CreateAppListUrl(SearchBy searchBy,
CostType costType,
bool includeGames = false,
//int maxNumberOfAppsToReturn = 20,
int pageNumber = 1,
string locale = null,
string categoryId = null)
{
var storeFormat = WindowsPhoneStore == Store.WindowsPhone8 ? Constants.AppListUrlFormat : Constants.AppListUrlFormat7;
var url = string.Format(storeFormat,
pageNumber * 10,
locale,
includeGames ? "" : "windowsphone.games",
searchBy.ToString(),
costType.ToString());
if (!string.IsNullOrEmpty(categoryId)) url += string.Format("&category={0}", categoryId);
return url;
}
internal static T ParseXml<T>(string xml)
{
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(xml))
{
var result = (T)serializer.Deserialize(reader);
return result;
}
}
}
}