public async Task <PharmanetCollegeRecord> GetCollegeRecordAsync(string licencePrefix, string licenceNumber) { licencePrefix.ThrowIfNull(nameof(licencePrefix)); if (string.IsNullOrWhiteSpace(licenceNumber)) { return(null); } var requestParams = new CollegeRecordRequestParams(licencePrefix, licenceNumber); HttpResponseMessage response = null; try { response = await _client.PostAsync(PrimeEnvironment.PharmanetApi.Url, CreateStringContent(requestParams)); } catch (Exception ex) { await LogError(requestParams, response, ex); throw new PharmanetCollegeApiException("Error occurred when calling Pharmanet API. Try again later.", ex); } if (!response.IsSuccessStatusCode) { await LogError(requestParams, response); throw new PharmanetCollegeApiException($"Error code {response.StatusCode} was returned when calling Pharmanet API."); } var content = await response.Content.ReadAsAsync <List <PharmanetCollegeRecord> >(); var practicionerRecord = content.SingleOrDefault(); // If we get a record back, it should have the same transaction UUID as our request. if (practicionerRecord != null && practicionerRecord.ApplicationUUID != requestParams.ApplicationUUID) { throw new PharmanetCollegeApiException($"Expected matching applicationUUIDs between request data and response data. Request was \"{requestParams.ApplicationUUID}\", response was \"{practicionerRecord.ApplicationUUID}\"."); } return(practicionerRecord); }
public async Task <PharmanetCollegeRecord> GetCollegeRecordAsync(Certification certification) { if (certification == null) { throw new ArgumentNullException(nameof(certification)); } var requestParams = new CollegeRecordRequestParams(certification); HttpResponseMessage response = null; try { response = await _client.PostAsync(PrimeConstants.PHARMANET_API_URL, requestParams.ToRequestContent()); } catch (Exception ex) { await LogError(requestParams, response, ex); throw new PharmanetCollegeApiException("Error occurred when calling Pharmanet API. Try again later.", ex); } if (!response.IsSuccessStatusCode) { await LogError(requestParams, response); throw new PharmanetCollegeApiException($"Error code {response.StatusCode} was returned when calling Pharmanet API."); } var content = await response.Content.ReadAsAsync <List <PharmanetCollegeRecord> >(); var practicionerRecord = content.SingleOrDefault(); // If we get a record back, it should have the same transaction UUID as our request. if (practicionerRecord != null && practicionerRecord.applicationUUID != requestParams.applicationUUID) { throw new PharmanetCollegeApiException($"Expected matching applicationUUIDs between request data and response data. Request was \"{requestParams.applicationUUID}\", response was \"{practicionerRecord.applicationUUID}\"."); } return(practicionerRecord); }
// TODO use real logger private async Task LogError(CollegeRecordRequestParams requestParams, HttpResponseMessage response, Exception exception = null) { string secondaryMessage; if (exception != null) { secondaryMessage = $"exception:{exception.Message}"; } else if (response != null) { string responseMessage = await response.Content.ReadAsStringAsync(); secondaryMessage = $"response code:{(int)response.StatusCode}, response body:{responseMessage}"; } else { secondaryMessage = "no additional message. Http response and exception were null."; } Console.WriteLine($"{DateTime.Now} - Error validating college licence. UUID:{requestParams.ApplicationUUID}, with {secondaryMessage}."); }