I am setting up a lambda function to put data into a db. I am using API gateway to control the endpoint.
The request I am making sends the data as a string:
fetch(
url,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name: "hello", contact: "Tim", phone: "555" }),
}
);
My lambda function keeps erroring out because it doesn't know how to handle the string.
My function is:
import re
import psycopg2
import os
import json
def lambda_handler(event, context):
password = os.environ['DB_SECRET']
host = os.environ['HOST']
connection = psycopg2.connect(user="postgres", password=password,
host=host, port="5432",
database="postgres")
cursor = connection.cursor()
postgres_insert_query = "INSERT INTO clients (name, phone, contact) VALUES ('{0}','{1}','{2}')".format(event.get('name'), event.get('phone'), event.get('contact'))
cursor.execute(postgres_insert_query)
print(cursor.rowcount)
print(event['body'])
connection.commit()
return {
'statusCode': 200,
'headers': {
"Access-Control-Allow-Origin" : "*"
},
}
Now my print statement print(event['body']) returns '{"name":"hello","contact":"Tim","phone":"555"}' which is the exact data I need. However for the life of me I can't figure out how to get the data out of the string.
import json
data = json.loads(event['body'])
print(data['name'])
Related
I come here for help after being stuck a few hours creating an OAuth2 Async httpx client.
It's one of those situations that feels so close, and still doesn't come through in the end.
I'm using this oauth2 session/client documentation:
What I need:
Creating an oauth2_async_client with authentication implemented at the creation -hopefully-.
The flow isn't that much different than what is described in the documentation.
Understanding the usage of fetch_token in the client.
About the flow:
I have two sets of credentials: a service principal (id, secret), and (user, password) that are called inside data.
The Postman Authentication request looks like this:
// Postman call
auth_enc = btoa(sp_key+":"+sp_secret)
var a_request = {
url : auth_login,
method : 'POST',
header : { "Authorization": "Basic "+auth_enc,
"Content-Type":"application/x-www-form-urlencoded"},
body: {
mode: "urlencoded",
urlencoded : [
{ key: 'username' , value: user },
{ key: 'password' , value: pass },
{ key: 'grant_type', value:'password'},
] } }
pm.sendRequest(a_request, (error, response) => {
if (error === null) {
pm.environment.set("ERP_TOKEN", response.json()["access_token"])
};
} );
What I tried:
class ERPClientX(OauthAsyncClient):
def __init__(self, config, **kwargs):
self.config = config
clienters = {
'client_id' : config['client_id'],
'client_secret': config['client_secret'],
'token_endpoint_auth_method': 'client_secret_basic'}
authers = {
'url' : self.config['auth-url'],
'data' : self.config['auth-data']), # Keys: {grant_type, username, password}
'headers': {'Content-Type': "application/x-www-form-urlencoded"}}
super().__init__(**clienters, **kwargs)
self.create_authorization_url(**authers)
self.base_url = self.config['base-url']
self.headers.update(self.std_headers)
std_headers = { # For the general request, not auth.
'Accept-Encoding' : 'gzip, deflate',
'Content-Type' : 'application/json',
'Accept' : 'application/json',
'Format' : 'json'}
async def inside_call(self, call_params):
sub_url = f"/call-me-maybe/{params['number']}/"
the_resp = await self.get(sub_url) # prepended by base_url defined above.
# Do something with result.
results_ls = the_resp.json()['d']['results']
for each_result in results_ls:
each_result.pop('__metadata')
results_df = (pd.DataFrame(results_ls)
.assign(ts_call=dt.now().strftime("%Y-%m-%d %H:%M:%S")))
return results_df
The error I get:
TypeError: 'str' object cannot be interpreted as an integer
...
307
308 super().__init__(**clienters, **kwargs)
--> 309 uri, _ = self.create_authorization_url(**authers)
310
When I didn't include the data and headers parameters in create_authorization_url -authers dict, I got another error:
<MissingTokenError \"missing_token\".
The documentation has several examples of using fetch_token but haven't been able to get my head around it.
Thank you for your help.
I am trying to send email from lambda by passing variable in the html body inside payload(in send_email()).I tried using +str(latesturgency)+ and also using {latestimpact} .This doesn.t work.I am new to lambda and python.
How can I solve this?
import json
import logging
import re
import http.client
import mimetypes
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def send_email(email,latestdescription,latestservice,latestimpact,latesturgency):
conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.bmw")
payload = {
"from":"xxx#yyy.com",
"to": "xxx#yyy.com",
"subject": "Test mail","textbody": "body",
"htmlbody": "<h3>Test body!Please create a +str(latesturgency)+ priority ticket to {latestservice} , with below message:{latestdescription}</h3>"
}
print(payload)
headers = {
'Content-Type': 'application/json'
}
data=json.dumps(payload)
print(data)
conn.request("POST", "", data, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
print("done")
def lambda_handler(event, context):
empidemployee= event['currentIntent']["slots"]["empidemployee"]
latestdescription= event['currentIntent']["slots"]["latestdescription"]
latestservice= event['currentIntent']["slots"]["latestservice"]
latestimpact= event['currentIntent']["slots"]["latestimpact"]
latesturgency= event['currentIntent']["slots"]["latesturgency"]
email=event['currentIntent']["slots"]["email"]
send_email(email,latestdescription,latestservice,latestimpact,latesturgency)
You can do this using the format function fro your string, I have added this below.
import json
import logging
import re
import http.client
import mimetypes
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def send_email(email,latestdescription,latestservice,latestimpact,latesturgency):
conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.bmw")
payload = {
"from":"xxx#yyy.com",
"to": "xxx#yyy.com",
"subject": "Test mail","textbody": "body",
"htmlbody": "<h3>Test body!Please create a {} priority ticket to {}, with below message:{}</h3>".format(latesturgency, latestservice, latestdescription)
}
print(payload)
headers = {
'Content-Type': 'application/json'
}
data=json.dumps(payload)
print(data)
conn.request("POST", "", data, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
print("done")
def lambda_handler(event, context):
empidemployee= event['currentIntent']["slots"]["empidemployee"]
latestdescription= event['currentIntent']["slots"]["latestdescription"]
latestservice= event['currentIntent']["slots"]["latestservice"]
latestimpact= event['currentIntent']["slots"]["latestimpact"]
latesturgency= event['currentIntent']["slots"]["latesturgency"]
email=event['currentIntent']["slots"]["email"]
send_email(email,latestdescription,latestservice,latestimpact,latesturgency)
Use the {} notation to indicate where a variable should be replace then use .format() after your string passing in the variables in the ordering you will use them in.
In your case it is now
"<h3>Test body!Please create a {} priority ticket to {}, with below message:{}</h3>".format(latesturgency, latestservice, latestdescription)
For more information about the format function take a read in the documentation.
I am trying to build a python script that access the email and last logged in time data of all my users on my auth0 portal. How should i approach this?
this is the code i have so far
import sys
sys.path.append("D:\home\site\wwwroot\env\Lib\site-packages")
import quickbase
import urllib2
import pypyodbc
import timestring
import datetime
import time
import math
import sys
import locale
import smtplib
import pypyodbc
import httplib
import json
import urllib
import urllib2
from dateutil.relativedelta import relativedelta
#Activate Webhook with Campaign is added or modified.
#0.1 establish connection to SQL server
connection = pypyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=tcp:xxxxe.database.windows.net,1433;'
'Database=LQDC;'
'uid=Admin#lxx;pwd=xxxx')
cursor = connection.cursor()
#0.2 establish connection to Auth0
def main():
#Configuration Values
AUDIENCE = "https://xxe.auth0.com/api/v2/"
DOMAIN = "lxxx.auth0.com"
CLIENT_ID = "xxxxxL"
CLIENT_SECRET = "xxxxxxr"
GRANT_TYPE = "client_credentials" # OAuth 2.0 flow to use
#Get an Access Token from Auth0
base_url = "https://{domain}".format(domain=DOMAIN)
data = urllib.urlencode([('client_id', CLIENT_ID),
('client_secret', CLIENT_SECRET),
('audience', AUDIENCE),
('grant_type', GRANT_TYPE)])
req = urllib2.Request(base_url + "/oauth/token", data)
response = urllib2.urlopen(req)
oauth = json.loads(response.read())
access_token = oauth['access_token']
#Get all Applications using the token
req = urllib2.Request(base_url + "/api/v2/clients")
req.add_header('Authorization', 'Bearer ' + access_token)
req.add_header('Content-Type', 'application/json')
try:
response = urllib2.urlopen(req)
res = json.loads(response.read())
print res
conn = httplib.HTTPSConnection(DOMAIN)
payload = "{\"connection_id\":\"not sure which API/token/code to put here\",\"format\": \"csv\", \"limit\": 4, \"fields\": [{ \"name\": \"email\"},{\"name\": \"last_ip\"}, { \"name\": \"created_at\"}, { \"name\": \"last_login\"}]}"
headers = {
'authorization': "Bearer "+access_token,
'content-type': "application/json"
}
conn.request("POST", "/xxxx.auth0.com/api/v2/jobs/users-exports", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
except urllib2.HTTPError, e:
print 'HTTPError = ' + str(e.code) + ' ' + str(e.reason)
except urllib2.URLError, e:
print 'URLError = ' + str(e.reason)
except urllib2.HTTPException, e:
print 'HTTPException'
except Exception:
print 'Generic Exception'
#Standard boilerplate to call the main() function.
if name == 'main':
main()
the first part works fine, second part returns the error below
im trying to have this return a csv file with all the information of users listed, and populate the database automatically with that.
also found some useful links here which i tried incorporating into my code:
https://auth0.com/docs/users/guides/bulk-user-exports
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /lqdfinance.auth0.com/api/v2/jobs/users-exports</pre>
</body>
</html>
As i can see i your code, you need a connection id so you can use
base_url + '/api/v2/connections'
to get your connection id
Next you need to modify your payload body as below
{
"connection_id": "your_id_as_above",
"format": "csv",
"limit": 5,
"fields": [
{
"name": "user_id"
},
{
"name": "name"
},
{
"name": "email"
},
{
"name": "identities[0].connection",
"export_as": "provider"
},
{
"name": "user_metadata.some_field"
}
]
}
In order to test your response data i suggest you test your api payload and response using Postman, as it gives you more clarity on how the request is working.
Following code will query the table and result in a JSON as output
import boto3
import json
import re
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('master')
response = table.scan()
data = response['Items']
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
data.extend(response['Items'])
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin' : '*',
},
'body': json.dumps(data)
}
My JSON response contains a \r.
[
{
"r_dt": "29-Oct-18",
"de_dt": "31-Dec-99\r",
"v_status": "R",
"v_num": "M13020"
},
{
"r_dt": "29-Oct-18",
"de_dt": "31-Dec-99\r",
"v_status": "R",
"v_num": "O03873"
}
}
How do I remove \r from from my JSON response? I tried JSON.loads/RegEx, but it didn't work
How to remove \r from from my json reposnse. I tried json.loads/regex
, didnt work
Your input data contains \r so it ends up in JSON response. So, instead of "fixing" JSON output, you have to fix input data. If that's not possible, you have to sanitize data as early as possible, before serializing it to JSON:
def sanitize(item):
item['de_dt'] = item['de_dt'].rstrip('\r')
# OR
item['de_dt'] = item['de_dt'].replace('\r', '')
return item
# ...
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
data.extend(sanitize(item) for item in response['Items'])
import boto3
import json
import re
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb', region_name='ap-southeast-1')
table = dynamodb.Table('dsl_vehicle_master')
response = table.scan()
data =[sanitize(item) for item in response['Items']]
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
data.extend(sanitize(item) for item in response['Items'])
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin' : '*',
},
'body': json.dumps(data)
}
def sanitize(item):
item['dereg_dt'] = item['dereg_dt'].rstrip('\r\n')
# OR
#item['dereg_dt'] = item['dereg_dt'].replace('\r', '')
return item
I am trying to send a message via Firebase to a certain client. This is my current (test) code:
import json
import requests
import urllib
def send_message():
server = "https://fcm.googleapis.com/fcm/send"
api_key = "xxx"
user_token = "xxx"
headers = {'Content-Type': 'application/json', 'Authorization': 'key=' + api_key}
data = {"type": "dataUpdate"}
payload = {"data": data, "to": user_token}
payload = json.dumps(payload)
res = requests.post(server, headers=headers, json=payload)
return res
which produces the following error, returned by Firebase:
JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0.
The following JSON sent to Firebase seems correct to me:
{
"data": {
"type":"dataUpdate"
},
"to":"xxx"
}
which is in the format described by the Firebase documentation. Any idea why Firebase doesn't accept the given data?
When you use json=payload as a parameter to requests.post() you don't need to specify 'Content-Type': 'application/json' in the header. In addition, you are passing a string when the parameter should be payload as a dict (ie. no need for json.dumps())
Try this:
def send_message():
server = "https://fcm.googleapis.com/fcm/send"
api_key = "xxx"
user_token = "xxx"
headers = {'Authorization': 'key=' + api_key}
data = {"type": "dataUpdate"}
payload = {"data": data, "to": user_token}
res = requests.post(server, headers=headers, json=payload)
return res