/// <inheritdoc/> public async Task <Pharmacy> GetPharmacyAsync(string pharmacyId, string userId, string ipAddress) { using (HttpClient client = this.httpClientFactory.CreateClient("medicationService")) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json)); client.BaseAddress = new Uri(this.configService.GetSection("HNClient")?.GetValue <string>("Url")); HNMessage responseMessage; HNMessage requestMessage = this.pharmacyParser.CreateRequestMessage(pharmacyId, userId, ipAddress); HttpResponseMessage response = await client.PostAsJsonAsync("v1/api/HNClient", requestMessage).ConfigureAwait(true); if (response.IsSuccessStatusCode) { string payload = await response.Content.ReadAsStringAsync().ConfigureAwait(true); responseMessage = JsonConvert.DeserializeObject <HNMessage>(payload); } else { throw new HttpRequestException($"Unable to connect to HNClient: ${response.StatusCode}"); } return(this.pharmacyParser.ParseResponseMessage(responseMessage.Message).FirstOrDefault()); } }
/// <inheritdoc/> public async Task <List <MedicationStatement> > GetMedicationsAsync(string phn, string userId, string ipAddress) { JWTModel jwtModel = this.AuthenticateService(); using (HttpClient client = this.httpClientFactory.CreateClient("medicationService")) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json)); client.BaseAddress = new Uri(this.configService.GetSection("HNClient")?.GetValue <string>("Url")); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + jwtModel.AccessToken); HNMessage requestMessage = this.medicationParser.CreateRequestMessage(phn, userId, ipAddress); HttpResponseMessage response = await client.PostAsJsonAsync("v1/api/HNClient", requestMessage).ConfigureAwait(true); if (response.IsSuccessStatusCode) { string payload = await response.Content.ReadAsStringAsync().ConfigureAwait(true); HNMessage responseMessage = JsonConvert.DeserializeObject <HNMessage>(payload); return(this.medicationParser.ParseResponseMessage(responseMessage.Message)); } else { throw new HttpRequestException($"Unable to connect to HNClient: ${response.StatusCode}"); } } }
public async Task ShouldGetMedications() { HNMessage expected = new HNMessage() { Message = "Test", IsErr = false, }; Mock <IAuthService> authMock = new Mock <IAuthService>(); authMock.Setup(s => s.ClientCredentialsAuth()).ReturnsAsync(new JWTModel()); Mock <IHNMessageParser <MedicationStatement> > parserMock = new Mock <IHNMessageParser <MedicationStatement> >(); parserMock.Setup(s => s.ParseResponseMessage(expected.Message)).Returns(new List <MedicationStatement>()); Mock <IHttpClientFactory> httpMock = new Mock <IHttpClientFactory>(); var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => { request.SetConfiguration(new HttpConfiguration()); HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK, expected, MediaTypeNames.Application.Json); return(Task.FromResult(response)); }); var client = new HttpClient(clientHandlerStub); httpMock.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client); IMedicationService service = new RestMedicationService(parserMock.Object, httpMock.Object, configuration, authMock.Object); List <MedicationStatement> medications = await service.GetMedicationsAsync("123456789", "test", "10.0.0.1"); Assert.True(medications.Count == 0); }
/// <inheritdoc/> public HNMessage SendMessage(HNMessage msg) { if (msg is null) { throw new ArgumentNullException(nameof(msg)); } HNMessage retMessage = new HNMessage(); try { retMessage.Message = this.hnclient.SendReceive(msg.Message); } catch (Exception e) { if (e is InvalidDataException || e is InvalidOperationException || e is SocketException) { retMessage.IsErr = true; retMessage.Error = e.Message; } else { #pragma warning disable CA1303 // Do not pass literals as localized parameters throw new Exception("Failed to send message.", e); #pragma warning restore CA1303 // Do not pass literals as localized parameters } } return(retMessage); }
public void ShouldCreateRequestMessage() { string dateTime = this.getDateTime().ToString("yyyy/MM/dd HH:mm:ss", this.culture); string date = this.getDateTime().ToString("yyMMdd", this.culture); HNMessage request = this.parser.CreateRequestMessage(phn, userId, ipAddress); Assert.False(request.IsErr); Assert.StartsWith($"MSH|^~\\&|{hnClientConfig.SendingApplication}|{hnClientConfig.SendingFacility}|{hnClientConfig.ReceivingApplication}|{hnClientConfig.ReceivingFacility}|{dateTime}|{userId}:{ipAddress}|ZPN|{traceNumber}|{hnClientConfig.ProcessingID}|{hnClientConfig.MessageVersion}\r", request.Message); Assert.Contains($"ZCA|{hnClientConfig.ZCA.BIN}|{hnClientConfig.ZCA.CPHAVersionNumber}|{hnClientConfig.ZCA.TransactionCode}|{hnClientConfig.ZCA.SoftwareId}|{hnClientConfig.ZCA.SoftwareVersion}", request.Message); Assert.Contains($"ZZZ|TRP||{traceNumber}|{hnClientConfig.ZZZ.PractitionerIdRef}|{hnClientConfig.ZZZ.PractitionerId}", request.Message); Assert.Contains($"ZCB|{hnClientConfig.ZCB.PharmacyId}|{date}|{traceNumber}", request.Message); Assert.EndsWith($"ZCC||||||||||{phn}|\r", request.Message); }
/// <inheritdoc/> public HNMessage CreateRequestMessage(string pharmacyId, string userId, string ipAddress) { CultureInfo culture = CultureInfo.CreateSpecificCulture("en-CA"); culture.DateTimeFormat.DateSeparator = "/"; HL7Encoding encoding = new HL7Encoding(); Message m = new Message(); DateTime utc = DateTime.UtcNow; TimeZoneInfo localtz = TimeZoneInfo.FindSystemTimeZoneById(this.timeZoneId); DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, localtz); // MSH - Message Header m.AddSegmentMSH( this.hnClientConfig.SendingApplication, this.hnClientConfig.SendingFacility, this.hnClientConfig.ReceivingApplication, this.hnClientConfig.ReceivingFacility, $"{userId}:{ipAddress}", $"{HNClientConfiguration.PATIENT_PROFILE_MESSAGE_TYPE}^00", TRACE, this.hnClientConfig.ProcessingID, this.hnClientConfig.MessageVersion); m.SetValue("MSH.7", string.Format(culture, "{0:yyyy/MM/dd HH:mm:ss}", localDateTime)); // HNClient specific date format m.SetValue("MSH.9", HNClientConfiguration.PATIENT_PROFILE_MESSAGE_TYPE); // HNClient doesn't recognize event types (removes ^00 from message type) // ZCA - Claims Standard Message Header Segment zca = new Segment(HNClientConfiguration.SEGMENT_ZCA, encoding); zca.AddNewField(string.Empty); // BIN zca.AddNewField(this.hnClientConfig.ZCA.CPHAVersionNumber); // CPHA Version Number zca.AddNewField(this.hnClientConfig.ZCA.TransactionCode); // Transaction Code zca.AddNewField(this.hnClientConfig.ZCA.SoftwareId); // Provider Software ID zca.AddNewField(this.hnClientConfig.ZCA.SoftwareVersion); // Provider Software Version m.AddNewSegment(zca); // ZCB - Provider Information Segment zcb = new Segment(HNClientConfiguration.SEGMENT_ZCB, encoding); zcb.AddNewField(this.hnClientConfig.ZCB.PharmacyId); // Pharmacy ID Code zcb.AddNewField(localDateTime.ToString("yyMMdd", culture)); // Provider Transaction Date zcb.AddNewField(TRACE); // Trace Number m.AddNewSegment(zcb); // ZPL - Location Information Segment zpl = new Segment(HNClientConfiguration.SEGMENT_ZPL, encoding); zpl.AddNewField(pharmacyId); // Requested PharmaNet Location Identifier zpl.AddNewField(string.Empty); // PharmaNet Location Nam zpl.AddNewField(string.Empty); // Location Type Code zpl.AddNewField(string.Empty); // Address Line 1 zpl.AddNewField(string.Empty); // Address Line 2 zpl.AddNewField(string.Empty); // City or Municipality zpl.AddNewField(string.Empty); // Province Code zpl.AddNewField(string.Empty); // Postal Code zpl.AddNewField(string.Empty); // Country Code zpl.AddNewField(string.Empty); // Telecom Type Code zpl.AddNewField(string.Empty); // Effective Date zpl.AddNewField(string.Empty); // Area Code zpl.AddNewField(string.Empty); // Telephone Number zpl.AddNewField(string.Empty); // Termination Date zpl.AddNewField(this.hnClientConfig.ZPL.TransactionReasonCode); // Transaction Reason Code m.AddNewSegment(zpl); // ZZZ - Transaction Control // ZZZ|TIL||1111|P1|nnnnnnnnnn|||||ZZZ1 Segment zzz = new Segment(HNClientConfiguration.SEGMENT_ZZZ, encoding); zzz.AddNewField(HNClientConfiguration.PHARMACY_PROFILE_TRANSACTION_ID); // Transaction ID zzz.AddNewField(string.Empty); // Response Status (Empty) zzz.AddNewField(TRACE); // Trace Number zzz.AddNewField(this.hnClientConfig.ZZZ.PractitionerIdRef); // Practitioner ID Reference zzz.AddNewField(this.hnClientConfig.ZZZ.PractitionerId); // Practitioner ID m.AddNewSegment(zzz); HNMessage retMessage = new HNMessage(); retMessage.Message = m.SerializeMessage(false); return(retMessage); }
/// <inheritdoc/> public HNMessage GetTime() { HNMessage request = new HNMessage(this.timeRequest); return(this.SendMessage(request)); }
/// <inheritdoc/> public HNMessage CreateRequestMessage(string phn, string userId, string ipAddress) { if (phn.Length < 13) { phn = phn.PadLeft(13, '0'); } HNClientConfiguration hnClientConfig = new HNClientConfiguration(); this.configuration.GetSection("HNClient").Bind(hnClientConfig); // OUR FACILITY 'BC01001249' // Raw HL7 sample message. // MSH|^~\\&|GATEWAY|BC01001249|PNP|CPA|2019/09/24 13:49:29|1001:127.0.0.1|ZPN|248875|D|2.1|| // \r // ZZZ|TRP||248876|91|XXAPZ // \r // ZCA|1|70|00|HG|01| // \r // ZCB|BCXX000034|190819|248876 // \r // ZCC||||||||||0009735353315| // \r // \r CultureInfo culture = CultureInfo.CreateSpecificCulture("en-CA"); culture.DateTimeFormat.DateSeparator = "/"; HL7Encoding encoding = new HL7Encoding(); Message m = new Message(); // MSH - Message Header DateTime utc = DateTime.UtcNow; TimeZoneInfo localtz = TimeZoneInfo.FindSystemTimeZoneById(this.timeZoneId); DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, localtz); m.AddSegmentMSH( this.hnClientConfig.SendingApplication, this.hnClientConfig.SendingFacility, this.hnClientConfig.ReceivingApplication, this.hnClientConfig.ReceivingFacility, $"{userId}:{ipAddress}", $"{HNClientConfiguration.PATIENT_PROFILE_MESSAGE_TYPE}^00", TRACE, this.hnClientConfig.ProcessingID, this.hnClientConfig.MessageVersion); m.SetValue("MSH.7", string.Format(culture, "{0:yyyy/MM/dd HH:mm:ss}", localDateTime)); // HNClient specific date format m.SetValue("MSH.9", HNClientConfiguration.PATIENT_PROFILE_MESSAGE_TYPE); // HNClient doesn't recognize event types (removes ^00 from message type) // ZZZ - Transaction Control Segment zzz = new Segment(HNClientConfiguration.SEGMENT_ZZZ, encoding); zzz.AddNewField(HNClientConfiguration.PATIENT_PROFILE_TRANSACTION_ID); // Transaction ID zzz.AddNewField(string.Empty); // Response Status (Empty) zzz.AddNewField(TRACE); // Trace Number zzz.AddNewField(this.hnClientConfig.ZZZ.PractitionerIdRef); // Practitioner ID Reference zzz.AddNewField(this.hnClientConfig.ZZZ.PractitionerId); // Practitioner ID m.AddNewSegment(zzz); // ZCA - Claims Standard Message Header Segment zca = new Segment(HNClientConfiguration.SEGMENT_ZCA, encoding); zca.AddNewField(this.hnClientConfig.ZCA.BIN); // BIN zca.AddNewField(this.hnClientConfig.ZCA.CPHAVersionNumber); // CPHA Version Number zca.AddNewField(this.hnClientConfig.ZCA.TransactionCode); // Transaction Code zca.AddNewField(this.hnClientConfig.ZCA.SoftwareId); // Provider Software ID zca.AddNewField(this.hnClientConfig.ZCA.SoftwareVersion); // Provider Software Version m.AddNewSegment(zca); // ZCB - Provider Information Segment zcb = new Segment(HNClientConfiguration.SEGMENT_ZCB, encoding); zcb.AddNewField(this.hnClientConfig.ZCB.PharmacyId); // Pharmacy ID Code zcb.AddNewField(localDateTime.ToString("yyMMdd", culture)); // Provider Transaction Date zcb.AddNewField(TRACE); // Trace Number m.AddNewSegment(zcb); // ZCC - Beneficiary Information Segment zcc = new Segment(HNClientConfiguration.SEGMENT_ZCC, encoding); zcc.AddNewField(string.Empty); // Carrier ID zcc.AddNewField(string.Empty); // Group Number or Code zcc.AddNewField(string.Empty); // Client ID Number or Code zcc.AddNewField(string.Empty); // Patient Code zcc.AddNewField(string.Empty); // Patient Date of Birth zcc.AddNewField(string.Empty); // Cardholder Identity zcc.AddNewField(string.Empty); // Relationship zcc.AddNewField(string.Empty); // Patient First Name zcc.AddNewField(string.Empty); // Patient Last Name zcc.AddNewField(phn); // Provincial Health Care ID zcc.AddNewField(string.Empty); // Patient Gender m.AddNewSegment(zcc); HNMessage retMessage = new HNMessage(); retMessage.Message = m.SerializeMessage(false); return(retMessage); }
public HNMessage SendMessage(HNMessage msg) { return(this.service.SendMessage(msg)); }