-
Notifications
You must be signed in to change notification settings - Fork 1
/
caller.py
48 lines (34 loc) · 1.28 KB
/
caller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def sda(q=str, meta=False):
"""accessing Soil Data Access POST REST API
:str q: query sent to doil data access
:bool meta: instructions for returning column information
:return: python dictionary"""
import requests, json
from json.decoder import JSONDecodeError
try:
theURL = "https://sdmdataaccess.nrcs.usda.gov"
theURL = theURL + "/Tabular/SDMTabularService/post.rest"
rDic = {}
if meta:
rDic["format"] = "JSON+COLUMNNAME+METADATA"
else:
rDic["format"] = "JSON+COLUMNNAME"
rDic["query"] = q
rData = json.dumps(rDic)
results = requests.post(data=rData, url=theURL)
data = results.json()
# cols = qData.get('Table')[0]
# data = qData.get('Table')[1:]
return True, data
except JSONDecodeError as e:
msg = e
print(msg)
return False, msg
except requests.exceptions.RequestException as e:
# msg = e.msg
# print('Requests error: ' + msg)
return False, e
except Exception as e:
msg = 'Unhadled error in Soil Data Access caller ' + e
print(msg)
return False, msg