33import requests
44import pandas as pd
55import pprint
6+ import os
67
78from regcensus .cache import Memoized
89
910pp = pprint .PrettyPrinter ()
1011
1112date_format = re .compile (r'\d{4}(?:-\d{2}-\d{2})?' )
1213
13- URL = 'https://api.quantgov.org'
14+ try :
15+ APIKEY = os .environ ['REGCENSUS_KEY' ]
16+ except KeyError :
17+ print ('ERROR: Set your API key into an environment variable called REGCENSUS_KEY' )
18+
19+ URL = 'https://yhjle3rrc4.execute-api.us-east-1.amazonaws.com/live'
1420
1521
1622def get_values (series , jurisdiction , year , documentType = 1 , summary = True ,
@@ -218,7 +224,7 @@ def get_values(series, jurisdiction, year, documentType=1, summary=True,
218224
219225 # Puts flattened JSON output into a pandas DataFrame
220226 try :
221- json_output = requests . get (url_call ). json ( )
227+ json_output = get_json (url_call )
222228 output = json_normalize (json .loads (json_output ))
223229 # Prints error message if call errors
224230 except TypeError :
@@ -233,8 +239,8 @@ def get_values(series, jurisdiction, year, documentType=1, summary=True,
233239 if verbose :
234240 print (f'Output truncated, found page { page } ' )
235241 page += 1
236- output = json_normalize (json . loads ( requests . get (
237- url_call + f'&page={ page } ' ). json ( )))
242+ output = json_normalize (
243+ json . loads ( get_json ( url_call + f'&page={ page } ' )))
238244 full_output = pd .concat ([full_output , output ])
239245 output = full_output
240246
@@ -283,14 +289,12 @@ def get_datafinder(jurisdiction, documentType=None):
283289 along with the endpoints to access the data
284290 """
285291 if documentType :
286- output = clean_columns (json_normalize (json .loads (requests . get (
292+ output = clean_columns (json_normalize (json .loads (get_json (
287293 URL + (f'/datafinder?jurisdiction={ jurisdiction } &'
288- f'documenttype={ documentType } ' )
289- ).json ())))
294+ f'documenttype={ documentType } ' )))))
290295 else :
291- output = clean_columns (json_normalize (json .loads (requests .get (
292- URL + f'/datafinder?jurisdiction={ jurisdiction } '
293- ).json ())))
296+ output = clean_columns (json_normalize (json .loads (get_json (
297+ URL + f'/datafinder?jurisdiction={ jurisdiction } ' ))))
294298 return output .rename ({
295299 'jurisdiction_id' : 'jurisdiction' ,
296300 'document_type_id' : 'documentType' ,
@@ -334,7 +338,7 @@ def get_series(verbose=0):
334338 """
335339 url_call = series_url (verbose )
336340 return clean_columns (json_normalize (
337- json .loads (requests . get (url_call ). json ( ))))
341+ json .loads (get_json (url_call ))))
338342
339343
340344@Memoized
@@ -350,7 +354,7 @@ def get_agencies(jurisdictionID=None, keyword=None, verbose=0):
350354 if not url_call :
351355 return
352356 return clean_columns (json_normalize (
353- json .loads (requests . get (url_call ). json ( ))))
357+ json .loads (get_json (url_call ))))
354358
355359
356360@Memoized
@@ -364,7 +368,7 @@ def get_jurisdictions(verbose=0):
364368 """
365369 url_call = jurisdictions_url (verbose )
366370 return clean_columns (json_normalize (
367- json .loads (requests . get (url_call ). json ( ))))
371+ json .loads (get_json (url_call ))))
368372
369373
370374@Memoized
@@ -381,7 +385,7 @@ def get_industries(keyword=None, labellevel=3, labelsource=None, verbose=0):
381385 """
382386 url_call = industries_url (keyword , labellevel , labelsource , verbose )
383387 return clean_columns (json_normalize (
384- json .loads (requests . get (url_call ). json ( ))))
388+ json .loads (get_json (url_call ))))
385389
386390
387391@Memoized
@@ -432,17 +436,16 @@ def get_versions(jurisdictionID, documentType=1, verbose=0):
432436 if verbose :
433437 print (f'API call: { url_call } ' )
434438 return clean_columns (json_normalize (
435- json .loads (requests . get (url_call ). json ( ))))
439+ json .loads (get_json (url_call ))))
436440
437441
438442@Memoized
439443def get_documentation ():
440444 """
441445 Get documentation for projects, including citations.
442446 """
443- return clean_columns (json_normalize (json .loads (requests .get (
444- URL + '/documentation'
445- ).json ())))
447+ return clean_columns (json_normalize (
448+ json .loads (get_json (URL + '/documentation' ))))
446449
447450
448451@Memoized
@@ -458,7 +461,7 @@ def list_document_types(jurisdictionID=None, reverse=False, verbose=0):
458461 url_call = URL + '/documenttypes'
459462 if verbose :
460463 print (f'API call: { url_call } ' )
461- content = json .loads (requests . get (url_call ). json ( ))
464+ content = json .loads (get_json (url_call ))
462465 if reverse :
463466 return dict (sorted ({
464467 d ["document_type_id" ]: d ["document_type" ]
@@ -479,7 +482,7 @@ def list_series(reverse=False, verbose=0):
479482 Returns: dictionary containing names of series and associated IDs
480483 """
481484 url_call = series_url (verbose )
482- content = json .loads (requests . get (url_call ). json ( ))
485+ content = json .loads (get_json (url_call ))
483486 if reverse :
484487 return dict (sorted ({
485488 s ["series_id" ]: s ["series_name" ]
@@ -560,7 +563,7 @@ def list_clusters(reverse=False):
560563 Returns: dictionary containing names of clusters and associated IDs
561564 """
562565 url_call = URL + '/clusters'
563- content = json .loads (requests . get (url_call ). json ( ))
566+ content = json .loads (get_json (url_call ))
564567 if reverse :
565568 return dict (sorted ({
566569 a ["agency_cluster" ]: a ["cluster_name" ]
@@ -577,7 +580,7 @@ def list_jurisdictions(reverse=False):
577580 Returns: dictionary containing names of jurisdictions and associated IDs
578581 """
579582 url_call = jurisdictions_url ()
580- content = json .loads (requests . get (url_call ). json ( ))
583+ content = json .loads (get_json (url_call ))
581584 if reverse :
582585 return dict (sorted ({
583586 j ["jurisdiction_id" ]: j ["jurisdiction_name" ]
@@ -602,7 +605,7 @@ def list_industries(
602605 Returns: dictionary containing names of industries and associated IDs
603606 """
604607 url_call = industries_url (keyword , labellevel , labelsource )
605- content = json .loads (requests . get (url_call ). json ( ))
608+ content = json .loads (get_json (url_call ))
606609 # If industry has codes, include the code in the key
607610 try :
608611 if onlyID :
@@ -680,6 +683,10 @@ def industries_url(keyword, labellevel, labelsource, verbose=0):
680683 return url_call
681684
682685
686+ def get_json (url_call ):
687+ return requests .get (url_call , headers = {"x-api-key" : APIKEY }).json ()
688+
689+
683690def clean_columns (df ):
684691 """Removes prefixes from column names"""
685692 df .columns = [c .split ('v_' )[- 1 ] for c in df .columns ]
0 commit comments