File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/integrations/finserv.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 FinservException(Exception):
pass
def camel_case(thing):
if isinstance(thing, dict):
return {camel_case(key): val for key, val in thing.items()}
words = thing.split('_')
return words[0] + ''.join(x.capitalize() for x in words[1:])
class Finserv(object):
API_ROOT_TOKEN = os.environ.get(
'FINSERV_API_ROOT_TOKEN', 'https://login.preqpro.com/connect/token'
)
API_ROOT_DATA = os.environ.get(
'FINSERV_API_ROOT_DATA', 'https://api.preqpro.com/check'
)
def __init__(self, username, password, dealercode, product, api_root_token=API_ROOT_TOKEN,
api_root_data=API_ROOT_DATA):
self.username = username
self.password = password
self.dealercode = dealercode
self.product = product
self.api_root_token = api_root_token
self.url_root_token = '{}'.format(self.api_root_token)
self.api_root_data = api_root_data
self.url_root_data = '{}'.format(self.api_root_data)
@property
def credit_prequalify(self):
return CreditPrequalify(self)
def post(self, url, data, headers):
r = requests.post(url, data=data, headers=headers)
if 300 <= r.status_code:
error_content = json.loads(r.content.decode())
raise FinservException('STATUS_CODE:{} - REASON: {} - DETAIL: {}'
.format(r.status_code, r.reason, error_content['error']))
return r.content.decode()
def get(self, url, headers, **kwargs):
kwargs = {k: v for k, v in kwargs.items() if v is not None}
log.debug('GET {}?{}\n'.format(
url,
'&'.join('{}={}'.format(key, val) for key, val in kwargs.items())
))
r = requests.get(url, headers=headers, params=camel_case(kwargs))
if 300 <= r.status_code:
error_content = json.loads(r.content.decode())
raise FinservException('STATUS_CODE:{} - REASON: {} - MESSAGE: {} - DETAIL: {}'
.format(r.status_code, r.reason, error_content['message'],
error_content['errors'][0]['message']))
return r.json()
class CreditPrequalify(object):
def __init__(self, client):
self.client = client
self.base_url_token = self.client.url_root_token
self.base_url_data = self.client.url_root_data
self.username = self.client.username
self.password = self.client.password
self.dealercode = self.client.dealercode
self.product_type = self.client.product
self.child_class = None
def get_token(self):
url = '{}'.format(self.base_url_token)
param = {'client_id': self.username, 'client_secret': self.password,
'grant_type': 'client_credentials', 'scope': 'preqpro_api.partner_access'}
headers = ''
return self.client.post(url, param, headers)
def soft_pull(self, headers, **kwargs):
url = '{}'.format(self.base_url_data)
kwargs['DealerCode'] = self.dealercode
headers = {'authorization': headers, 'content-type': 'application/json; charset=utf-8'}
return self.client.get(url, headers, **kwargs)