我想通过Django中的API响应向用户发送电子邮件吗?

时间:2019-09-21 06:32:40

标签: python django

[数据图像] [1]我想在此函数调用后向用户发送电子邮件,因此在第一个函数中,我用剃刀钱创建客户,并在获取传递给其他函数的客户ID之后继续。 因此,在此函数get_virtual_account()中,我从razor-pay提供的API中获取所有响应,因此我需要将该响应发送给创建该帐户的用户,所以我该怎么办?我无法通过电子邮件发送此响应。

        def create_razor_customer(data):
            logger.info("Inside create_razor_customer")     
            headers = {'Content-Type': 'application/json',}
            data=json.dumps(data)
            response = requests.post(settings.API_RAZORPAY+'/customers', headers=headers, data=data, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
            logger.info(json.loads(response.content))
            json_response = response.json()
            customer_id = json_response['id']
            logger.info(customer_id)
            get_razor_customer(customer_id)
            return response

        def get_razor_customer(customer_id):
            logger.info("Inside get_razor_customer")
            headers = {'Content-Type': 'application/json',}
            response = requests.get(settings.API_RAZORPAY+'/customers/'+customer_id, headers=headers, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
            logger.info(json.loads(response.content))
            create_razor_virtual_account(customer_id)
            return response

        def create_razor_virtual_account(customer_id):
            logger.info("Inside create_razor_virtual_account")
            headers = {'Content-Type': 'application/json',}
            data = {"receivers": {"types": ["bank_account"]},"description": "razorpay","customer_id": customer_id,"close_by": 1761615838,"notes": {"reference_key": "reference_value"}}
            data=json.dumps(data)
            response = requests.post(settings.API_RAZORPAY+'/virtual_accounts', headers=headers, data=data, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
            json_response = response.json()
            virtual_id = json_response['id']
            logger.info(virtual_id)
            logger.info(json_response)
            return response

        def get_virtual_account(virtual_id):
            logger.info("Inside get_virtual_account")
            logger.info(virtual_id)
            headers = {'Content-Type': 'application/json',}
            response = requests.get(settings.API_RAZORPAY+'/virtual_accounts/'+virtual_id,headers=headers, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
            json_response = response.json()
            logger.info(json_response)
send_account_details()
            return response

        def send_account_details():
            logger.info('Inside send_account_details')
            send_mail('Account Details', 'Details for Razorpay account', settings.EMAIL_HOST_USER, ['abhishek@byond.travel',])
            logger.info("sent")
            return "sent"


  [1]: https://i.stack.imgur.com/proIH.png

1 个答案:

答案 0 :(得分:0)

假设您的响应JSON是 res_data={"duration":1201,"number":6,"result":"FAILURE","url":"http://localhost:8080/job/git_checkout/6/"}

然后您需要将其传递给send_account_details函数。

def send_account_details(res_data):
    import json
    logger.info('Inside send_account_details')
    body = "JSON Response is : " + json.dumps(res_data) + "\n\n" +  "Details for Razorpay account"
    send_mail('Account Details', body, settings.EMAIL_HOST_USER, ['abhishek@byond.travel',])
    logger.info("sent")
    return "sent"

使用json.dumps(res_data),您可以将JSON添加到电子邮件正文中。