public async Task <string> GetPhoneNumber(string patientReferenceNumber)
        {
            var openmrsRestPatientPath = $"{DiscoveryPathConstants.OnRestPatientPath}/{patientReferenceNumber}";

            var response = await openMrsClient.GetAsync(openmrsRestPatientPath);

            var content = await response.Content.ReadAsStringAsync();

            var jsonDoc = JsonDocument.Parse(content);
            var root    = jsonDoc.RootElement;

            var person     = root.GetProperty("person");
            var attributes = person.GetProperty("attributes");

            for (int i = 0; i < attributes.GetArrayLength(); i++)
            {
                var display = attributes[i].GetProperty("display");
                var strlist = display.ToString().Split(" = ");
                if (strlist[0] == "primaryContact")
                {
                    return(strlist[1]);
                }
            }

            return(null);
        }
        public async Task <Dictionary <string, string> > CheckHealth()
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            foreach (var entry in endpoints)
            {
                try
                {
                    var response = await openMrsClient.GetAsync(entry.Value);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        result.Add(entry.Key, "Healthy");
                    }
                    else
                    {
                        result.Add(entry.Key, "Unhealthy");
                    }
                }
                catch (Exception exception)
                {
                    result.Add(entry.Key, "Unhealthy");
                    Log.Fatal(exception, exception.StackTrace);
                }
            }
            return(result);
        }
        public virtual async Task <List <CareContextRepresentation> > LoadProgramEnrollments(string uuid)
        {
            var path  = DiscoveryPathConstants.OnProgramEnrollmentPath;
            var query = HttpUtility.ParseQueryString(string.Empty);

            if (!string.IsNullOrEmpty(uuid))
            {
                query["patient"] = uuid;
                query["v"]       = "full";
            }
            if (query.ToString() != "")
            {
                path = $"{path}/?{query}";
            }

            var response = await openMrsClient.GetAsync(path);

            var content = await response.Content.ReadAsStringAsync();

            var jsonDoc = JsonDocument.Parse(content);
            var root    = jsonDoc.RootElement;

            var careContexts = new List <CareContextRepresentation>();
            var results      = root.GetProperty("results");

            for (int i = 0; i < results.GetArrayLength(); i++)
            {
                var attributes = TryGetProperty(results[i], "attributes");
                if (attributes.GetArrayLength() == 0)
                {
                    LogAndThrowException($"Property 'attributes' is empty when getting program enrollments.");
                }
                var referenceNumber = TryGetProperty(attributes[0], "value");
                var display         = TryGetProperty(results[i], "display");
                careContexts.Add(new CareContextRepresentation(referenceNumber.GetString(), display.GetString()));
            }

            return(careContexts);
        }
        public async Task <List <Patient> > LoadPatientsAsync(string name, AdministrativeGender?gender, string yearOfBirth)
        {
            var path  = DiscoveryPathConstants.OnPatientPath;
            var query = HttpUtility.ParseQueryString(string.Empty);

            if (!string.IsNullOrEmpty(name))
            {
                query["name"] = name;
            }
            if (gender != null)
            {
                query["gender"] = gender.ToString().ToLower();
            }
            if (!string.IsNullOrEmpty(yearOfBirth))
            {
                query["birthdate"] = yearOfBirth;
            }
            if (query.ToString() != "")
            {
                path = $"{path}/?{query}";
            }

            var patients = new List <Patient>();
            var response = await openMrsClient.GetAsync(path);

            var content = await response.Content.ReadAsStringAsync();

            var bundle = new FhirJsonParser().Parse <Bundle>(content);

            bundle.Entry.ForEach(entry =>
            {
                if (entry.Resource.ResourceType.Equals(ResourceType.Patient))
                {
                    patients.Add((Patient)entry.Resource);
                }
            });

            return(patients);
        }