RemoteDisconnected: Remote end closed connection without response - python

I am trying to hit an API, and get data with requests module in python.
I am using conn.request("POST", "/middleware/application/servicegateway/Publicis/XXX/calculatetax", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
This does print the response as {"base_amount":40,"tax_percent":0.05,"total_tax":1.90476}
But, when I try to put this response data into a variable,using
I get the error as,
RemoteDisconnected: Remote end closed connection without response
Complete code is as below :
def create_query (start_date, end_date):
raw_query1 = "Select cnsmr_id as cnsmr_id_1, cnsmr_loc_id as loc_id_1 from staging.trvs_cnsmr where registration_date between '%s' and '%s' " %(start_date, end_date)
red_engine = create_engine('redshift+psycopg2://YYYY:ZZZZZ#AA.BB.CC.DD:5000/DB')
test1 = pd.read_sql_query(raw_query1,red_engine)
for index1, row1 in test1.iterrows():
print(row1['cnsmr_id_1'], row1['loc_id_1'])
raw_query2 = "Select cnsmr_loc_id, state, cnsmr_loc_addrss_line_1, cnsmr_loc_addrss_line_2, city, country, zipcode from staging.trvs_consumer_loc1 where cnsmr_loc_id = '%s' and country = 'CAN ' " %(row1['loc_id_1'])
test2 = pd.read_sql_query(raw_query2,red_engine)
for index2, row2 in test2.iterrows():
print(row2['cnsmr_loc_id'], row2['state'], row2['cnsmr_loc_addrss_line_1'], row2['cnsmr_loc_addrss_line_2'], row2['city'], row2['country'], row2['zipcode'] )
raw_query3 = "Select subscription_id, cnsmr_id as cnsmr_id_2 , bus_id, payment_amount from staging.trvs_cnsmr_payment where cnsmr_id_2 = '%s' " %(row1['cnsmr_id_1'])
test3 = pd.read_sql_query(raw_query3,red_engine)
for index3, row3 in test3.iterrows():
print (row3['bus_id'], row3['payment_amount'], row3['subscription_id'], row3['cnsmr_id_2'], row2['zipcode'])
payload = "{\n\"businessunit\":\"%s\", \"amount\":\"%d\", \"addresslineone\":\"%s\", \"addresslinetwo\":\"%s\", \"stateorprovince\":\"%s\", \"postcalcode\":\"%s\", \"city\":\"%s\", \"country\":\"%s\"\n}" %(row3['bus_id'], row3['payment_amount'], row2['cnsmr_loc_addrss_line_1'], row2['cnsmr_loc_addrss_line_2'], row2['state'], row2['zipcode'], row2['city'], row2['country'])
headers = {"X-API-CLIENT-ID":"XXXX", "X-API-TOKEN":"5a98"}
conn.request("POST", "/application/servicegateway/client/atn/taxation/calculatetax", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
x = data.decode("utf-8")

Related

want to insert api data intodatabase using python ,but it shows error as 'Could not process parameter: str(6875), it must be of type list/tuple/ dict'

Error 'Could not process parameter: str( 11429016), it must be of type list/tuple/ dict'
totalCollectedWeightDate = RestAPI['API']['totalCollectedWeightDate']
today1 = datetime.now()
today = today1.strftime("%Y-%m-%d")
year =datetime.strftime(datetime.now() - timedelta(365), '%Y-%m-%d')
txt6 = totalCollectedWeightDate
xx6 = txt6.replace("2021-02-17",year)
yy6=xx6.replace("2022-03-17",today)
# Generate Access Token
api_url =AccessTokenAPI
headers = {"Content-Type":"application/json"}
response = requests.post(api_url, data=json.dumps(Body),
headers=headers2,verify=False)
response.json()
#print(response.json()["access_token"])
# getSLAComplaintsData API Response
auth_token=response.json()["access_token"]
hed = {'Authorization': 'Bearer ' + auth_token}
totalCollectedWeightDateAPI1 =yy6
response = requests.get(totalCollectedWeightDateAPI1, headers=hed,verify=False)
r=response.content
print(r)
print(type(r))
q6 gives me the output as 11429016 which is a string but its showing an error
q6=str(json.loads(r)['SWM']['data']['dumpweight'])
print(q6)
print(type(q6))
for totalCollectedWeightDateAPI1 in r:
c.execute(""" UPDATE totalCollectedWeightDate22 SET dumpweight=%s """, (str(q6)))
# print(q6)
conn.commit()

restarting through nested loop

I am pulling stock data from an REST API, however the API doesn't supply a list of stocks they offer so my list and their list is different. I running my requests through nested loops and I'm trying to figure out how to except KeyError just move on to the next company in the list.
for stock in stock_list:
#queryString = "symbols:"+stock+" AND publishedAt:[2021-03-20 TO 2021-04-02]"
queryString = "source.id:sec-api AND symbols:" + stock
payload = {
"type": "filterArticles",
"queryString": queryString,
"from": 0,
"size": 2000
}
print(queryString)
# Format your payload to JSON bytes
jsondata = json.dumps(payload)
jsondataasbytes = jsondata.encode('utf-8')
# Instantiate the request
req = urllib.request.Request(API_ENDPOINT)
# Set the correct HTTP header: Content-Type = application/json
req.add_header('Content-Type', 'application/json; charset=utf-8')
# Set the correct length of your request
req.add_header('Content-Length', len(jsondataasbytes))
# Send the request to the API
response = urllib.request.urlopen(req, jsondataasbytes)
# Read the response
res_body = response.read()
# Transform the response into JSON
assets = json.loads(res_body.decode("utf-8"))
##### parse JSON Array into variables for SQL ######
articles = assets["articles"][0:50]
#print(articles)
for article in articles:
title = (article['title'][0:50])
stock_id =(article['id'][0:50])
date = (article['publishedAt'][0:50])
sources = (article['source'])
name = (sources['name'][0:50])
id = (sources['id'][0:50])
#print(title)
details = (article['details'])
company_name = (details['name'][0:50])
type = (details['type'][0:50])
#print(type)
cik = (details['cik'][0:50])
symbol = (details['ticker'][0:50])
link_sec = (details['linkToHtmlAnnouncement'][0:50])
description = (details['description'][0:50])
try:
cursor.execute("""
INSERT INTO stock_sec (stock_id, title, date, description, link_sec, type, symbol, company_name, cik)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",(stock_id[0:50], title[0:50], date[0:50], description[0:50], link_sec[0:50], type[0:50], symbol[0:50], company_name[0:50], cik[0:50]))
except Exception as e:
print(e)

Pass 2 parameters in url for app.route (python with flask )

Try to pass two parameters but seems like I'm missing something with syntax here , can someone help me on this?
#app.route('/BotMetrics/<int:fromdate>/<int:todate>')
def user(fromdate, todate):
print("connecting")
con = Get_hdb()
cursor1 = con.cursor(pymysql.cursors.DictCursor)
cursor1.execute("select * from order_details where date between '%s' and '%s'",(fromdate,todate,))
row = cursor1.fetchall()
resp = jsonify(row)
resp.status_code = 200
return resp
Trying to access URL , here I want to pass two parameters FromDate and ToDate in URL ,
http://127.0.0.1:5000/BotMetrics/?FromDate?Todate
Updated the code to below and it worked
#app.route('/BotMetrics/<fromdate>/<todate>')
def user(fromdate=None, todate=None):
print("connecting")
con = Get_hdb()
cursor1 = con.cursor(pymysql.cursors.DictCursor)
cursor1.execute("select * from order_details where date between %s and %s",(fromdate,todate,))
row = cursor1.fetchall()
resp = jsonify(row)
resp.status_code = 200
return resp
URL
http://127.0.0.1:5000/BotMetrics/2021-02-01/2021-02-27

loop for json data extraction

this is webhook
. i recive json form api with this . i need extract json tags and send to mysql database . problem is it not sent all json tags . i think it need loop at the step i tag with this step at code . thanks .
import json
import urllib.parse
import urllib.request
import mysql.connector
urls = ('/.*', 'hooks')
app = web.application(urls, globals())
class hooks:
def POST(self):
data = web.data()
print()
print('DATA RECEIVED:')
print(data)
print()
cts = data.decode('utf-8') #decode bytes to string
r1 = cts.replace(cts[:9], '')
parsed = urllib.parse.unquote_plus(r1) # ready for post
print(parsed)
print(cts)
print(type(cts))
myurl = "https://webhook.site/c0e861b0-3cc1-42c2-a0c6-54ad980b01b0"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = parsed
jsondataasbytes = jsondata.encode('utf-8') # convert to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
print(jsondataasbytes)
response = urllib.request.urlopen(req, jsondataasbytes)
test_dict = json.loads(parsed)[0]
print(type(test_dict))
# Extracting specific keys from dictionary <<<<<<THIS STEP>>>>>>>>>
indic_label = test_dict['indicator_label']
status = test_dict['status']
creation_date = test_dict['creation_date']
laststatus = test_dict['last_status']
base = test_dict['base_currency']
quote_currency = test_dict['quote_currency']
indic = test_dict['indicator']
prices = test_dict['prices']
mydb = mysql.connector.connect(
host="*",
user="*",
password="*",
database="*"
)
cursor = mydb.cursor()
cursor.execute("""INSERT INTO allcoins
(base,quote_currency , indic,status,laststatus,creation_date,prices,indic_label)
VALUES(%s,%s,%s,%s,%s,%s,%s,%s)""",
(base, quote_currency, indic, status, laststatus, creation_date, prices, indic_label))
mydb.commit()
cursor.close()
mydb.close()
return 'OK'
if __name__ == '__main__':
app.run()
you can post test data to this hook by this curl
curl -d "messages=%5B%7B%22values%22%3A+%7B%22momentum%22%3A+%220.00%22%7D%2C+%22exchange%22%3A+%22binance%22%2C+%22market%22%3A+%22BNT%2FETH%22%2C+%22base_currency%22%3A+%22BNT%22%2C+%22quote_currency%22%3A+%22ETH%22%2C+%22indicator%22%3A+%22momentum%22%2C+%22indicator_number%22%3A+0%2C+%22analysis%22%3A+%7B%22config%22%3A+%7B%22enabled%22%3A+true%2C+%22alert_enabled%22%3A+true%2C+%22alert_frequency%22%3A+%22once%22%2C+%22signal%22%3A+%5B%22momentum%22%5D%2C+%22hot%22%3A+0%2C+%22cold%22%3A+0%2C+%22candle_period%22%3A+%224h%22%2C+%22period_count%22%3A+10%7D%2C+%22status%22%3A+%22hot%22%7D%2C+%22status%22%3A+%22hot%22%2C+%22last_status%22%3A+%22hot%22%2C+%22prices%22%3A+%22+Open%3A+0.000989+High%3A+0.000998+Low%3A+0.000980+Close%3A+0.000998%22%2C+%22lrsi%22%3A+%22%22%2C+%22creation_date%22%3A+%222020-05-10+16%3A16%3A23%22%2C+%22hot_cold_label%22%3A+%22%22%2C+%22indicator_label%22%3A+%22%22%2C+%22price_value%22%3A+%7B%22open%22%3A+0.000989%2C+%22high%22%3A+0.000998%2C+%22low%22%3A+0.00098%2C+%22close%22%3A+0.000998%7D%2C+%22decimal_format%22%3A+%22%25.6f%22%7D%2C+%7B%22values%22%3A+%7B%22leading_span_a%22%3A+%220.00%22%2C+%22leading_span_b%22%3A+%220.00%22%7D%2C+%22exchange%22%3A+%22binance%22%2C+%22market%22%3A+%22BNT%2FETH%22%2C+%22base_currency%22%3A+%22BNT%22%2C+%22quote_currency%22%3A+%22ETH%22%2C+%22indicator%22%3A+%22ichimoku%22%2C+%22indicator_number%22%3A+1%2C+%22analysis%22%3A+%7B%22config%22%3A+%7B%22enabled%22%3A+true%2C+%22alert_enabled%22%3A+true%2C+%22alert_frequency%22%3A+%22once%22%2C+%22signal%22%3A+%5B%22leading_span_a%22%2C+%22leading_span_b%22%5D%2C+%22hot%22%3A+true%2C+%22cold%22%3A+true%2C+%22candle_period%22%3A+%224h%22%2C+%22hot_label%22%3A+%22Bullish+Alert%22%2C+%22cold_label%22%3A+%22Bearish+Alert%22%2C+%22indicator_label%22%3A+%22ICHIMOKU+4+hr%22%2C+%22mute_cold%22%3A+false%7D%2C+%22status%22%3A+%22cold%22%7D%2C+%22status%22%3A+%22cold%22%2C+%22last_status%22%3A+%22cold%22%2C+%22prices%22%3A+%22+Open%3A+0.000989+High%3A+0.000998+Low%3A+0.000980+Close%3A+0.000998%22%2C+%22lrsi%22%3A+%22%22%2C+%22creation_date%22%3A+%222020-05-10+16%3A16%3A23%22%2C+%22hot_cold_label%22%3A+%22Bearish+Alert%22%2C+%22indicator_label%22%3A+%22ICHIMOKU+4+hr%22%2C+%22price_value%22%3A+%7B%22open%22%3A+0.000989%2C+%22high%22%3A+0.000998%2C+%22low%22%3A+0.00098%2C+%22close%22%3A+0.000998%7D%2C+%22decimal_format%22%3A+%22%25.6f%22%7D%2C+%7B%22values%22%3A+%7B%22bbp%22%3A+%220.96%22%2C+%22mfi%22%3A+%2298.05%22%7D%2C+%22exchange%22%3A+%22binance%22%2C+%22market%22%3A+%22BNT%2FETH%22%2C+%22base_currency%22%3A+%22BNT%22%2C+%22quote_currency%22%3A+%22ETH%22%2C+%22indicator%22%3A+%22bbp%22%2C+%22indicator_number%22%3A+1%2C+%22analysis%22%3A+%7B%22config%22%3A+%7B%22enabled%22%3A+true%2C+%22alert_enabled%22%3A+true%2C+%22alert_frequency%22%3A+%22once%22%2C+%22candle_period%22%3A+%224h%22%2C+%22period_count%22%3A+20%2C+%22hot%22%3A+0.09%2C+%22cold%22%3A+0.8%2C+%22std_dev%22%3A+2%2C+%22signal%22%3A+%5B%22bbp%22%2C+%22mfi%22%5D%2C+%22hot_label%22%3A+%22Lower+Band%22%2C+%22cold_label%22%3A+%22Upper+Band+BB%22%2C+%22indicator_label%22%3A+%22Bollinger+4+hr%22%2C+%22mute_cold%22%3A+false%7D%2C+%22status%22%3A+%22cold%22%7D%2C+%22status%22%3A+%22cold%22%2C+%22last_status%22%3A+%22cold%22%2C+%22prices%22%3A+%22+Open%3A+0.000989+High%3A+0.000998+Low%3A+0.000980+Close%3A+0.000998%22%2C+%22lrsi%22%3A+%22%22%2C+%22creation_date%22%3A+%222020-05-10+16%3A16%3A23%22%2C+%22hot_cold_label%22%3A+%22Upper+Band+BB%22%2C+%22indicator_label%22%3A+%22Bollinger+4+hr%22%2C+%22price_value%22%3A+%7B%22open%22%3A+0.000989%2C+%22high%22%3A+0.000998%2C+%22low%22%3A+0.00098%2C+%22close%22%3A+0.000998%7D%2C+%22decimal_format%22%3A+%22%25.6f%22%7D%5D" -X POST http://192.168.30.1
Perhaps define all variables upfront because you still need them to write to the db, then check if they are in the response and update:
indic_label = ''
status = ''
creation_date = ''
laststatus = ''
base = ''
quote_currency = ''
indic =''
prices = ''
if test_dict['indicator_label']:
indic_label = test_dict['indicator_label']
if test_dict['status']:
status = test_dict['status']
...
...
If it's an all or none situation, you can check for one variable then exit, otherwise check for each of them.

Shouldn't be accessing this method but get UnboundLocalError: local variable 'response' referenced before assignment

I am getting an error on this line
File "/Users/j/udacity/item_catalog/item-catalog-1490/application.py", line 171, in fbconnect
response.headers['Content-Type'] = 'application/json'
UnboundLocalError: local variable 'response' referenced before assignment
But I don't understand why it's even going to that method because the two states seem to be equal
***************state C155X84FWY0WLD2V5VS2UZDDWXN72F1O
************* login C155X84FWY0WLD2V5VS2UZDDWXN72F1O
But inside that method I have a print for state not equal and it doesn't print that out. In the browser I am getting a 500 error.
#app.route('/fbconnect', methods=['POST'])
def fbconnect():
print "***************state" ,request.args.get('state')
print "*************login session state", login_session['state']
if request.args.get('state') != login_session['state']:
print "*********************state not equal"
response = make_response(json.dumps('Invalid state parameter.'), 401)
response.headers['Content-Type'] = 'application/json'
return response
I'm posting the full content below
#app.route('/fbconnect', methods=['POST'])
def fbconnect():
#state = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in xrange(32))
#login_session['state'] = state
print "***************state" ,request.args.get('state')
print "*************login session state", login_session['state']
if request.args.get('state') != login_session['state']:
print "*********************state not equal"
response = make_response(json.dumps('Invalid state parameter.'), 401)
response.headers['Content-Type'] = 'application/json'
return response
access_token = request.data
app_id = json.loads(open('fb_client_secrets.json', 'r').read())['web']['app_id']
app_secret = json.loads(open('fb_client_secrets.json', 'r').read())['web']['app_secret']
url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s' % (app_id, app_secret, access_token)
h = httplib2.Http()
result = h.request(url, 'GET')[1]
# Use token to get user info from API
userinfo_url = "https://graph.facebook.com/v2.4/me"
token = result.split("&")[0]
url = 'https://graph.facebook.com/v2.4/me?%s&fields=name,id,email' % token
h = httplib2.Http()
result = h.request(url, 'GET')[1]
data = json.loads(result)
login_session['provider'] = 'facebook'
login_session['username'] = data["name"]
login_session['email'] = data["email"]
login_session['facebook_id'] = data["id"]
# The token must be stored in the login_session in order to properly logout, let's strip out the information before the equals sign in our token
stored_token = token.split("=")[1]
login_session['access_token'] = stored_token
# Get user picture
url = 'https://graph.facebook.com/v2.4/me/picture?%s&redirect=0&height=200&width=200' % token
h = httplib2.Http()
result = h.request(url, 'GET')[1]
data = json.loads(result)
login_session['picture'] = data["data"]["url"]
# see if user exists
user_id = getUserID(login_session['email'])
print "user_id", user_id
if not user_id:
user_id = createUser(login_session)
login_session['user_id'] = user_id
output = ''
output += '<h1>Welcome, '
output += login_session['username']
output += '!</h1>'
output += '<img src="'
output += login_session['picture']
output += ' " style = "width: 300px; height: 300px;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;"> '
flash("Now logged in as %s" % login_session['username'])
#session['name']=login_session['username']
#print session['']
return output

Categories

Resources