Skip to content

Commit 4134816

Browse files
committed
[Feature] Add clinical officers api
1 parent a05d99b commit 4134816

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

healthtools_ke_api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from healthtools_ke_api.views.doctors import doctors_api
55
from healthtools_ke_api.views.nurses import nurses_api
6+
from healthtools_ke_api.views.clinical_officers import clinical_officers_api
7+
68
from healthtools_ke_api.views.sms_handler import sms_handler
79

810
import os
@@ -18,6 +20,7 @@
1820

1921
app.register_blueprint(doctors_api, url_prefix='/doctors')
2022
app.register_blueprint(nurses_api, url_prefix='/nurses')
23+
app.register_blueprint(clinical_officers_api, url_prefix='/clinical-officers')
2124
app.register_blueprint(sms_handler)
2225

2326

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from unittest import TestCase
2+
from healthtools_ke_api import app
3+
from healthtools_ke_api.views.clinical_officers import get_clinical_officers_from_cloudsearch
4+
5+
6+
class TestClinicalOfficersAPI(TestCase):
7+
def setUp(self):
8+
self.client = app.test_client()
9+
10+
def test_gets_cos_from_cloudsearch(self):
11+
clinical_officers = get_clinical_officers_from_cloudsearch("Marie")
12+
self.assertTrue(len(clinical_officers) > 0)
13+
14+
def test_cos_endpoint_with_bad_query(self):
15+
response = self.client.get("/clinical-officers/search.json?q=")
16+
self.assertIn("A query is required.", response.data)
17+
18+
def test_cos_endpoint_gets_doctors(self):
19+
response = self.client.get("/clinical-officers/search.json?q=Marie")
20+
self.assertIn("success", response.data)

healthtools_ke_api/tests/test_doctors_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from healthtools_ke_api.views.doctors import get_doctors_from_cloudsearch
44

55

6-
class TestNursesAPI(TestCase):
6+
class TestDoctorsAPI(TestCase):
77
def setUp(self):
88
self.client = app.test_client()
99

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import boto3
2+
from flask import Blueprint, request, jsonify, current_app
3+
4+
from healthtools_ke_api.analytics import track_event
5+
from healthtools_ke_api.settings import AWS_CONFIGS
6+
7+
8+
clinical_officers_api = Blueprint('clinical_officers_api', __name__)
9+
COS_CLOUDSEARCH_ENDPOINT = "http://doc-cfa-healthtools-ke-cos-nhxtw3w5goufkzram4er7sciz4.eu-west-1.cloudsearch.amazonaws.com/"
10+
cloudsearch_client = boto3.client("cloudsearchdomain",
11+
endpoint_url=COS_CLOUDSEARCH_ENDPOINT,
12+
**AWS_CONFIGS)
13+
14+
15+
@clinical_officers_api.route('/', methods=['GET'])
16+
def index():
17+
'''
18+
Landing endpoint
19+
'''
20+
msg = {
21+
"name": "API to Kenyan Clinical Officers registry",
22+
"authentication": [],
23+
"endpoints": {
24+
"/": {"methods": ["GET"]},
25+
"/clinical-officers/search.json": {
26+
"methods": ["GET"],
27+
"args": {
28+
"q": {"required": True}
29+
}
30+
},
31+
}
32+
}
33+
return jsonify(msg)
34+
35+
36+
@clinical_officers_api.route('/search.json', methods=['GET'])
37+
def search():
38+
try:
39+
query = request.args.get('q')
40+
if not query or len(query) < 1:
41+
return jsonify({
42+
"error": "A query is required.",
43+
"results": "",
44+
"data": {"clinical_officers": []}
45+
})
46+
47+
# get clinical_officers by that name from aws
48+
response = {}
49+
clinical_officers = get_clinical_officers_from_cloudsearch(query)
50+
51+
if not clinical_officers:
52+
response["message"] = "No clinical officer by that name found."
53+
54+
track_event(current_app.config.get('GA_TRACKING_ID'),
55+
'Clinical-Officers', 'search', request.remote_addr,
56+
label=query, value=len(clinical_officers))
57+
response["data"] = {"clinical_officers": clinical_officers}
58+
response["status"] = "success"
59+
60+
results = jsonify(response)
61+
return results
62+
except Exception as err:
63+
return jsonify({
64+
"status": "error",
65+
"message": str(err),
66+
"data": {"clinical_officers": []}
67+
})
68+
69+
70+
def get_clinical_officers_from_cloudsearch(query):
71+
'''
72+
Get clinical officers from AWS cloudsearch
73+
'''
74+
results = cloudsearch_client.search(query=query, size=10000)
75+
return results["hits"]["hit"]

healthtools_ke_api/views/doctors.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def index():
1818
Landing endpoint
1919
'''
2020
msg = {
21-
"name": "API to doctors registry on AWS Cloudsearch",
21+
"name": "API to the Kenyan doctors registry",
2222
"authentication": [],
2323
"endpoints": {
2424
"/": {"methods": ["GET"]},
@@ -51,6 +51,8 @@ def search():
5151
if not doctors:
5252
response["message"] = "No doctor by that name found."
5353

54+
track_event(current_app.config.get('GA_TRACKING_ID'), 'Doctor', 'search',
55+
request.remote_addr, label=query, value=len(doctors))
5456
response["data"] = {"doctors": doctors}
5557
response["status"] = "success"
5658

@@ -68,5 +70,5 @@ def get_doctors_from_cloudsearch(query):
6870
'''
6971
Get doctors from AWS cloudsearch
7072
'''
71-
results = cloudsearch_client.search(query=query)
73+
results = cloudsearch_client.search(query=query, size=10000)
7274
return results["hits"]["hit"]

0 commit comments

Comments
 (0)