This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
/
README
362 lines (264 loc) · 11.3 KB
/
README
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
# semantics3-csharp
semantics3-csharp is a C# client for accessing the Semantics3 Products API, which provides structured information, including pricing histories, for a large number of products.
See https://www.semantics3.com for more information.
API documentation can be found at https://www.semantics3.com/docs/
## Installation
semantics3-csharp can be installed through the Nuget. Nuget: https://www.nuget.org/packages/Semantics3/.
Target Frameworks for this library build are .NET Framework 4.0 and .NET Framework 4.5. Change your Target Framework from .NET Framework 4.0 (or 4.5) Client Profile to just .NET Framework 4.0 (or 4.5) in your project properties if need to.
```bash
$ Install-Package Semantics3
```
To build and install from the latest source:
```bash
$ git clone git@github.com:Semantics3/semantics3-csharp.git
```
## Requirements
* Google.GData.Client >= 2.2.2.0
* Newtonsoft.Json >= 5.0.6
* .NET Framework >= 4.0
## Getting Started
In order to use the client, you must have both an API key and an API secret. To obtain your key
and secret, you need to first create an account at
https://www.semantics3.com/
You can access your API access credentials from the user dashboard at
https://www.semantics3.com/dashboard/applications
### Setup Work
Let's lay the groundwork.
```c#
using Semantics3;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Products products = new Products("APIKEY",
"APISECRET");
}
}
}
```
### First Query aka 'Hello World':
Let's run our first request! We are going to run a simple search fo the word "iPhone" as follows:
```c#
// Build the query
products.products_field( "search", "iphone" );
// Make the query
JObject hashProducts = products.get_products();
// View the results of the query
Console.Write(hashProducts.ToString());
```
## Sample Requests
The following requests show you how to interface with some of the core functionality of the
Semantics3 Products API:
### Pagination
The example in our "Hello World" script returns the first 10 results. In this example, we'll scroll to subsequent pages, beyond our initial request:
```c#
// Build the query
products.products_field( "search", "iphone" );
// Make the query
JObject hashProducts = products.get_products();
// View the results of the query
Console.Write(hashProducts.ToString());
// Goto the next 'page'
int page = 1;
JObject nextProducts = products.iterate_products();
while( nextProducts != null )
{
Console.Write("We are at page"); Console.Write(page);
Console.Write(nextProducts.ToString());
page++;
nextProducts = products.iterate_products();
}
```
### URL Query
Get the picture? You can run URL queries as follows:
```c#
// Build the query
products.products_field( "url", "http://www.walmart.com/ip/15833173" );
// Make the query
JObject hashProducts = products.get_products();
// View the results of the query
Console.Write(hashProducts.ToString());
```
### Price Filter
Filter by price using the "lt" (less than) tag:
```c#
// Build the query
products.products_field( "search", "iphone" );
products.products_field( "price", "lt", 300 );
// Make the query
JObject hashProducts = products.get_products();
// View the results of the query
Console.Write(hashProducts.ToString());
```
### Category ID Query
To lookup details about a cat_id, run your request against the categories resource:
```c#
// Build the query
products.categories_field( "cat_id", 4992 );
// Make the query
JObject hashCategories = products.get_categories();
// View the results of the query
Console.Write(hashCategories.ToString());
```
### Parse JSON/Response Example
The code below shows how to use the JObject (from Newtonsoft.Json library) value returned from the Semantics3 API to access the data fields interested in. The JSON response from the API is already parsed into the Newtonsoft.Json Object ready to be consumed easily in your target application.
```c#
// Query the API
JObject hashResponse = products.get_products();
if ((int)hashResponse["results_count"] > 0)
{
JArray arrayProducts = (JArray)hashResponse["results"];
// For each product in the results
for (int i = 0; i < arrayProducts.Count; i++)
{
String productTitle = (String)arrayProducts[i]["name"];
List<String> images = new List<string>();
for (int j = 0; j < ((JArray)arrayProducts[i]["images"]).Count; j++)
{
images.Add((String)arrayProducts[i]["images"][j]);
}
// Pricing Information
JArray ecommerceStores = (JArray)arrayProducts[i]["sitedetails"];
for (int k = 0; k < ecommerceStores.Count; k++)
{
String skuInStore = (String)ecommerceStores[k]["sku"];
String productUrlInStore = (String)ecommerceStores[k]["url"];
// Latest offers from the ecommerceStore
for (int s = 0; s < ((JArray)ecommerceStores[k]["latestoffers"]).Count; s++)
{
String sellerName = (String)ecommerceStores[k]["latestoffers"][s]["seller"];
String price = (String)ecommerceStores[k]["latestoffers"][s]["price"];
String currency = (String)ecommerceStores[k]["latestoffers"][s]["currency"];
String availability = (String)ecommerceStores[k]["latestoffers"][s]["availability"];
// Preprocess and use in application ...
}
// Parsed offers
}
// Loop Ecommerse stores
}
// Loop each product in the api response
}
// If the above query resulted in some results
```
### VB .NET
Install this library through Nuget package manager using the instructions above. After installing the library in your project (Make sure your target framework is .NET 4 (or later) and NOT .NET 4(or later) Client Framework , can be changed in Properties->Compile->Advanced Compile Options->Target Framework), here's a sample VB .NET code on using the API. All of the above library functions work the same in VB as the Semantics3 API C# library is CLS Compliant.
```vb
Imports Semantics3
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Module Module1
Sub Main()
Dim products As Products = New Products("API_KEY", "API_SECRET")
' Build the query
products.products_field("search", "iphone")
Dim constructedJson As String = products.get_query_json("products")
Console.Write(constructedJson)
' Make the query
Dim apiResponse As JObject = products.get_products()
Console.Write(apiResponse.ToString())
End Sub
End Module
```
## Webhooks
You can use webhooks to get near-real-time price updates from Semantics3.
### Creating a webhook
You can register a webhook with Semantics3 by sending a POST request to `"webhooks"` endpoint.
To verify that your URL is active, a GET request will be sent to your server with a `verification_code` parameter. Your server should respond with `verification_code` in the response body to complete the verification process.
If your webhook server does not respond to as indicated in the Semantics3 Webhooks documentation, the run_query
call will crash. You'll have to surround them in a try catch block.
```c#
JObject addWebhookUriQuery = new JObject();
addWebhookUriQuery["webhook_uri"] = "http://mydomain.com/myendpoint";
JObject hashResponse = products.run_query("webhooks", addWebhookUriQuery, "POST");
Console.Write(hashResponse.ToString());
```
To fetch existing webhooks
```c#
JObject hashResponse = products.run_query("webhooks", "{}", "GET");
Console.Write(hashResponse.ToString());
```
To remove a webhook
```c#
String webhookId = "7JcGN81u";
String endpoint = "webhooks/" + webhookId;
JObject hashResponse = products.run_query( endpoint, "{}", "DELETE" );
Console.Write(hashResponse.ToString());
```
### Registering events
Once you register a webhook, you can start adding events to it. Semantics3 server will send you notifications when these events occur.
To register events for a specific webhook send a POST request to the `"webhooks/{webhook_id}/events"` endpoint
```c#
JObject productSpec = new JObject();
productSpec["sem3_id"] = "1QZC8wchX62eCYS2CACmka";
JObject constraintsSpec = new JObject();
constraintsSpec["gte"] = 10;
constraintsSpec["lte"] = 100;
JObject createEventsQuery = new JObject();
createEventsQuery["type"] = "price.change";
createEventsQuery["product"] = productSpec;
createEventsQuery["constraints"] = constraintsSpec;
String webhookId = "7JcGN81u";
String endpoint = "webhooks/" + webhookId + "/events";
JObject hashResponse = products.run_query( endpoint, createEventsQuery, "POST" );
Console.Write(hashResponse.ToString());
```
To fetch all registered events for a give webhook
```c#
String webhookId = "7JcGN81u";
String endpoint = "webhooks/" + webhookId + "/events";
JObject hashResponse = products.run_query(endpoint, "{}", "GET");
Console.Write(hashResponse.ToString());
```
### Webhook Notifications
Once you have created a webhook and registered events on it, notifications will be sent to your registered webhook URI via a POST request when the corresponding events occur. Make sure that your server can accept POST requests. Here is how a sample notification object looks like
```javascript
{
"type": "price.change",
"event_id": "XyZgOZ5q",
"notification_id": "X4jsdDsW",
"changes": [{
"site": "abc.com",
"url": "http://www.abc.com/def",
"previous_price": 45.50,
"current_price": 41.00
}, {
"site": "walmart.com",
"url": "http://www.walmart.com/ip/20671263",
"previous_price": 34.00,
"current_price": 42.00
}]
}
```
### Additional utility methods
| method | Description
| ------------- |:-------------
| `products.get_results_json()` | returns the result json string from the previous query
| `products.clear()` | clears all the fields in the query
| `products.run_query(endpoint, rawJson, method, callback)` | You can use this method to send raw JSON string in the request
## Contributing
Use GitHub's standard fork/commit/pull-request cycle. If you have any questions, email <support@semantics3.com>.
## Author
* Srinivasan Kidambi <srinivas@semantics3.com>
## Copyright
Copyright (c) 2014 Semantics3 Inc.
## License
The "MIT" License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.