public static async Task <List <MarketHistoriesResponse> > GetHistoryItemPricesFromJsonAsync(string uniqueName, IList <string> locations, DateTime?date, IList <int> qualities, int timeScale = 24) { var locationsString = ""; var qualitiesString = ""; if (locations?.Count > 0) { locationsString = string.Join(",", locations); } if (qualities?.Count > 0) { qualitiesString = string.Join(",", qualities); } var url = Settings.Default.CityPricesHistoryApiUrl ?? Settings.Default.CityPricesHistoryApiUrlDefault; url += uniqueName; url += $"?locations={locationsString}"; url += $"&date={date:M-d-yy}"; url += $"&qualities={qualitiesString}"; url += $"&time-scale={timeScale}"; using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromSeconds(30); try { using (var response = await client.GetAsync(url)) { using (var content = response.Content) { if (response.StatusCode == (HttpStatusCode)429) { throw new TooManyRequestsException(); } return(JsonConvert.DeserializeObject <List <MarketHistoriesResponse> >(await content.ReadAsStringAsync())); } } } catch (TooManyRequestsException) { ConsoleManager.WriteLineForWarning(MethodBase.GetCurrentMethod().DeclaringType, new TooManyRequestsException()); throw new TooManyRequestsException(); } catch (Exception e) { ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e); Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e); return(null); } } }
/// <summary> /// Returns city item prices bye uniqueName, locations and qualities. /// </summary> /// <exception cref="TooManyRequestsException"></exception> public static async Task <List <MarketResponse> > GetCityItemPricesFromJsonAsync(string uniqueName, List <string> locations, List <int> qualities) { var url = Settings.Default.CityPricesApiUrl ?? Settings.Default.CityPricesApiUrlDefault; url += uniqueName; if (locations?.Count >= 1) { url += "?locations="; url = locations.Aggregate(url, (current, location) => current + $"{location},"); } if (qualities?.Count >= 1) { url += "&qualities="; url = qualities.Aggregate(url, (current, quality) => current + $"{quality},"); } using (var client = new HttpClient()) { try { client.Timeout = TimeSpan.FromSeconds(30); using (var response = await client.GetAsync(url)) { if (response.StatusCode == (HttpStatusCode)429) { throw new TooManyRequestsException(); } using (var content = response.Content) { return(JsonConvert.DeserializeObject <List <MarketResponse> >(await content.ReadAsStringAsync())); } } } catch (TooManyRequestsException) { ConsoleManager.WriteLineForWarning(MethodBase.GetCurrentMethod().DeclaringType, new TooManyRequestsException()); throw new TooManyRequestsException(); } catch (Exception e) { ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e); Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e); return(null); } } }