-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsosShop.cs
176 lines (152 loc) · 6.77 KB
/
AsosShop.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Asos.Mobile.Domain.Browsing.Api;
using Asos.Mobile.Domain.Models.Product;
using Asos.Mobile.Domain.Properties;
using Newtonsoft.Json;
namespace Asos.Mobile.Domain.Browsing
{
/// <summary>
/// Defines the implementation fo the Asos shop against Asos' Api interface as defined http://developer.asos.com
/// </summary>
public class AsosShop : ShopBase
{
/// <summary>
/// Asos Uri Constant to get product detail by querying a specific product Id
/// {0} = Inventory Id
/// {1} = Locale
/// {2} = Currency
/// </summary>
public const string AsosBaseProductInformationUri = "http://api1.asos.com/product/{0}/{1}/{2}";
/// <summary>
/// Asos Uri Constant to list a collection of products>
/// <remarks>http://developer.asos.com/docs/read/scenarios/category_and_search/Get_products_for_a_particular_category</remarks>
/// </summary>
public const string AsosBaseProductListingUri = "http://api1.asos.com/productlisting/category/{0}/{1}/{2}/{3}/{4}";
/// <summary>
/// Get child categories for a given caregory or floor id.
/// <remarks>http://developer.asos.com/docs/read/scenarios/category_and_search/Get_subcategories_for_a_category_or_floor</remarks>
/// </summary>
public const string AsosBaseSubCategoryUri = "http://api1.asos.com/subcategorylisting/{0}/{1}/{2}";
/// <summary>
/// Asos Uri Constant to list a collection of products using a search term
/// {0} = Search Term
/// {1} = Page Number
/// {2} = Sort By
/// {3} = Locale
/// {4} = Currency
/// <remarks>http://developer.asos.com/docs/read/scenarios/category_and_search/Get_products_using_search</remarks>
/// </summary>
public const string AsosBaseProductSearchUri = "http://api1.asos.com/productlisting/search/{0}/{1}/{2}/{3}/{4}";
/// <summary>
/// Add the App Specific Key to the Uri required for the request
/// </summary>
public const string AsosKeyParameter = "?api_key={0}";
/// <summary>
/// Build the refinement options if required for the product query
/// </summary>
public const string AsosRefineOptions = "?op={5}";
protected string ProductJsonData = string.Empty;
/// <summary>
/// Gets or sets the product.
/// </summary>
/// <value>The product.</value>
public RootObject Product { get; set; }
/// <summary>
/// Gets or sets the product query details.
/// </summary>
/// <value>The product query details.</value>
public List<RootObject> Products { get; set; }
string AddAsosKeyParameter()
{
return string.Format(AsosKeyParameter, ApplicationKeys.ApiKey);
}
string BuildAsosProductQueryByIdUri(string inventoryId)
{
return string.Format(AsosBaseProductInformationUri, inventoryId, ApplicationKeys.EnglishLocale,
Currencies.GBP) + AddAsosKeyParameter();
}
string BuildAsosProductQueryBySearchTerm(string searchTerm)
{
return string.Format(AsosBaseProductSearchUri, searchTerm, 1, ProductResultsSortBy.none, ApplicationKeys.EnglishLocale,
Currencies.GBP) + AddAsosKeyParameter(); //TODO Add Sort By and Refinement Options to Domain
}
/// <summary>
/// Get product information for display to the end user from Asos' Api gateway
/// </summary>
/// <param name="searchParam"></param>
/// <param name="queryType"></param>
/// <remarks>http://developer.asos.com/docs/read/scenarios/category_and_search/Get_products_using_search</remarks>
/// <remarks>http://developer.asos.com/docs/read/scenarios/category_and_search/Get_products_for_a_particular_category</remarks>
/// <remarks>http://developer.asos.com/docs/read/scenarios/product/Get_information_about_a_product</remarks>
/// <returns></returns>
public override void GetProducts(string searchParam, ProductQueryType queryType)
{
switch (queryType)
{
case ProductQueryType.ProductByIdQuery:
GetProductById(searchParam);
break;
case ProductQueryType.ProductBySearchTerm:
GetProductsBySearchTerm(searchParam);
break;
case ProductQueryType.ProductByCategory:
GetProductsByCategory(searchParam);
break;
}
}
public override void GetProductInformation(string productId)
{
GetProductById(productId);
}
private void GetProductById(string productId)
{
RetrievedDataSuccessfully = false;
//Intiate new HTTP Web Request to return to non UI Thread
var request = WebRequest.Create(BuildAsosProductQueryByIdUri(productId));
request.BeginGetResponse(ProductCallback, request);
}
private void GetProductsBySearchTerm(string searchTerm)
{
RetrievedDataSuccessfully = false;
//Intiate new HTTP Web Request to return to non UI Thread
var request = WebRequest.Create(BuildAsosProductQueryBySearchTerm(searchTerm));
request.BeginGetResponse(ProductCallback, request);
}
private void GetProductsByCategory(string searchTerm)
{
RetrievedDataSuccessfully = false;
//Intiate new HTTP Web Request to return to non UI Thread
var request = WebRequest.Create(BuildAsosProductQueryBySearchTerm(searchTerm));
request.BeginGetResponse(ProductCallback, request);
}
private void ProductCallback(IAsyncResult result)
{
try
{
var request = (HttpWebRequest)result.AsyncState;
var response = request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
ProductJsonData = reader.ReadToEnd();
}
Product = BuildProductData(ProductJsonData);
RetrievedDataSuccessfully = true;
}
finally
{
RaiseCompletedCallEvent();
}
}
RootObject BuildProductData(string data)
{
//Check data is not null
if (data == null)
throw new ArgumentException(Resource.NullJsonData);
return JsonConvert.DeserializeObject<RootObject>(data);
}
}
}