Skip to content
Permalink
Browse files

Change up project structure.

  • Loading branch information...
DavidLemayian committed Apr 18, 2017
1 parent 7e00320 commit 71f5ac137c71b6a08afe6dfea03f80fa7fa59088
@@ -1,5 +1,5 @@
FROM python:2.7.13
COPY . /api-healthtools
WORKDIR /api-healthtools
COPY . /healthtools-ke-api
WORKDIR /healthtools-ke-api
RUN pip install -r requirements.txt
CMD ["gunicorn", "api_healthtools_ke.nurses:app"]
CMD ["gunicorn", "healthtools_ke_api:app"]
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 David Lemayian
Copyright (c) 2017 Code for Africa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
No changes.
No changes.

This file was deleted.

@@ -0,0 +1,25 @@
from flask import Flask, jsonify
from healthtools_ke_api.nurses import nurses_api, index as nurses_index


app = Flask(__name__)
app.register_blueprint(nurses_api, url_prefix='/nurses')


@app.route("/")
def index():
'''
Landing endpoint
'''
msg = {
"name": "HealthTools.KE-API",
"authentication": [],
"endpoints": {
"/": {"methods": ["GET"]},
"/nurses": {"methods": ["GET"]}
}
}
return jsonify(msg)

if __name__ == "__main__":
app.run(debug=True)
@@ -0,0 +1,29 @@
import requests


def track_event(tracking_id, category, action, cid, label=None, value=0):
'''
Posts Tracking in info to Google Analytics using measurement protocol.
Args:
tracking_id: The tracking ID of the Google Analytics account in which these data is associated with.
category: The name assigned to the group of similar events to track.
action: The Specific action being tracked.
cid: Anonymous Client Identifier. Ideally, this should be a UUID that is associated with particular user, device
label: Label of the event.
value: Value of event in this case number of results obtained
Returns:
No return value # If the request fails, it will raise a RequestException. .
'''
data = {
'v': '1',
'tid': tracking_id,
'cid': cid,
't': 'event',
'ec': category,
'ea': action,
'el': label,
'ev': value,
}
response = requests.post(
'http://www.google-analytics.com/collect', data=data)
response.raise_for_status()
@@ -0,0 +1,4 @@
import os

MEMCACHED_URL = os.getenv('MEMCACHED_URL', '127.0.0.1:11211')
GA_TRACKING_ID = os.environ.get('GA_TRACKING_ID', 'UA-44795600-33')
@@ -1,28 +1,29 @@
from bs4 import BeautifulSoup
from api_healthtools_ke.config import NURSING_COUNCIL_URL, MEMCACHED_URL, GA_TRACKING_ID
from flask import Flask, request, jsonify, make_response, json
from werkzeug.exceptions import HTTPException, default_exceptions
import requests
import memcache

from bs4 import BeautifulSoup
from flask import Blueprint, request, jsonify, make_response, json
from werkzeug.exceptions import HTTPException, default_exceptions
from memcache import Client

app = Flask(__name__)
cache = memcache.Client([(MEMCACHED_URL)], debug=True) # cache server
from healthtools_ke_api.config import *
from healthtools_ke_api.analytics import track_event

nurses_api = Blueprint('nurses_api', __name__)
nurse_fields = ["name", "licence_no", "valid_till"]
cache = Client([(MEMCACHED_URL)], debug=True)


@app.route('/', methods=['GET'])
def home():
@nurses_api.route('/', methods=['GET'])
def index():
'''
Landing endpoint
'''
msg = {
"name": "Nursing Council of Kenya API",
"authentication": [],
"endpoints": {
"/": {"methods": ["GET"]},
"/nurses": {
"/nurses": {"methods": ["GET"]},
"/nurses/search.json": {
"methods": ["GET"],
"args": {
"q": {"required": True}
@@ -33,8 +34,8 @@ def home():
return jsonify(msg)


@app.route('/nurses', methods=['GET'])
def find_nurse():
@nurses_api.route('/search.json', methods=['GET'])
def search():
try:
query = request.args.get('q')
if not query or len(query) < 1:
@@ -79,11 +80,16 @@ def find_nurse():
entry = dict(zip(nurse_fields, columns))
entries.append(entry)

# send action to google analytics
track_event(GA_TRACKING_ID, 'Nurse', 'search',
request.remote_addr, label=query, value=len(entries))
results = jsonify({"status": "success", "data": {"nurses": entries}})
# Cache the results
cache.set(query, results, time=345600) # expire after 4 days

# Send action to google analytics
track_event(
GA_TRACKING_ID, 'Nurse', 'search', request.remote_addr,
label=query, value=len(entries))

results = jsonify({"status": "success", "data": {"nurses": entries}})

return results

except Exception as err:
@@ -93,34 +99,6 @@ def find_nurse():
})


def track_event(tracking_id, category, action, cid, label=None, value=0):
'''
Posts Tracking in info to Google Analytics using measurement protocol.
Args:
tracking_id: The tracking ID of the Google Analytics account in which these data is associated with.
category: The name assigned to the group of similar events to track.
action: The Specific action being tracked.
cid: Anonymous Client Identifier. Ideally, this should be a UUID that is associated with particular user, device
label: Label of the event.
value: Value of event in this case number of results obtained
Returns:
No return value # If the request fails, it will raise a RequestException. .
'''
data = {
'v': '1',
'tid': tracking_id,
'cid': cid,
't': 'event',
'ec': category,
'ea': action,
'el': label,
'ev': value,
}
response = requests.post(
'http://www.google-analytics.com/collect', data=data)
response.raise_for_status()


def handle_error(error):
'''Generic error handlers for all http exceptions'''
response = {}
@@ -135,7 +113,4 @@ def handle_error(error):

# change error handler for all http exceptions to return json instead of html
for code in default_exceptions.keys():
app.errorhandler(code)(handle_error)

if __name__ == "__main__":
app.run(port=5555)
nurses_api.errorhandler(code)(handle_error)
@@ -0,0 +1,11 @@
from setuptools import setup

setup(
name='healthtools_ke_api',
version='0.0.1',
packages=['healthtools_ke_api'],
include_package_data=True,
install_requires=[
'flask',
],
)

0 comments on commit 71f5ac1

Please sign in to comment.
You can’t perform that action at this time.