public async Task <Result <CourseSearchResult> > GetYourCoursesByUKPRNAsync(CourseSearchCriteria criteria) { if (criteria == null) { throw new ArgumentNullException(nameof(criteria)); } if (criteria.UKPRN < 0) { throw new ArgumentOutOfRangeException(nameof(criteria.UKPRN), $"{nameof(criteria.UKPRN)} cannot be less than 0."); } try { if (!criteria.UKPRN.HasValue) { return(Result.Fail <CourseSearchResult>("Get your courses unknown UKRLP")); } // use local version of httpclient as when we are called from the background worker thread we get socket exceptions HttpClient httpClient = new HttpClient(); httpClient.Timeout = new TimeSpan(0, 10, 0); httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _settings.ApiKey); var response = await httpClient.GetAsync(new Uri(_getYourCoursesUri.AbsoluteUri + "?UKPRN=" + criteria.UKPRN)); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); if (!json.StartsWith("[")) { json = "[" + json + "]"; } var courses = JsonConvert.DeserializeObject <IEnumerable <IEnumerable <IEnumerable <Course> > > >(json); CourseSearchResult searchResult = new CourseSearchResult(courses); return(Result.Ok(searchResult)); } else { return(Result.Fail <CourseSearchResult>("Get your courses service unsuccessful http response")); } } catch (HttpRequestException hre) { _logger.LogError(hre, "Get your courses service http request error"); return(Result.Fail <CourseSearchResult>("Get your courses service http request error.")); } catch (Exception e) { _logger.LogError(e, "Get your courses service unknown error."); return(Result.Fail <CourseSearchResult>($"Get your courses service unknown error. {e.Message}")); } }
public async Task <IResult <ICourseSearchResult> > GetYourCoursesByUKPRNAsync(ICourseSearchCriteria criteria) { Throw.IfNull(criteria, nameof(criteria)); Throw.IfLessThan(0, criteria.UKPRN.Value, nameof(criteria.UKPRN.Value)); _logger.LogMethodEnter(); try { _logger.LogInformationObject("Get your courses criteria", criteria); _logger.LogInformationObject("Get your courses URI", _getYourCoursesUri); if (!criteria.UKPRN.HasValue) { return(Result.Fail <ICourseSearchResult>("Get your courses unknown UKRLP")); } var response = await _httpClient.GetAsync(new Uri(_getYourCoursesUri.AbsoluteUri + "&UKPRN=" + criteria.UKPRN)); _logger.LogHttpResponseMessage("Get your courses service http response", response); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); if (!json.StartsWith("[")) { json = "[" + json + "]"; } _logger.LogInformationObject("Get your courses service json response", json); IEnumerable <IEnumerable <IEnumerable <Course> > > courses = JsonConvert.DeserializeObject <IEnumerable <IEnumerable <IEnumerable <Course> > > >(json); CourseSearchResult searchResult = new CourseSearchResult(courses); return(Result.Ok <ICourseSearchResult>(searchResult)); } else { return(Result.Fail <ICourseSearchResult>("Get your courses service unsuccessful http response")); } } catch (HttpRequestException hre) { _logger.LogException("Get your courses service http request error", hre); return(Result.Fail <ICourseSearchResult>("Get your courses service http request error.")); } catch (Exception e) { _logger.LogException("Get your courses service unknown error.", e); return(Result.Fail <ICourseSearchResult>("Get your courses service unknown error.")); } finally { _logger.LogMethodExit(); } }