Browse Source

setup method to pass in secrets better than having them in plain text, will be using Hashicorp Vault for secrets long term.

kc-covid19-zipcode
prasket 2 months ago
parent
commit
b6ef797e27
5 changed files with 48 additions and 9 deletions
  1. +2
    -1
      .gitignore
  2. +14
    -5
      api.py
  3. +10
    -1
      create_covid-19_zip.py
  4. +12
    -0
      creds_sample.py
  5. +10
    -2
      load_db.py

+ 2
- 1
.gitignore View File

@@ -1,3 +1,4 @@
.vscode
test.py
output
output
creds.py

+ 14
- 5
api.py View File

@@ -5,6 +5,15 @@ import json

app = flask.Flask(__name__)
app.config["DEBUG"] = True
import creds

# assign secrets to variables
dbhost = creds.dblogin['host']
database = creds.dblogin['db']
username = creds.dblogin['username']
password = creds.dblogin['password']
port = creds.dblogin['port']


# Create some test data this will eventually be in a Postgresql db.
zipcodes = [
@@ -59,16 +68,16 @@ def home():
site is a collection of api's from various public sources collected \
by the team at Seattle Matrix</p>"

# A route to return all of the available entries in our catalog.
# A route to return all of the available records for King County
@app.route('/v1/kingcounty/covid-19/zipcode/all', methods=['GET'])
def api_all():
conn = psycopg2.connect("host='localhost' dbname='apiv1' user='postgres' password='postgres'" )
def api_zipcode_all():
conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port}")
cur = conn.cursor()
cur.execute("select row_to_json(king_county_covid19_zip) from king_county_covid19_zip;")
rv = cur.fetchall()
rows = cur.fetchall()
# returning rv throws an error and jsonify returns double nested
# json result need to figure this out.
return jsonify(rv)
return jsonify(rows)

@app.route('/v1/kingcounty/covid-19/zipcode/', methods=['GET'])
def api_zipcode():


+ 10
- 1
create_covid-19_zip.py View File

@@ -1,5 +1,14 @@
import psycopg2
conn = psycopg2.connect("host='localhost' dbname='apiv1' user='postgres' password='postgres'" )
import creds

# assign secrets to variables
dbhost = creds.dblogin['host']
database = creds.dblogin['db']
username = creds.dblogin['username']
password = creds.dblogin['password']
port = creds.dblogin['port']

conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port}")

cur = conn.cursor()
cur.execute("""


+ 12
- 0
creds_sample.py View File

@@ -0,0 +1,12 @@
""" save this file as creds.py and update with your
db information. I will be updating app to use Vault
for secrets soon.
"""

dblogin = {
'host' : 'localhost',
'port' : '5432',
'username' : 'username',
'password' : 'password',
'db' : 'db'
}

+ 10
- 2
load_db.py View File

@@ -1,13 +1,21 @@
import psycopg2
import csv
from datetime import datetime
import creds

conn = psycopg2.connect("host='localhost' dbname='apiv1' user='postgres' password='postgres'" )
# assign secrets to variables
dbhost = creds.dblogin['host']
database = creds.dblogin['db']
username = creds.dblogin['username']
password = creds.dblogin['password']
port = creds.dblogin['port']

conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port}")

cur = conn.cursor()

# take the source data csv and upload to the database.
# hardcoded to 20200810 right now
# using date variable to choose file
data_date = "20200831"
source_file = "./source_data/covid-19_" +data_date+"_zip.csv"



Loading…
Cancel
Save