Coverage for libs/sdc_etl_libs/api_helpers/apis/Ultipro/Ultipro.py : 67%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import logging
2import backoff
3import requests
4from ast import literal_eval
5from zeep import Client as Zeep
6from zeep import xsd
7from sdc_etl_libs.api_helpers.API import API
9logging.basicConfig(level=logging.INFO)
12class Ultipro(API):
14 def __init__(self):
15 self.credentials = self.get_credentials("aws_secrets", "ultipro")
16 self.base_url = ""
18 def process_endpoint(self):
19 pass
21 def get_daily_filter(self):
22 raise Exception("Do not use base class get_daily_filter function.")
24 def rest_authenticate(self, username_key_, password_key_):
25 """
26 Authentication for Ultipro REST API.
27 :param username_key_: Secrets dict key for username
28 :param password_key_: Secrets dict key for password
29 :return:
30 """
32 self.auth = literal_eval(f"('{self.credentials[username_key_]}','{self.credentials[password_key_]}')")
34 @staticmethod
35 def soap_backoff_handler(details):
36 """
37 Message formatting function for Backoff messages.
38 :return: Message for logger.
39 """
40 logging.warning("Backing off {wait:0.1f} seconds after {tries} tries "
41 "calling function {target}".format(**details))
43 @backoff.on_exception(backoff.expo, requests.exceptions.HTTPError,
44 max_tries=8, on_backoff=soap_backoff_handler)
45 def soap_authenticate(self):
46 """
47 Authentication for Ultipro SOAP connection.
48 :return: None
49 """
51 login_header = {
52 'UserName': self.credentials["soap_username"],
53 'Password': self.credentials["soap_password"],
54 'ClientAccessKey': self.credentials["api_key"],
55 'UserAccessKey': self.credentials["soap_user_access_key"]
56 }
58 zeep_client = Zeep(f"{self.base_url}LoginService")
59 result = zeep_client.service.Authenticate(_soapheaders=login_header)
60 self.token = result['Token']
62 # Create xsd ComplexType header -
63 # http://docs.python-zeep.org/en/master/headers.html
64 header = xsd.ComplexType([
65 xsd.Element(
66 '{http://www.ultimatesoftware.com/foundation/authentication'
67 '/ultiprotoken}UltiProToken',
68 xsd.String()),
69 xsd.Element(
70 '{http://www.ultimatesoftware.com/foundation/authentication'
71 '/clientaccesskey}ClientAccessKey',
72 xsd.String()),
73 ])
75 # Add authenticated header to client object
76 self.session_header = header(UltiProToken=self.token,
77 ClientAccessKey=self.credentials["api_key"])