-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Would like to poll data for multiple years or sectors, to reduce number of api calls made, how ever with the api currently, I cannot
Here is a code snippet of the change I implemented in my own code to fix
def nass_data(api_key, source_desc=None, sector_desc=None, group_desc=None, commodity_desc=None, short_desc=None, domain_desc=None, agg_level_desc=None, domaincat_desc=None, statisticcat_desc=None, state_name=None, asd_desc=None, county_name=None, region_desc=None, zip_5=None, watershed_desc=None, year=None, freq_desc=None, reference_period_desc=None, format=None, numeric_vals=None):
# get dict of paramaters
inputs = vars()
inputs.pop('api_key') # get rid of api_key in dict for building url, as it is hardcoded below
# set up the url with api key
base_url = "http://quickstats.nass.usda.gov/api/" + 'api_GET?key=' + api_key
# filter dict for non-None values and add actual values to the request
for item in list(inputs):
if inputs[item] == None:
inputs.pop(item)
else:
# make sure the desc inputs are all uppercase, and strings
if type(inputs[item]) == list:
for indvidual_item in inputs[item]:
base_url += '&' + item + '=' + requests.utils.quote(str(indvidual_item).upper())
else:
if item != 'numeric_vals':
inputs[item] = str((inputs[item])).upper()
# add on the url parameters
base_url += '&' + item + '=' + requests.utils.quote(inputs[item]) #encodes unsafe / reserved chars in the user input (such as in ANIMALS & PRODUCTS)
# make the request
r = requests.get(base_url)
# validate the response
status = r.status_code
if status >= 200 and status < 300:
# success
return r.json()
else:
return 'Response code ' + str(status) + ': ' + r.json()['error'][0]
Metadata
Metadata
Assignees
Labels
No labels