private async Task <IList <T> > LoadBase <T>(string api, ILogger logger) { string content; var dataAccess = new HttpDataAccess( logger, hasWriteAccess, "mhwdb", TimeSpan.FromHours(24.0), httpClient => httpClient.GetStringAsync($"https://mhw-db.com/{api}") ); for (int tryCount = 0; tryCount < 2; tryCount++) { content = await dataAccess.GetRawData(api); if (content == null) { dataAccess.InvalidateCache(api); continue; } try { return(JsonConvert.DeserializeObject <IList <T> >(content)); } catch { dataAccess.InvalidateCache(api); } } return(null); }
/// <summary> /// Validates all properties of properties of an AssetDto object. /// </summary> /// <param name="dto">The <see cref="AssetDto"/>.</param> protected override async Task ValidateAsync(AssetDto dto) { if (IsNull(dto)) { return; } var url = $"https://restcountries.eu/rest/v2/name/{dto.CountryOfDepartment}?fullText=true"; var assetValidator = new AssetValidator(HttpDataAccess.SendRequestAsync(url)); var results = await assetValidator.ValidateAsync(new ValidationContext <Asset>(Adapt(dto))); foreach (var e in results.Errors) { ModelState.AddModelError(e.PropertyName, Localizer[e.ErrorCode ?? e.ErrorMessage]); } }
public async Task <IActionResult> IsValidAssetCountry([FromQuery] string country) { if (string.IsNullOrEmpty(country)) { return(Ok(new ValidationResultDto { IsValid = false, ErrorCode = "InvalidCountryName", ErrorMessage = Localizer["InvalidCountryName"] })); } var url = $"https://restcountries.eu/rest/v2/name/{country}?fullText=true"; var validator = new AssetCountryValidator(HttpDataAccess.SendRequestAsync(url)); var dto = GetValidationResultDto(await validator.ValidateAsync(country)); return(Ok(dto)); }