Python Post data through command line - python

I am struggling with this a lot. Have gone through the requests API documentation, but not able to execute this perfectly.
I have written a python script, which is meant to GET, POST',and the remaining REST operations through a command line.GETworks fine, and any request from thelocalhost` url gets retrieved.
However, I am unable to POST a new data. For example, the GET method is issued like this:
request.py -r get -f 1 -u http://127.0.0.1:5002/user
and the userID 1's data get displayed in JSON format. user is the name of the database, which contains these operations.
Similarly, I would like to post a new data in JSON format
I tried this command, but I am receiving the error
request.py -r get -f 7 -u http://127.0.0.1:5002/user {'user':'abc','email':xyz#abc.com}
getAddrInfoFailed
The following is the code for creating database and also the requests
from flask import Flask, request
from flask_restful import Resource, Api
import sqlite3
import re
app = Flask(__name__)
api = Api(app)
def validate_email(email):
match = re.search('^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$', email)
if match:
return True
else:
return False
def validate_username(username):
match = re.search('^[A-Za-z0-9_-]{3,20}$', username)
if match:
return True
else:
return False
def validate_id(id):
if id.isdigit():
return True
else:
return False
class Users(Resource):
def get(self, id):
try:
conn = sqlite3.connect('curd.db')
cursor = conn.cursor()
if validate_id(id):
query = cursor.execute("SELECT * FROM user WHERE id=?", [int(id), ])
row = query.fetchone()
if row:
return {'item': {'id': row[0], 'username': row[1], 'email': row[2]}},200
else:
return {'MESSAGE': 'USER NOT FOUND'}, 404
else:
return {'MESSAGE': 'INVALID INPUT'}, 400
except:
conn.rollback()
finally:
conn.commit()
conn.close()
def post(self, id):
try:
conn = sqlite3.connect('curd.db')
cursor = conn.cursor()
usr = request.get_json()
username = usr['username']
email = usr['email']
if validate_id(id) is True and validate_username(username)is True and validate_email(email) is True:
query = cursor.execute("SELECT * FROM user WHERE id=?", [int(id), ])
row = query.fetchone()
if row:
return {'MESSAGE': 'USER ID ALREADY EXISTS'}, 400
else:
cursor.execute("INSERT INTO user(id, username, email) VALUES(?,?,?)", [id, username, email])
return {'response': 'Success'},201
else:
return {'MESSAGE': 'INVALID INPUT'}, 400
except:
conn.rollback()
finally:
conn.commit()
conn.close()
def put(self, id):
try:
conn = sqlite3.connect('curd.db')
cursor = conn.cursor()
usr = request.get_json()
updated_name = usr['username']
updated_email = usr['email']
if validate_id(id) and validate_username(updated_name) and validate_email(updated_email):
query = cursor.execute("SELECT * FROM user WHERE id=?", [int(id), ])
row = query.fetchone()
if row:
cursor.execute("UPDATE user SET username = '" + str(updated_name) + "',email = '" + str(updated_email) + "' WHERE id = %d" % int(id))
return {'response': 'Success'},200
else:
return {'MESSAGE': 'USER NOT FOUND'}, 404
else:
return {'MESSAGE': 'INVALID INPUT'}, 400
except:
conn.rollback()
finally:
conn.commit()
conn.close()
def delete(self, id):
try:
conn = sqlite3.connect('curd.db')
cursor = conn.cursor()
if validate_id(id):
query = cursor.execute("SELECT * FROM user WHERE id=?", [id, ])
row = query.fetchone()
if row:
conn.execute("DELETE FROM user WHERE id = %d" % int(id))
return {'response': 'Success'},200
else:
return {'MESSAGE': 'USER NOT FOUND'}, 404
else:
return {'MESSAGE': 'INVALID INPUT'}, 400
except:
conn.rollback()
finally:
conn.commit()
conn.close()
api.add_resource(Users,'/user/<string:id>',methods=['GET','POST','PUT','DELETE'])
if __name__ == '__main__':
app.run(port=5002, debug=True)
requests.py
import requests
import json
import sys
from optparse import OptionParser, make_option
if len(sys.argv) <= 1:
print('Need at least 1 argument.')
print("Add -h for Help")
sys.exit(1)
parser = OptionParser()
parser.add_option('-r', '--request', help="Request Type['GET','POST','PUT','DELETE']",default="")
parser.add_option('-f', '--id', help='id')
parser.add_option('-u', '--url', help="URL String",default="")
(options, args) = parser.parse_args()
print("ID = "+options.id)
print("Request Type = "+options.request)
print("Request URL = "+options.url)
url = options.url+"/"+options.id
data = ''
payload= {'key1':'value1','key2':'value2'}
if options.request == "get":
response = requests.get(url, data=data)
elif options.request=="post":
response = requests.post(url, params=payload)
else:
print("Invalid Request..!!")
print(response.text)

