Repository navigation
Expand file tree
/
Copy pathShopSavvyDataApiClient.cs
More file actions
631 lines (569 loc) · 28.4 KB
/
Copy pathShopSavvyDataApiClient.cs
File metadata and controls
631 lines (569 loc) · 28.4 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ShopSavvy.DataApi
{
/// <summary>
/// Official C# client for ShopSavvy Data API
///
/// Provides access to product data, pricing information, and price history
/// across tens of thousands of retailers and 30M+ products.
/// </summary>
/// <example>
/// <code>
/// var client = new ShopSavvyDataApiClient("ss_live_your_api_key_here");
/// var product = await client.GetProductsAsync("012345678901");
/// Console.WriteLine($"Product: {product.Data[0].Title}");
/// </code>
/// </example>
public class ShopSavvyDataApiClient : IDisposable
{
private readonly HttpClient _httpClient;
private readonly ShopSavvyConfig _config;
/// <summary>
/// Absolute API root every request is built from (e.g. https://api.shopsavvy.com/v1),
/// with no trailing slash.
///
/// Requests are built as <c>_baseUrl + "/products/..."</c> rather than resolved against
/// <see cref="HttpClient.BaseAddress"/>: resolving a root-relative path like
/// <c>/products</c> against <c>https://api.shopsavvy.com/v1</c> REPLACES the path, so
/// every call went to <c>https://api.shopsavvy.com/products</c> with the <c>/v1</c>
/// silently dropped. The deals/batch/webhook/review methods also referenced this field
/// before it was ever declared, so the package did not compile at all from 1.1.0 on.
/// </summary>
private readonly string _baseUrl;
private bool _disposed = false;
/// <summary>
/// Initialize a new ShopSavvy Data API client
/// </summary>
/// <param name="apiKey">Your ShopSavvy API key</param>
/// <param name="baseUrl">Base URL for API (default: https://api.shopsavvy.com/v1)</param>
/// <param name="timeout">Request timeout in milliseconds (default: 30000)</param>
public ShopSavvyDataApiClient(string apiKey, string? baseUrl = null, int timeout = 30000)
: this(new ShopSavvyConfig
{
ApiKey = apiKey,
BaseUrl = baseUrl ?? "https://api.shopsavvy.com/v1",
Timeout = TimeSpan.FromMilliseconds(timeout)
})
{
}
/// <summary>
/// Initialize a new ShopSavvy Data API client with configuration
/// </summary>
/// <param name="config">Client configuration</param>
public ShopSavvyDataApiClient(ShopSavvyConfig config)
: this(config, null)
{
}
/// <summary>
/// Initialize a new ShopSavvy Data API client that sends its requests through a custom
/// <see cref="HttpMessageHandler"/> — for proxies, logging/retry handlers, or tests.
/// </summary>
/// <param name="config">Client configuration</param>
/// <param name="handler">Handler to send requests through; the default handler when null. Disposed with the client.</param>
public ShopSavvyDataApiClient(ShopSavvyConfig config, HttpMessageHandler? handler)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
// Validate API key
if (string.IsNullOrEmpty(_config.ApiKey))
{
throw new ArgumentException("API key is required. Get one at https://shopsavvy.com/data", nameof(config));
}
if (!Regex.IsMatch(_config.ApiKey, @"^ss_(live|test)_[a-zA-Z0-9]+$"))
{
throw new ArgumentException("Invalid API key format. API keys should start with ss_live_ or ss_test_", nameof(config));
}
_baseUrl = _config.BaseUrl.TrimEnd('/');
// Create HTTP client
_httpClient = handler == null ? new HttpClient() : new HttpClient(handler);
_httpClient.Timeout = _config.Timeout;
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_config.ApiKey}");
_httpClient.DefaultRequestHeaders.Add("User-Agent", $"ShopSavvy-CSharp-SDK/{ShopSavvySdk.Version}");
}
/// <summary>
/// Look up product details by identifier
/// </summary>
/// <param name="id">Product identifier (barcode, ASIN, URL, model number, or ShopSavvy product ID)</param>
/// <returns>Product details array</returns>
public async Task<ApiResponse<ProductDetails[]>> GetProductsAsync(string id)
{
var queryParams = new Dictionary<string, string> { { "ids", id } };
return await MakeRequestAsync<ProductDetails[]>("GET", "/products", queryParams);
}
/// <summary>
/// Look up details for multiple products
/// </summary>
/// <param name="ids">Array of product identifiers</param>
/// <returns>Array of product details</returns>
public async Task<ApiResponse<ProductDetails[]>> GetProductsBatchAsync(string[] ids)
{
var queryParams = new Dictionary<string, string> { { "ids", string.Join(",", ids) } };
return await MakeRequestAsync<ProductDetails[]>("GET", "/products", queryParams);
}
/// <summary>
/// Look up product details by identifier (deprecated, use GetProductsAsync)
/// </summary>
[Obsolete("Use GetProductsAsync instead")]
public async Task<ApiResponse<ProductDetails>> GetProductDetailsAsync(string identifier, string? format = null)
{
var result = await GetProductsAsync(identifier);
return new ApiResponse<ProductDetails>
{
Success = result.Success,
Data = result.Data?.Length > 0 ? result.Data[0] : null!,
Message = result.Message,
Meta = result.Meta
};
}
/// <summary>
/// Look up details for multiple products (deprecated, use GetProductsBatchAsync)
/// </summary>
[Obsolete("Use GetProductsBatchAsync instead")]
public async Task<ApiResponse<ProductDetails[]>> GetProductDetailsBatchAsync(string[] identifiers, string? format = null)
{
return await GetProductsBatchAsync(identifiers);
}
/// <summary>
/// Search for products by query
/// </summary>
/// <param name="query">Search query</param>
/// <param name="limit">Maximum results (default 20, max 100)</param>
/// <param name="offset">Offset for pagination</param>
/// <returns>Product search results with pagination</returns>
public async Task<ProductSearchResult> SearchProductsAsync(string query, int limit = 20, int offset = 0)
{
var queryParams = new Dictionary<string, string>
{
{ "q", query },
{ "limit", limit.ToString() },
{ "offset", offset.ToString() }
};
return await MakeRequestDirectAsync<ProductSearchResult>("GET", "/products/search", queryParams);
}
/// <summary>
/// Get current offers for a product
/// </summary>
/// <param name="id">Product identifier</param>
/// <param name="retailer">Optional retailer to filter by</param>
/// <returns>Products with their current offers</returns>
public async Task<ApiResponse<ProductWithOffers[]>> GetOffersAsync(string id, string? retailer = null)
{
var queryParams = new Dictionary<string, string> { { "ids", id } };
if (!string.IsNullOrEmpty(retailer))
{
queryParams["retailer"] = retailer;
}
return await MakeRequestAsync<ProductWithOffers[]>("GET", "/products/offers", queryParams);
}
/// <summary>
/// Get current offers for multiple products
/// </summary>
/// <param name="ids">Array of product identifiers</param>
/// <param name="retailer">Optional retailer to filter by</param>
/// <returns>Products with their current offers</returns>
public async Task<ApiResponse<ProductWithOffers[]>> GetOffersBatchAsync(string[] ids, string? retailer = null)
{
var queryParams = new Dictionary<string, string> { { "ids", string.Join(",", ids) } };
if (!string.IsNullOrEmpty(retailer))
{
queryParams["retailer"] = retailer;
}
return await MakeRequestAsync<ProductWithOffers[]>("GET", "/products/offers", queryParams);
}
/// <summary>
/// Get current offers for a product (deprecated, use GetOffersAsync)
/// </summary>
[Obsolete("Use GetOffersAsync instead")]
public async Task<ApiResponse<Offer[]>> GetCurrentOffersAsync(string identifier, string? retailer = null, string? format = null)
{
var result = await GetOffersAsync(identifier, retailer);
var offers = result.Data?.Length > 0 ? result.Data[0].Offers : Array.Empty<Offer>();
return new ApiResponse<Offer[]>
{
Success = result.Success,
Data = offers,
Message = result.Message,
Meta = result.Meta
};
}
/// <summary>
/// Get current offers for multiple products (deprecated, use GetOffersBatchAsync)
/// </summary>
[Obsolete("Use GetOffersBatchAsync instead")]
public async Task<ApiResponse<Dictionary<string, Offer[]>>> GetCurrentOffersBatchAsync(string[] identifiers, string? retailer = null, string? format = null)
{
var result = await GetOffersBatchAsync(identifiers, retailer);
var dict = new Dictionary<string, Offer[]>();
if (result.Data != null)
{
foreach (var product in result.Data)
{
dict[product.Shopsavvy] = product.Offers;
}
}
return new ApiResponse<Dictionary<string, Offer[]>>
{
Success = result.Success,
Data = dict,
Message = result.Message,
Meta = result.Meta
};
}
/// <summary>
/// Get price history for a product
/// </summary>
/// <param name="id">Product identifier</param>
/// <param name="startDate">Start date (YYYY-MM-DD format)</param>
/// <param name="endDate">End date (YYYY-MM-DD format)</param>
/// <param name="retailer">Optional retailer to filter by</param>
/// <returns>
/// One entry per matched product (product fields plus <c>Offers</c>), where each offer
/// carries its own <c>History</c> of price points, newest first
/// </returns>
public async Task<ApiResponse<ProductWithPriceHistory[]>> GetPriceHistoryAsync(string id, string startDate, string endDate, string? retailer = null)
{
var queryParams = new Dictionary<string, string>
{
// Wire params are "start"/"end" — what GET /products/offers/history
// reads, and what the OpenAPI spec and public docs document. The old
// "start_date"/"end_date" names came from the MCP tool's argument
// convention (a different interface entirely) and 400'd every call.
{ "ids", id },
{ "start", startDate },
{ "end", endDate }
};
if (!string.IsNullOrEmpty(retailer))
{
queryParams["retailer"] = retailer;
}
return await MakeRequestAsync<ProductWithPriceHistory[]>("GET", "/products/offers/history", queryParams);
}
/// <summary>
/// Get price history for multiple products
/// </summary>
/// <param name="ids">Array of product identifiers</param>
/// <param name="startDate">Start date (YYYY-MM-DD format)</param>
/// <param name="endDate">End date (YYYY-MM-DD format)</param>
/// <param name="retailer">Optional retailer to filter by</param>
/// <returns>One entry per matched product, each with its offers and their price history</returns>
public async Task<ApiResponse<ProductWithPriceHistory[]>> GetPriceHistoryBatchAsync(string[] ids, string startDate, string endDate, string? retailer = null)
{
var queryParams = new Dictionary<string, string>
{
// Wire params are "start"/"end" — see the single-id variant above.
{ "ids", string.Join(",", ids) },
{ "start", startDate },
{ "end", endDate }
};
if (!string.IsNullOrEmpty(retailer))
{
queryParams["retailer"] = retailer;
}
return await MakeRequestAsync<ProductWithPriceHistory[]>("GET", "/products/offers/history", queryParams);
}
/// <summary>
/// Schedule product monitoring
/// </summary>
/// <param name="identifier">Product identifier</param>
/// <param name="frequency">How often to refresh ('hourly', 'daily', 'weekly')</param>
/// <param name="retailer">Optional retailer domain to monitor (e.g. "amazon.com")</param>
/// <returns>The scheduled product (an array: the API always returns <c>data</c> as a list), with its schedule and retailer</returns>
public async Task<ApiResponse<ScheduledProduct[]>> ScheduleProductMonitoringAsync(string identifier, string frequency, string? retailer = null)
{
return await MakeRequestAsync<ScheduledProduct[]>("PUT", "/products/scheduled", ScheduleQueryParams(new[] { identifier }, frequency, retailer));
}
/// <summary>
/// Schedule monitoring for multiple products
/// </summary>
/// <param name="identifiers">Array of product identifiers</param>
/// <param name="frequency">How often to refresh</param>
/// <param name="retailer">Optional retailer domain to monitor (e.g. "amazon.com")</param>
/// <returns>One entry per identifier that resolved to a product (unknown identifiers are omitted, and not charged)</returns>
public async Task<ApiResponse<ScheduledProduct[]>> ScheduleProductMonitoringBatchAsync(string[] identifiers, string frequency, string? retailer = null)
{
return await MakeRequestAsync<ScheduledProduct[]>("PUT", "/products/scheduled", ScheduleQueryParams(identifiers, frequency, retailer));
}
/// <summary>
/// Get all scheduled products
/// </summary>
/// <returns>List of scheduled products, each with its schedule and retailer</returns>
public async Task<ApiResponse<ScheduledProduct[]>> GetScheduledProductsAsync()
{
return await MakeRequestAsync<ScheduledProduct[]>("GET", "/products/scheduled");
}
/// <summary>
/// Remove product from monitoring schedule
/// </summary>
/// <param name="identifier">Product identifier to remove</param>
/// <returns>Confirmation (<c>success</c>, <c>message</c>, <c>meta</c>; the API returns no <c>data</c>)</returns>
public async Task<UnscheduleResponse> RemoveProductFromScheduleAsync(string identifier)
{
var queryParams = new Dictionary<string, string> { { "ids", identifier } };
return await MakeRequestDirectAsync<UnscheduleResponse>("DELETE", "/products/scheduled", queryParams);
}
/// <summary>
/// Remove multiple products from monitoring schedule
/// </summary>
/// <param name="identifiers">Array of product identifiers to remove</param>
/// <returns>Confirmation (<c>success</c>, <c>message</c>, <c>meta</c>; the API returns no per-product results)</returns>
public async Task<UnscheduleResponse> RemoveProductsFromScheduleAsync(string[] identifiers)
{
var queryParams = new Dictionary<string, string> { { "ids", string.Join(",", identifiers) } };
return await MakeRequestDirectAsync<UnscheduleResponse>("DELETE", "/products/scheduled", queryParams);
}
/// <summary>
/// Query string for PUT /products/scheduled.
///
/// The schedule/unschedule handlers read ONLY query parameters — <c>ids</c>
/// (comma-separated), <c>schedule</c> and optional <c>retailer</c>. These methods used
/// to POST/DELETE a JSON body (<c>identifier</c>/<c>frequency</c>) to /products/schedule,
/// which the server ignores entirely, so every call failed for want of <c>ids</c>.
/// </summary>
private static Dictionary<string, string> ScheduleQueryParams(string[] identifiers, string frequency, string? retailer)
{
var queryParams = new Dictionary<string, string>
{
{ "ids", string.Join(",", identifiers) },
{ "schedule", frequency }
};
if (retailer != null && retailer.Length > 0)
{
queryParams["retailer"] = retailer;
}
return queryParams;
}
/// <summary>
/// Get API usage information
/// </summary>
/// <returns>Current usage and credit information</returns>
public async Task<ApiResponse<UsageInfo>> GetUsageAsync()
{
return await MakeRequestAsync<UsageInfo>("GET", "/usage");
}
/// <summary>Browse current shopping deals</summary>
public async Task<string> GetDealsAsync(Dictionary<string, string>? parameters = null)
{
var url = "/deals";
if (parameters != null && parameters.Count > 0)
{
var query = string.Join("&", parameters.Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
url += "?" + query;
}
var response = await _httpClient.GetAsync(_baseUrl + url);
return await response.Content.ReadAsStringAsync();
}
/// <summary>Look up multiple products at once (sync for <=20, async for >20)</summary>
public async Task<string> BatchLookupAsync(string[] identifiers, string[]? include = null)
{
var body = new Dictionary<string, object> { { "identifiers", identifiers } };
if (include != null) body["include"] = include;
var json = Newtonsoft.Json.JsonConvert.SerializeObject(body);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(_baseUrl + "/products/batch", content);
return await response.Content.ReadAsStringAsync();
}
/// <summary>Poll for async batch job results</summary>
public async Task<string> GetBatchStatusAsync(string batchId)
{
var response = await _httpClient.GetAsync(_baseUrl + $"/batch/{Uri.EscapeDataString(batchId)}");
return await response.Content.ReadAsStringAsync();
}
public async Task<string> CreateWebhookAsync(string url, string[] events)
{
var body = new { url, events };
var json = Newtonsoft.Json.JsonConvert.SerializeObject(body);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(_baseUrl + "/webhooks", content);
return await response.Content.ReadAsStringAsync();
}
public async Task<string> ListWebhooksAsync()
{
var response = await _httpClient.GetAsync(_baseUrl + "/webhooks");
return await response.Content.ReadAsStringAsync();
}
public async Task<string> TestWebhookAsync(string webhookId)
{
var response = await _httpClient.PostAsync(_baseUrl + $"/webhooks/{Uri.EscapeDataString(webhookId)}/test", null);
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Update a webhook. All parameters are optional, but at least one of
/// <paramref name="url"/>, <paramref name="events"/>, or
/// <paramref name="isActive"/> must be non-null.
/// </summary>
public async Task<string> UpdateWebhookAsync(string webhookId, string? url = null, string[]? events = null, bool? isActive = null)
{
if (url == null && events == null && !isActive.HasValue)
{
throw new ArgumentException("UpdateWebhookAsync requires at least one of url, events, or isActive");
}
var body = new System.Collections.Generic.Dictionary<string, object>();
if (url != null) body["url"] = url;
if (events != null) body["events"] = events;
if (isActive.HasValue) body["is_active"] = isActive.Value;
var json = Newtonsoft.Json.JsonConvert.SerializeObject(body);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var request = new HttpRequestMessage(HttpMethod.Put, _baseUrl + $"/webhooks/{Uri.EscapeDataString(webhookId)}") { Content = content };
var response = await _httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
public async Task<string> DeleteWebhookAsync(string webhookId)
{
var response = await _httpClient.DeleteAsync(_baseUrl + $"/webhooks/{Uri.EscapeDataString(webhookId)}");
return await response.Content.ReadAsStringAsync();
}
/// <summary>Get TLDR review for a product</summary>
public async Task<string> GetProductReviewAsync(string identifier)
{
var response = await _httpClient.GetAsync(_baseUrl + $"/products/reviews?id={Uri.EscapeDataString(identifier)}");
return await response.Content.ReadAsStringAsync();
}
private async Task<ApiResponse<T>> MakeRequestAsync<T>(string method, string endpoint, Dictionary<string, string>? queryParams = null, object? body = null)
{
var url = _baseUrl + endpoint;
if (queryParams != null && queryParams.Count > 0)
{
var query = string.Join("&", queryParams.Select(kvp => $"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"));
url += $"?{query}";
}
var request = new HttpRequestMessage(new HttpMethod(method), url);
if (body != null)
{
var json = JsonConvert.SerializeObject(body, Formatting.None, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
try
{
var response = await _httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw CreateExceptionFromResponse(response, responseContent);
}
var apiResponse = JsonConvert.DeserializeObject<ApiResponse<T>>(responseContent);
return apiResponse ?? throw new ShopSavvyApiException("Failed to deserialize response");
}
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
throw new ShopSavvyTimeoutException($"Request timeout after {_config.Timeout.TotalSeconds} seconds");
}
catch (HttpRequestException ex)
{
throw new ShopSavvyNetworkException($"Network error: {ex.Message}", ex);
}
}
private async Task<T> MakeRequestDirectAsync<T>(string method, string endpoint, Dictionary<string, string>? queryParams = null, object? body = null)
{
var url = _baseUrl + endpoint;
if (queryParams != null && queryParams.Count > 0)
{
var query = string.Join("&", queryParams.Select(kvp => $"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"));
url += $"?{query}";
}
var request = new HttpRequestMessage(new HttpMethod(method), url);
if (body != null)
{
var json = JsonConvert.SerializeObject(body, Formatting.None, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
try
{
var response = await _httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw CreateExceptionFromResponse(response, responseContent);
}
var result = JsonConvert.DeserializeObject<T>(responseContent);
return result ?? throw new ShopSavvyApiException("Failed to deserialize response");
}
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
throw new ShopSavvyTimeoutException($"Request timeout after {_config.Timeout.TotalSeconds} seconds");
}
catch (HttpRequestException ex)
{
throw new ShopSavvyNetworkException($"Network error: {ex.Message}", ex);
}
}
private static Exception CreateExceptionFromResponse(HttpResponseMessage response, string responseContent)
{
var statusCode = response.StatusCode;
var errorMessage = "Unknown error";
string? code = null, action = null, docsUrl = null, requestId = null;
try
{
var errorResponse = JObject.Parse(responseContent);
errorMessage = errorResponse["error"]?.ToString() ?? errorMessage;
if (errorResponse["error_detail"] is JObject detail)
{
code = detail["code"]?.ToString();
action = detail["action"]?.ToString();
docsUrl = detail["docs_url"]?.ToString();
}
requestId = (errorResponse["meta"] as JObject)?["request_id"]?.ToString();
}
catch (JsonException)
{
errorMessage = responseContent;
}
int? retryAfterSeconds = null;
if (response.Headers.TryGetValues("Retry-After", out var retryValues)
&& int.TryParse(retryValues.FirstOrDefault()?.Trim(), out var seconds) && seconds >= 0)
{
retryAfterSeconds = seconds;
}
// The code decides the type when present (two different codes share HTTP 429); the
// HTTP status decides for older responses that carry no error_detail.
ShopSavvyApiException exception;
if (code == "ERR_CREDITS_EXHAUSTED")
{
exception = new ShopSavvyCreditsExhaustedException(errorMessage);
}
else
{
exception = (int)statusCode switch
{
401 => new ShopSavvyAuthenticationException("Authentication failed. Check your API key."),
404 => new ShopSavvyNotFoundException("Resource not found"),
422 => new ShopSavvyValidationException("Request validation failed. Check your parameters."),
429 => new ShopSavvyRateLimitException("Rate limit exceeded. Please slow down your requests."),
_ => new ShopSavvyApiException($"HTTP {(int)statusCode}: {errorMessage}")
};
}
exception.Code = code;
exception.Action = action;
exception.DocsUrl = docsUrl;
exception.RequestId = requestId;
exception.StatusCode = (int)statusCode;
exception.RetryAfterSeconds = retryAfterSeconds;
return exception;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed && disposing)
{
_httpClient?.Dispose();
_disposed = true;
}
}
}
}