<jupyter_start><jupyter_text>Testing api for curing<jupyter_code>import requests

get_user=requests.get("http://localhost:5000/api/users")
users=get_user.json()['users']
print(users)
get_bus=requests.get("http://localhost:5000/api/bu")
bus=get_bus.json()['bus']
print(bus)
get_comps=requests.get("http://localhost:5000/api/compounds")
compounds=get_comps.json()['comps']
print(compounds)
get_comps.content
uparam={'id':3,'name':'ABCDE'}
user_response=requests.post("http://localhost:5000/api/users", json=uparam)
user_response
uparam={'id':5,'name':'John5'}
user_response=requests.put("http://localhost:5000/api/users/"+str(uparam['id']), json=uparam)
user_response
udel= User.query.filter_by(name='ABCDE').first()
print(udel)
user_response=requests.delete("http://localhost:5000/api/users/"+str(udel.id))
user_response
get_response=requests.get("http://localhost:5000/api/cures")#, params=parameters)
cures=get_response.json()['cures']
cures
import json
parameters={'user_id':2,'name':'ABC_BG','a':1.0,'b':3.0,'description':'my comp name entry', 'bu_id':3,'comp_id':2}
#pstr='user_id=1 name="ABCD" a=1.0 b=2.0 description="no description"'
#xx=json.dumps(parameters)
post_response=requests.post("http://localhost:5000/api/cures", json=parameters)
post_response.content
print(dir(post_response))
post_response.status_code
get_response.content
import pandas as pd
# get as pandas dataFrame
def get_list_of_compounds():
    #cures=get_response.json()['cures']
    get_response=requests.get("http://localhost:5000/api/cures")
    #df=pd.DataFrame(data=cures,index='id')
    df=pd.DataFrame(get_response.json()['cures'])
    return df
    #return [ (c['id'],c['name'],c['description']) for c in cures]
lcc=get_list_of_compounds()    
lcc     
# using filters in query    
def get_list_of_compounds_perUser(user=None):
    if user:
        uname = User.query.filter_by(name=user).first()
        cure=Curedb.query.filter_by(user_id=uname.id) 
        return cure.all()
    else:
        cure=Curedb.query.all()
        return cure
        


lcca=get_list_of_compounds_perUser()   
#print(dir(lccu))
print(lcca)

lccu=get_list_of_compounds_perUser('Kai')
#print(dir(lccu))
for lc in lccu:    
    print(lc.id,lc.name,lc.description)
# alternative request filter in url
get_2=requests.get("http://localhost:5000/api/cures?user_id=2&name=ABCDE")
cures=get_2.json()['cures']
cures
# filter by name, needs objects imported (no flexible solution here)
uname=User.query.filter_by(name='Kai').first()
cure=Curedb.query.filter_by(user_id=uname.id)
print('%d Cure(s) found in db for name: %s'%(cure.count(),uname.name))
for x in cure:
    print(x.id,x.name,x.a)
parameter={'cure_id':1}
get_byid=requests.get("http://localhost:5000/api/cures",json=parameter)
get_byid.content
get_byid.json()
del_byid=requests.delete("http://localhost:5000/api/cures/7",json=parameter)
del_byid.content<jupyter_output><empty_output>