def update_animal(): if request.method == 'POST': #get Animal_ID, the only required query parameter animalid = str(request.args.get('Animal_ID')) #raise exception if user does not provide Animal_ID if not animalid: raise Exception('Animal_ID must be one of the query parameters') #iterate over all keys in raw data db for key in rd1.keys(): #return "checking for animal id match" #find animal that matches the Animal_ID parameter if str(rd1.hget(key, 'Animal_ID'))[1:] == "'" + animalid + "'": #return "found animal match" #iterate over all field:value pairs passed in the query string for field, value in request.args.items(): #skip the Animal_ID field if field == 'Animal_ID': pass #update the database for any other field that is passed in the query else: rd1.hset(key, field, value) return "You have edited animal " + animalid else: return "cannot find animal" else: return """
def add_animal(): if request.method == 'POST': animal_dict = request.get_json(force=True) rd1.hset(rd1.dbsize(), animal_dict) return "Added new animal with ID = " + animal_dict['Animal_ID'] else: return """
def add_animal(): if method == 'POST' animal_dict = request.get_json(force=True) rd1.hset( rd1.dbsize(), animal_dict ) return f'Added new animal with ID = {animal_dict['Animal_ID']}' else: return """ Assemble and post a json structure like this: curl -X POST -H "Content-type: application/json" -d @file.json host-ip:5000/add_animal Where 'file.json' is a file containing: { "Animal_ID": "A781976", "Name": "*Fancy", "DateTime": "10/16/2018 14:25", "Date_of_Birth": "10/8/2017", "Outcome_Type": "Transfer", "Outcome_Subtype": "Partner", "Animal_Type": "Dog", "Sex_upon_Outcome": "Intact Female", "Age_upon_Outcome": "1 year", "Breed": "German Shepherd Mix", "Color": "White" } """ # this is a read route @app.route('/get_animal',methods=['GET']) def get_id_animal(): animalid = str(request.args.get('Animal_ID')) test = get_data() return json.dumps([x for x in test if x['Animal_ID'] == "'"+animalid+"'"]) # need an update route @app.route('/update_animal', methods=['GET', 'POST']) def update_animal(): if method == 'POST': animalid = str(request.args.get('Animal_ID')) field_to_update = request.args.keys() # joe look this up for key in rd1.keys(): if rd1.hget(key, 'Animal_ID') == animalid: rd1.hset(key, field_to_update, value_to_update) else: return """ Try a curl command like: curl localhost:5000/update_animal?Animal_ID=A643424&Animal_Type=Dog """ # need a delete route @app.route('/delete_animal', methods=['DELETE']) def delete_animal(): if method == 'DELETE': animal_to_delete = request.args.get('Animal_ID') for key in rd1.keys(): if rd1.hget(key, 'Animal_ID') == animal_to_delete: rd1.hdel(key) # or maybe rd1.delete() @app.route('/get_results',methods=['GET']) def get_result(): jid = str(request.args.get('Job_ID')) return json.dumps(jobs.get_result(jid)) @app.route('/jobs', methods=['POST']) def jobs_api(): try: job = request.get_json(force=True) except Exception as e: return True, json.dumps({'status': "Error", 'message': 'Invalid JSON: {}.'.format(e)}) return json.dumps(jobs.add_job(job['start'], job['end'])) # user should do a curl request like: # curl ip_address:5000/jobs -X POST -H "content-type: application/json" -d '{"start": "9/26/2018", "end": "9/26/2019"}' @app.route('/download/<jobid>', methods=['GET']) def download(jobid): path = f'/app/{jobid}.png' with open(path, 'wb') as f: f.write(rd.hget(jobid, 'image')) return send_file(path, mimetype='image/png', as_attachment=True) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
def update_animal(): if method == 'POST': animalid = str(request.args.get('Animal_ID')) field_to_update = request.args.keys() # joe look this up for key in rd1.keys(): if rd1.hget(key, 'Animal_ID') == animalid: rd1.hset(key, field_to_update, value_to_update) else: return """ Try a curl command like: curl localhost:5000/update_animal?Animal_ID=A643424&Animal_Type=Dog """ # need a delete route @app.route('/delete_animal', methods=['DELETE']) def delete_animal(): if method == 'DELETE': animal_to_delete = request.args.get('Animal_ID') for key in rd1.keys(): if rd1.hget(key, 'Animal_ID') == animal_to_delete: rd1.hdel(key) # or maybe rd1.delete() @app.route('/get_results',methods=['GET']) def get_result(): jid = str(request.args.get('Job_ID')) return json.dumps(jobs.get_result(jid)) @app.route('/jobs', methods=['POST']) def jobs_api(): try: job = request.get_json(force=True) except Exception as e: return True, json.dumps({'status': "Error", 'message': 'Invalid JSON: {}.'.format(e)}) return json.dumps(jobs.add_job(job['start'], job['end'])) # user should do a curl request like: # curl ip_address:5000/jobs -X POST -H "content-type: application/json" -d '{"start": "9/26/2018", "end": "9/26/2019"}' @app.route('/download/<jobid>', methods=['GET']) def download(jobid): path = f'/app/{jobid}.png' with open(path, 'wb') as f: f.write(rd.hget(jobid, 'image')) return send_file(path, mimetype='image/png', as_attachment=True) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')