Skip to content

Instantly share code, notes, and snippets.

@andreasohlund
Created November 28, 2017 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasohlund/44b737f4522d88ae5a8ec291d2fcff34 to your computer and use it in GitHub Desktop.
Save andreasohlund/44b737f4522d88ae5a8ec291d2fcff34 to your computer and use it in GitHub Desktop.
public static async Task<InventoryStatusResponse> GetInventoryStatus(string id)
{
var numRetries = 0;
var siteId = "228202-91B";
do
{
try
{
using (var client = new HttpClient())
{
var inventoryStatusRequest = new InventoryStatusRequest
{
SiteId = siteId,
BasketItems = new List<BasketItem>
{
new BasketItem
{
ProductNumber = id
}
}
};
var content = new StringContent(JsonConvert.SerializeObject(inventoryStatusRequest), Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://www.systembolaget.se/api/basket/create", content).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.InternalServerError)
{
siteId = "2202"; //try ordinary store as well
}
response.EnsureSuccessStatusCode();
var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<InventoryStatusResponse>(responseText, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
}
}
catch (HttpRequestException)
{
numRetries++;
if (numRetries > 3)
{
throw;
}
await Task.Delay(TimeSpan.FromSeconds(10));
Console.Out.WriteLine("Retrying " + id);
}
} while (true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment