Created
November 28, 2017 16:15
-
-
Save andreasohlund/44b737f4522d88ae5a8ec291d2fcff34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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