Related

How to unit test a lambda function written in python which connects to AWS secrets manager and RDS (mySQL)

I would describe myself as a novice developer and am have written a lambda function in python which queries an RDS mySQL database.I coded this program on aws and am now trying to test it locally don't know where to start. I have tried using moto but my limited knowledge is making things very difficult. Any tips or pointers?
import json
import boto3
from botocore.exceptions import ClientError
from rds_config import get_secret
resp_dict = json.loads(get_secret())
rds_client = boto3.client('rds-data')
# Connection Configuration Values
endpoint = resp_dict.get('host')
username = resp_dict.get('username')
password = resp_dict.get('password')
database_name = resp_dict.get('dbname')
port = resp_dict.get('port')
# Group Configuration Values
Group2 = "#email"
connection = pymysql.connect(host=endpoint, user=username, passwd=password, db=database_name, port=port)
cursor = connection.cursor()
def lambda_handler(event, context):
# Dump and load event data
data = json.dumps(event)
y = json.loads(data)
# Get the email and username
email = y['request']['userAttributes']['email']
cognitoUsername = y['userName']
# verify email and username are the same
if email != cognitoUsername:
raise Exception("Username and Email address must be the same")
try:
if checkGroup2(email):
if searchGroup2TableAccountID(email):
return event
else:
raise Exception("Group 2 user is not present in group 2 table")
else:
accountID = searchGroup1TableAccountID(email)
if accountID is not None:
status = searchOnboardingStatusTableOnboardingOutcome(accountID)
if (status is not None) and (status == 'accepted'):
return event
else:
raise Exception("User is not present in onboarding table")
else:
raise Exception("User is not present in Group1 table ")
except ClientError as e:
print(e.response['Error']['Message'])
def checkGroup2User(email):
if "#email" in email:
return True
else:
return False
def searchGroup1TableAccountID(email):
sqlCheckEmail = "SELECT account_ID from group1 where email_address = %s"
cursor.execute(sqlCheckEmail, (email,))
accountID = cursor.fetchone()[0]
return accountID
def searchOnboardingStatusTableOnboardingOutcome(accountID):
sqlCheckStatus = "SELECT onboarding_outcome from onboarding_status where account_ID = %s"
cursor.execute(sqlCheckStatus, accountID)
status = cursor.fetchone()[0]
return status
def searchGroup2TableAccountID(email):
sqlCheckGroup2AccountID = "SELECT account_id from group2_account where user_email_address = %s"
cursor.execute(sqlCheckGroup2AccountID, (email,))
accountID = cursor.fetchone()
return accountID

How to get back a value based by parameter input using Flask Python API

I keep recieving the following error when I try to curl this or go to the URL directly i.e. 127.0.0.1:5000/api/v1/currentuserid/3/ .. cannot see what is wrong with my code:
TypeError: 'NoneType' object is not callable
#app.route('/api/v1/currentuserid/<userid>/', methods=['GET'])
def api_retrieve_useridd(userid):
try:
mycursor = mydb.cursor()
_json = request.get_json()
userid = _json('userid')
if userid and request.method == 'GET':
sqlQuery = "SELECT username FROM tblUser WHERE user_id=%s"
bindData = (userid,)
mycursor.execute(sqlQuery, bindData)
row_headers=[x[0] for x in mycursor.description] #this will extract row headers
rv = mycursor.fetchone()
json_data=[]
json_data.append(dict(zip(row_headers,rv)))
return jsonify(json_data[0])
else:
return not_found()
finally:
mycursor.close()
userid parameter is already passed to the function via Flask route you defined. So you no need to get the value from request.get_json(). You may try below code. By the way, you can enforce the user_id type as int by doing <int:userid> in the route definition.
#app.route('/api/v1/currentuserid/<int:userid>/', methods=['GET'])
def api_retrieve_useridd(userid):
try:
mycursor = mydb.cursor()
if userid and request.method == 'GET':
sqlQuery = "SELECT username FROM tblUser WHERE user_id=%s"
bindData = (userid,)
mycursor.execute(sqlQuery, bindData)
row_headers=[x[0] for x in mycursor.description] #this will extract row headers
rv = mycursor.fetchone()
json_data=[]
json_data.append(dict(zip(row_headers,rv)))
return jsonify(json_data[0])
else:
return not_found()
finally:
mycursor.close()

Having trouble autoincrementing in an API

