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 <Result <IEnumerable <Course> > > GetRecentCourseChangesByUKPRN(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 <IEnumerable <Course> >("Get recent course changes unknown UKRLP")); } _httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _settings.ApiKey); var response = await _httpClient.GetAsync(new Uri(_getRecentCourseChangesByUKPRNUri.AbsoluteUri + "?UKPRN=" + criteria.UKPRN)); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); if (!json.StartsWith("[")) { json = "[" + json + "]"; } var courses = JsonConvert.DeserializeObject <IEnumerable <Course> >(json); return(Result.Ok(courses)); } else { return(Result.Fail <IEnumerable <Course> >("Get recent course changes service unsuccessful http response")); } } catch (HttpRequestException hre) { _logger.LogError(hre, "Get recent course changes service http request error"); return(Result.Fail <IEnumerable <Course> >("Get recent course changes service http request error.")); } catch (Exception e) { _logger.LogError(e, "Get recent course changes service unknown error."); return(Result.Fail <IEnumerable <Course> >("Get recent course changes service unknown error.")); } }