Skip to content

Commit a05d99b

Browse files
committed
[Feature] Add doctors api
1 parent ebb73e3 commit a05d99b

File tree

10 files changed

+112
-8
lines changed

10 files changed

+112
-8
lines changed

healthtools_ke_api/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from flask import Flask, jsonify
22
from werkzeug.exceptions import HTTPException, default_exceptions
33

4-
from healthtools_ke_api.nurses import nurses_api
5-
from healthtools_ke_api.sms_handler import sms_handler
4+
from healthtools_ke_api.views.doctors import doctors_api
5+
from healthtools_ke_api.views.nurses import nurses_api
6+
from healthtools_ke_api.views.sms_handler import sms_handler
67

78
import os
89
import sys
@@ -15,6 +16,7 @@
1516
print "No config has been specified for use in the environment variables."
1617
sys.exit()
1718

19+
app.register_blueprint(doctors_api, url_prefix='/doctors')
1820
app.register_blueprint(nurses_api, url_prefix='/nurses')
1921
app.register_blueprint(sms_handler)
2022

healthtools_ke_api/config.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

healthtools_ke_api/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
# Url of memcached server
44
MEMCACHED_URL = os.getenv("MEMCACHED_URL")
55

6+
# Amazon Web Services configs
7+
AWS_CONFIGS = {
8+
"aws_access_key_id": os.getenv("AWS_ACCESS_KEY_ID"),
9+
"aws_secret_access_key": os.getenv("AWS_SECRET_KEY"),
10+
"region_name": os.getenv("AWS_REGION"),
11+
}
12+
613

714
class Config(object):
815
# Google Analytics tracking id
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.doctors import get_doctors_from_cloudsearch
4+
5+
6+
class TestNursesAPI(TestCase):
7+
def setUp(self):
8+
self.client = app.test_client()
9+
10+
def test_gets_doctors_from_cloudsearch(self):
11+
doctors = get_doctors_from_cloudsearch("Marie")
12+
self.assertTrue(len(doctors) > 0)
13+
14+
def test_doctors_endpoint_with_bad_query(self):
15+
response = self.client.get("/doctors/search.json?q=")
16+
self.assertIn("A query is required.", response.data)
17+
18+
def test_doctors_endpoint_gets_doctors(self):
19+
response = self.client.get("/doctors/search.json?q=Marie")
20+
self.assertIn("success", response.data)

healthtools_ke_api/tests/test_nurses_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22
from healthtools_ke_api import app
3-
from healthtools_ke_api.nurses import get_nurses_from_nc_registry
3+
from healthtools_ke_api.views.nurses import get_nurses_from_nc_registry
44

55

66
class TestNursesAPI(TestCase):
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
doctors_api = Blueprint('doctors_api', __name__)
9+
DOCTORS_CLOUDSEARCH_ENDPOINT = "http://doc-cfa-healthtools-ke-doctors-m34xee6byjmzcgzmovevkjpffy.eu-west-1.cloudsearch.amazonaws.com/"
10+
cloudsearch_client = boto3.client("cloudsearchdomain",
11+
endpoint_url=DOCTORS_CLOUDSEARCH_ENDPOINT,
12+
**AWS_CONFIGS)
13+
14+
15+
@doctors_api.route('/', methods=['GET'])
16+
def index():
17+
'''
18+
Landing endpoint
19+
'''
20+
msg = {
21+
"name": "API to doctors registry on AWS Cloudsearch",
22+
"authentication": [],
23+
"endpoints": {
24+
"/": {"methods": ["GET"]},
25+
"/doctors/search.json": {
26+
"methods": ["GET"],
27+
"args": {
28+
"q": {"required": True}
29+
}
30+
},
31+
}
32+
}
33+
return jsonify(msg)
34+
35+
36+
@doctors_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": {"doctors": []}
45+
})
46+
47+
# get doctors by that name from aws
48+
response = {}
49+
doctors = get_doctors_from_cloudsearch(query)
50+
51+
if not doctors:
52+
response["message"] = "No doctor by that name found."
53+
54+
response["data"] = {"doctors": doctors}
55+
response["status"] = "success"
56+
57+
results = jsonify(response)
58+
return results
59+
except Exception as err:
60+
return jsonify({
61+
"status": "error",
62+
"message": str(err),
63+
"data": {"doctors": []}
64+
})
65+
66+
67+
def get_doctors_from_cloudsearch(query):
68+
'''
69+
Get doctors from AWS cloudsearch
70+
'''
71+
results = cloudsearch_client.search(query=query)
72+
return results["hits"]["hit"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import Blueprint, request, current_app
22

33
from healthtools_ke_api.analytics import track_event
4-
from healthtools_ke_api.nurses import get_nurses_from_nc_registry
4+
from healthtools_ke_api.views.nurses import get_nurses_from_nc_registry
55

66
import requests
77
import re

requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
appdirs==1.4.3
22
beautifulsoup4==4.5.3
3+
boto3==1.4.4
4+
botocore==1.5.42
35
bs4==0.0.1
46
click==6.7
7+
docutils==0.13.1
58
Flask==0.12.1
9+
futures==3.1.1
610
gunicorn==19.7.1
711
itsdangerous==0.24
812
Jinja2==2.9.6
13+
jmespath==0.9.2
914
MarkupSafe==1.0
1015
nose==1.3.7
1116
packaging==16.8
1217
pyparsing==2.2.0
18+
python-dateutil==2.6.0
1319
python-memcached==1.58
1420
requests==2.13.0
21+
s3transfer==0.1.10
1522
six==1.10.0
1623
Werkzeug==0.12.1

0 commit comments

Comments
 (0)