I built a to-do list API with Flask and SQlite, and now I'm trying to use AUTOINCREMENT for incrementing the id's for the tasks. However, I am getting an error ("Error: NOT NULL constraint failed: incomplete.id") when I try to add something to the list. I'm not sure why, I looked at the sqlite documentation, and I seem to be following. I even tried reformatting the create table statements. I'm not sure what else to do, i'd really appreciate some guidance/advice/help. Thanks!
Here is my helper.py
import helper
from flask import Flask, request, jsonify, Response
import json
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
#app.route('/tasks/new', methods=['PUT'])
def add_task():
# global idCount
# idCount = idCount + 1
# get item from the POST body, request module used to parse request and get HTTP body data. response is used to return response to the client, of type JSON
req_data = request.get_json()
task = req_data['task']
# add task to the list
res_data = helper.add_to_incomplete(task)
# return error if task cant be added
if res_data is None:
response = Response("{'error': 'Task not added - " + task + "'}", mimetype='application/json')
return response;
response = Response(json.dumps(res_data), mimetype='application/json')
return response
#app.route('/tasks/all', methods = ["GET"])
def get_all_items():
res_data = helper.get_all_completes(), helper.get_all_incompletes()
response = Response(json.dumps(res_data), mimetype='application/json')
return response
#app.route('/tasks/complete', methods = ["POST"])
def complete_task():
req_data = request.get_json()
inputId = req_data['id']
res_data = helper.add_to_complete(inputId)
# find matching task to input id
return "completed task" + inputId
#app.route('/tasks/incomplete', methods = ["PATCH"])
def uncomplete_task():
req_data = request.get_json()
inputId = req_data['id']
res_data = helper.uncomplete(inputId)
# find matching task to input id
return "un-completed task" + inputId
#app.route('/tasks/remove', methods = ["DELETE"])
def delete():
req_data = request.get_json()
inputId = req_data['id']
res_data = helper.delete_task(inputId)
if res_data is None:
response = Response("{'error': 'Error deleting task - '" + task + "}", status=400 , mimetype='application/json')
return "deleted task id" + " " + inputId
#app.route('/tasks/empty', methods = ["EMPTY"])
def delete_all():
helper.empty()
return "you deleted everything"
Here is my helper.py:
import sqlite3
import random
#for id's because users dont set them
DB_PATH = './todo.db'
# connect to database
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS complete (id INTEGER PRIMARY KEY, task TEXT NOT NULL);")
# save the change
c.execute("CREATE TABLE IF NOT EXISTS incomplete (id INTEGER PRIMARY KEY, task TEXT NOT NULL);")
conn.commit()
def add_to_incomplete(task):
try:
# id = str(random.randrange(100,999))
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('insert into incomplete(task) values(?)', (task,))
conn.commit()
return {"id": id}
except Exception as e:
print('Error: ', e)
return None
def add_to_complete(inputId):
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('select task from incomplete where id=?', (inputId,))
tasks = c.fetchone()[0]
c.execute('insert into complete values(?,?)', (inputId,tasks))
delete_task(inputId)
conn.commit()
return {"id": id}
except Exception as e:
print('Error: ', e)
return None
def get_all_completes():
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('select * from complete')
rows = c.fetchall()
conn.commit()
return { "complete": rows }
except Exception as e:
print('Error: ', e)
return None
def get_all_incompletes():
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('select * from incomplete')
rows = c.fetchall()
conn.commit()
return { "incomplete": rows }
except Exception as e:
print('Error: ', e)
return None
def uncomplete(inputId):
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('select task from complete where id=?', (inputId,))
tasks = c.fetchone()[0]
c.execute('insert into incomplete values(?,?)', (inputId,tasks))
delete_task(inputId)
conn.commit()
return {"id": id}
except Exception as e:
print('Error: ', e)
return None
def delete_task(inputId):
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('delete from complete where id=?', (inputId,))
c.execute('delete from incomplete where id=?', (inputId,))
conn.commit()
return {"id":id}
except Exception as e:
print('Error: ', e)
return None
def empty():
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('delete from complete')
c.execute('delete from incomplete')
conn.commit()
return "you deleted everything mwahaha"
except Exception as e:
print('Error: ', e)
return None
I would suggest changing your sql table creation code to:
create table if not exists complete
(
id int auto_increment,
constraint complete_pk
primary key (id)
);
However a better option is to use SQLAlchemy

Error 500 when running python flask application with xampp

