HARPgroup / pydro-tools

0 stars 0 forks source link

REST Requests in Python #15

Open jdkleiner opened 2 years ago

jdkleiner commented 2 years ago

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 commented 1 year ago

This is working well! Check out this tiny demo: https://github.com/HARPgroup/HSPsquared/issues/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 commented 1 year ago

@rburghol Awesome!