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.

46 lines
1.9 KiB

import flask
from flask import request, jsonify
import psycopg2
import json
app = flask.Flask(__name__)
app.config["DEBUG"] = True
import creds
# Assign secrets to variables, should look into making this
# a function. Plan to move this to Vault from creds file.
dbhost = creds.dblogin['host']
database = creds.dblogin['db']
username = creds.dblogin['username']
password = creds.dblogin['password']
port = creds.dblogin['port']
@app.route('/', methods=['GET'])
def home():
# TODO: Look into putting all this into a template or something.
return """<h1>Public API offered by the team at Seattle Matrix</h1> <p>This \
site is a collection of api's from various public sources collected \
by the team at <a href="https://seattlematrix.org" target="_blank">SeattleMatrix.org</a>.</p> Current Data: \
</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
# A route to return all of the available records for King County COVID-19
@app.route('/v1/kingcounty/covid-19/zipcode/all', methods=['GET'])
def api_zipcode_all():
""" This route returns all the data in the king_county_covid19_zip
table. This data is currently manually updated.
conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port} sslmode='require'")
cur = conn.cursor()
cur.execute("select row_to_json(king_county_covid19_zip) from king_county_covid19_zip;")
rows = cur.fetchall()
# FIXME: returning row throws an error and jsonify returns double nested
# json result need to figure this out.
return jsonify(rows)
if __name__ == "__main__":
# TODO: Create route for filtering on zipcode and date
# TODO: Put all above into function for covid-19 data
# TODO: Look into making a Class for db connection