Browse Source

added first filtering feature of API, and made the testing data look like what the data in DB will once that is completed.

kc-covid19-zipcode
prasket 2 months ago
parent
commit
812de9cad9
1 changed files with 66 additions and 8 deletions
  1. +66
    -8
      api.py

+ 66
- 8
api.py View File

@@ -4,19 +4,49 @@ from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True

# Create some test data
zipcode = [
# Create some test data this will eventually be in a Postgresql db.
zipcodes = [
{'id': 0,
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98121',
'population': '2123223'
'population': '22274',
'tests': '3182',
'test_rate': '142.86',
'positives': '64',
'positive_rate': '2.87',
'hospitalizations': '4',
'hospitalization_rate': '0.18',
'deaths INTEGER': '2',
'death_rate':'0.09'
},
{'id': 1,
'zipcode': '98122',
'population': '33244'
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98109',
'population': '34939',
'tests': '6142',
'test_rate': '175.79',
'positives': '206',
'positive_rate': '5.9',
'hospitalizations': '38',
'hospitalization_rate': '1.09',
'deaths INTEGER': '19',
'death_rate':'0.54'
},
{'id': 2,
'zipcode': '98123',
'population': '2134'
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98116',
'population': '26979',
'tests': '5259',
'test_rate': '194.93',
'positives': '121',
'positive_rate': '4.48',
'hospitalizations': '5',
'hospitalization_rate': '0.19',
'deaths INTEGER': '2',
'death_rate':'0.07'
}
]

@@ -30,6 +60,34 @@ def home():
# A route to return all of the available entries in our catalog.
@app.route('/v1/kingcounty/covid-19/zipcode/all', methods=['GET'])
def api_all():
return jsonify(zipcode)
return jsonify(zipcodes)

@app.route('/v1/kingcounty/covid-19/zipcode/', methods=['GET'])
def api_zipcode():
# Check to see if 'zipcode' was provided in URL
# If no zipcode display message stating this
if 'zipcode' in request.args:
provided_zipcode = request.args['zipcode']
else:
return "Error: No zipcode field provided. Please specify an zipcode."

# Create an empty list for our results
results = []

# Loop through the data and match results that fit the requested Zipcode.
# zipcodes are unique, but other fields might return many results
for zipcode in zipcodes:
if zipcode['zipcode'] == provided_zipcode:
results.append(zipcode)

# If no zipcodes matched and results are empty show message rather than
# empty page, maybe this isnt good to do in an API that is going to be used
# for human and computer consumption, need to check into that.
if not results:
results = "The Zipcode you entered was not in the API, please try again"

# Use the jsonify function from Flask to convert our list of
# Python dictionaries to the JSON format.
return jsonify(results)

app.run()

Loading…
Cancel
Save