I am trying to run a flask application with xampp but I can't run it on the web server.
I have added .py to Addhandler in /opt/lampp/etc/httpd.conf and at the top of all my scripts I added this: #!/usr/bin/python
#!/usr/bin/python
import sqlite3, os, hashlib
from flask import Flask, jsonify, render_template, request, g
app = Flask(__name__)
app.database = "sample.db"
#app.route('/')
def index():
return render_template('login.html')
#API routes
#app.route('/api/v1.0/storeLoginAPI/', methods=['POST'])
def loginAPI():
if request.method == 'POST':
uname,pword = (request.json['username'],request.json['password'])
g.db = connect_db()
cur = g.db.execute("SELECT * FROM employees WHERE username = '%s' AND password = '%s'" %(uname,pword))
if cur.fetchone():
result = {'status': 'success'}
else:
result = {'status': 'fail'}
g.db.close()
return jsonify(result)
#app.route('/api/v1.0/storeAPI', methods=['GET', 'POST'])
def storeapi():
if request.method == 'GET':
g.db = connect_db()
curs = g.db.execute("SELECT * FROM shop_items")
cur2 = g.db.execute("SELECT * FROM employees")
items = [{'items':[dict(name=row[0], quantity=row[1], price=row[2]) for row in curs.fetchall()]}]
empls = [{'employees':[dict(username=row[0], password=row[1]) for row in cur2.fetchall()]}]
g.db.close()
return jsonify(items+empls)
elif request.method == 'POST':
g.db = connect_db()
name,quan,price = (request.json['name'],request.json['quantity'],request.json['price'])
curs = g.db.execute("""INSERT INTO shop_items(name, quantitiy, price) VALUES(?,?,?)""", (name, quan, price))
g.db.commit()
g.db.close()
return jsonify({'status':'OK','name':name,'quantity':quan,'price':price})
#app.route('/api/v1.0/storeAPI/<item>', methods=['GET'])
def searchAPI(item):
g.db = connect_db()
#curs = g.db.execute("SELECT * FROM shop_items WHERE name=?", item) #The safe way to actually get data from db
curs = g.db.execute("SELECT * FROM shop_items WHERE name = '%s'" %item)
results = [dict(name=row[0], quantity=row[1], price=row[2]) for row in curs.fetchall()]
g.db.close()
return jsonify(results)
#app.errorhandler(404)
def page_not_found_error(error):
return render_template('error.html', error=error)
#app.errorhandler(500)
def internal_server_error(error):
return render_template('error.html', error=error)
def connect_db():
return sqlite3.connect(app.database)
# Create password hashes
def hash_pass(passw):
m = hashlib.md5()
m.update(passw)
return m.hexdigest()
if __name__ == "__main__":
#create database if it doesn't exist yet
if not os.path.exists(app.database):
with sqlite3.connect(app.database) as connection:
c = connection.cursor()
c.execute("""CREATE TABLE shop_items(name TEXT, quantitiy TEXT, price TEXT)""")
c.execute("""CREATE TABLE employees(username TEXT, password TEXT)""")
c.execute('INSERT INTO shop_items VALUES("water", "40", "100")')
c.execute('INSERT INTO shop_items VALUES("juice", "40", "110")')
c.execute('INSERT INTO shop_items VALUES("candy", "100", "10")')
c.execute('INSERT INTO employees VALUES("itsjasonh", "{}")'.format(hash_pass("badword")))
c.execute('INSERT INTO employees VALUES("theeguy9", "{}")'.format(hash_pass("badpassword")))
c.execute('INSERT INTO employees VALUES("newguy29", "{}")'.format(hash_pass("pass123")))
connection.commit()
connection.close()
app.run()
When I run my apache server and try to access localhost/sql.py I get an "Error message:
End of script output before headers: sql.py

FLASK-python ValueError: View function did not return a response [duplicate]

This question already has answers here:
Flask view return error "View function did not return a response"
(3 answers)
Closed 2 years ago.
I am trying to run an sql function that inserts data into a table. I am following the example explained here but whenever i run the script i get the error "ValueError: View function did not return a response"
My code looks like this:
from flask import render_template, flash, redirect, request
from app import app
from .forms import LoginForm
from .forms import RegistrationForm
import sqlite3 as sql
#app.route('/')
#app.route('/index')
#app.route('/registration', methods = ['GET','POST'])
def registration():
form = RegistrationForm()
if request.method == 'POST':
try:
card_id = request.form['card_id']
pin = request.form['pin']
account_id = request.form['account_id']
with sql.connect("testDB.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id) )
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("index.html",msg = msg)
con.close()
what can i be possibly be doing wrong?
What would be returned if request method is GET? you are rendering template only on POST request. correct it.
#app.route('/registration', methods = ['GET','POST'])
def registration():
form = RegistrationForm()
if request.method == 'POST':
try:
card_id = request.form['card_id']
pin = request.form['pin']
account_id = request.form['account_id']
with sql.connect("testDB.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id) )
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("index.html",msg = msg)
con.close()
else:
return render_template('index.html', form=form)

Categories