API to help make public data easier to access. Idea started with need for King County, WA COVID-19 data in API format while only having an excel file for source data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.9 KiB

  1. import flask
  2. from flask import request, jsonify
  3. import psycopg2
  4. import json
  5. app = flask.Flask(__name__)
  6. app.config["DEBUG"] = True
  7. import creds
  8. # Assign secrets to variables, should look into making this
  9. # a function. Plan to move this to Vault from creds file.
  10. dbhost = creds.dblogin['host']
  11. database = creds.dblogin['db']
  12. username = creds.dblogin['username']
  13. password = creds.dblogin['password']
  14. port = creds.dblogin['port']
  15. @app.route('/', methods=['GET'])
  16. def home():
  17. # TODO: Look into putting all this into a template or something.
  18. return """<h1>Public API offered by the team at Seattle Matrix</h1> <p>This \
  19. site is a collection of api's from various public sources collected \
  20. by the team at <a href="https://seattlematrix.org" target="_blank">SeattleMatrix.org</a>.</p> Current Data: \
  21. </br><a href="https://api.seattlematrix.org/v1/kingcounty/covid-19/zipcode/all" target="_blank">King County, WA Covid-19 By ZipCode</a> - Current Dates: 20200810, 20200824, 20200831
  22. """
  23. # A route to return all of the available records for King County COVID-19
  24. @app.route('/v1/kingcounty/covid-19/zipcode/all', methods=['GET'])
  25. def api_zipcode_all():
  26. """ This route returns all the data in the king_county_covid19_zip
  27. table. This data is currently manually updated.
  28. """
  29. conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port} sslmode='require'")
  30. cur = conn.cursor()
  31. cur.execute("select row_to_json(king_county_covid19_zip) from king_county_covid19_zip;")
  32. rows = cur.fetchall()
  33. # FIXME: returning row throws an error and jsonify returns double nested
  34. # json result need to figure this out.
  35. return jsonify(rows)
  36. if __name__ == "__main__":
  37. app.run(host='0.0.0.0')
  38. # TODO: Create route for filtering on zipcode and date
  39. # TODO: Put all above into function for covid-19 data
  40. # TODO: Look into making a Class for db connection