Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST Requests in Python #15

Open
jdkleiner opened this issue Oct 7, 2022 · 2 comments
Open

REST Requests in Python #15

jdkleiner opened this issue Oct 7, 2022 · 2 comments

Comments

@jdkleiner
Copy link
Member

Just leaving this here in case we ever decide we need REST functionality in Python. The requests module is pretty straight forward and user-friendly, working example below using our standard rest user account:

import requests
from requests.auth import HTTPBasicAuth
import csv
import pandas as pd

# authentication using rest un and pw
basic = HTTPBasicAuth('usernameXXX', 'passwordXXX')

# alternate method, retrieve a token
# response = requests.get('http://deq1.bse.vt.edu:81/d.dh/restws/session/token/', auth=basic)
# print('status code:', response.status_code)
# token = response.content
# print('token:', token) # note: b' at the start of output means it is a reference to a bytes object

# get a remote csv using authentication, and convert to pandas dataframe:
csv_url = 'http://deq1.bse.vt.edu:81/d.dh/precipitation-drought-timeseries-export'
download = requests.get(csv_url, auth=basic)
decoded_content = download.content.decode('utf-8')
cr = csv.reader(decoded_content.splitlines(), delimiter=',')
cr_list = list(cr)
df = pd.DataFrame(cr_list[1:], columns = cr_list[0], dtype = int) 

# retrieve varid
propdef_table = pd.read_csv('http://deq1.bse.vt.edu:81/d.dh/?q=vardefs.tsv/all/drought',sep='\t')
variable_row = propdef_table[propdef_table['varkey'].str.contains('drought_status_precip')]
varid = int(variable_row['varid'])

# setup params
featureid = int(df['hydroid'][0])
pid = int(df['pid'][0])

PARAMS = {'featureid':featureid,
          'varid':varid,
          'pid':pid,
          'entity_type':'dh_feature'}

# get a property
prop = requests.get(url = 'http://deq1.bse.vt.edu:81/d.dh/dh_properties.json', params = PARAMS, auth=basic)
prop = prop.json() # return json object
prop = prop["list"][0] # access dict within list
print(prop)
print("propcode =", prop['propcode']) # access value using dict key

@rburghol

@rburghol
Copy link

This is working well! Check out this tiny demo: HARPgroup/HSPsquared#57

Note: On windows we may need:

/c/usr/local/bin/python -m pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org  requests

@jdkleiner
Copy link
Member Author

@rburghol Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants