File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/integrations/ams.py
import os
import logging
import xml.etree.ElementTree as ET
from flask import current_app as app
import requests
import xml.etree.ElementTree as ET
import xmltodict
import json
log = logging.getLogger(__name__)
class AMSException(Exception):
pass
class AMS(object):
def __init__(self, username, password, client_id, secret, url, request_id=None):
self.base_url = url
self.username = username
self.password = password
self.client_id = client_id
self.secret = secret
self.child_class = None
self.request_id = request_id
def post(self, data):
# # start creating request-log
# from buyercall.blueprints.sysadmin.utilities.request_log_task_call import LogRequestTaskTrigger
# from flask import request
# LogRequestTaskTrigger().log_request_task_trigger(
# request, "ams")
# # end creating request-log
headers = {'Content-Type': 'text/xml', 'Authorization': 'Bearer ' + self.secret}
r = requests.post(self.base_url, data=data, headers=headers)
request_id = self.request_id
from buyercall.blueprints.sysadmin.models import RequestLog
# updating the request log
update_data = {
"current_url": self.base_url,
'path_info': self.base_url,
'response_code': r.status_code,
'method':"POST",
"status": "success" if r.status_code < 400 else "failed"
}
RequestLog().update_record(request_id, update_data)
if 200 == r.status_code:
try:
if 'ErrorID' not in r.content.decode():
return 400, 'Error posting form lead.', ''
else:
root = ET.fromstring(r.content.decode())
if root is not None:
response_error_id = root.find('ErrorID').text
response_error_detail = root.find('ErrorDetail').text
response_message = root.find('Message').text
response_customer_dms_id = root.find('customerDMSID').text
if response_message == 'Success' and response_error_detail == 'Success':
return r.status_code, '', response_customer_dms_id
else:
error_message = f'Error: {response_error_id} - {response_message}'
RequestLog().update_record(
request_id, {"error": error_message})
return r.status_code, error_message, ''
else:
return r.status_code, 'Error posting form lead.', ''
except ET.ParseError:
log.error(f'Response: {r.content.decode()}')
return r.status_code, 'Error posting form lead.', ''
else:
log.error(f'Response: {r.content.decode()}')
return r.status_code, 'Error posting form lead.', ''