def download_order_tag_defaults(auth):
    logger.log('Downloading order tag default list (this may take a while)')

    order_tag_defaults = []
    endpoint = 'https://apigateway.{domain}/risk/{env}/ordertagdefaults/'.format(
        domain=auth.domain, env=auth.environment)
    headers = {
        'accept': 'application/json',
        'Authorization': 'Bearer {0}'.format(auth.access_token),
        'x-api-key': auth.client
    }

    next_page_key = None
    while True:
        req = request_receiver.RequestReceiver()
        j_resp = req.get(url=endpoint,
                         headers=headers,
                         params={'nextPageKey': next_page_key})

        # {
        #     "status": "string",
        #     "fields": [
        #         {
        #             "fieldId": 0,
        #             "isRequired": 0,
        #             "marketId": 0,
        #             "name": "string",
        #             "valueTypeId": 0,
        #             "category": 0,
        #             "enums": [
        #                 {
        #                     "valueEnumId": 0,
        #                     "valueEnumName": "string"
        #                 }
        #             ]
        #         }
        #     ]
        # }

        if j_resp['status'] == 'Ok':
            order_tag_defaults += j_resp['orderTagDefaults']
            logger.log_verbose('Fields downloaded:'
                               ' num_downloaded={0}'
                               ' total_downloaded={1}'
                               ' last_page={2}'.format(
                                   len(j_resp['orderTagDefaults']),
                                   len(order_tag_defaults),
                                   j_resp['lastPage']))

            if j_resp['lastPage'] == 'false':
                next_page_key = j_resp['nextPageKey']
            else:
                break
        else:
            raise Exception(
                'Order tag defaults download failed.  msg={0}'.format(
                    j_resp['status_message']))

    return order_tag_defaults
Beispiel #2
0
 def __throttle(self):
     global g_throttle
     if g_throttle == True:
         time_to_sleep = ThrottleRate - (self.__time1 - self.__time0)
         if time_to_sleep > 0.0:
             time.sleep(time_to_sleep)
             logger.log_verbose(
                 self.__throttle_log_format.format(time_to_sleep))
Beispiel #3
0
    def __preprocess(self, **kwargs):
        self.__time0 = time.time()

        frm = inspect.stack()[2]
        mod = inspect.getmodule(frm[0])
        app_name = os.path.splitext(os.path.basename(mod.__file__))[0]

        kwargs['params'][
            self.__request_id_key] = self.__request_id_format.format(
                app_name=app_name, company='tt', guid=uuid.uuid1())
        logger.log_verbose('Request ID: {0}'.format(
            kwargs['params'][self.__request_id_key]))
def download_users(auth):
    logger.log('Downloading users list')

    users = []
    endpoint = 'https://apigateway.{domain}/risk/{env}/users'.format(
        domain=auth.domain, env=auth.environment)
    headers = {
        'accept': 'application/json',
        'Authorization': 'Bearer {0}'.format(auth.access_token),
        'x-api-key': auth.client
    }

    next_page_key = None
    while True:
        req = request_receiver.RequestReceiver()
        j_resp = req.get(url=endpoint,
                         headers=headers,
                         params={'nextPageKey': next_page_key})

        # {
        #     "status": "Ok",
        #     "users": [
        #         {
        #             "id": 25,
        #             "email": "*****@*****.**",
        #             "firstname": "First",
        #             "lastname": "Last",
        #             "alias": "FLast"
        #         }
        #     ]
        #     "lastPage": "true",
        #     "nextPageKey": "...",
        # }

        if j_resp['status'] == 'Ok':
            users += j_resp['users']
            logger.log_verbose('Users downloaded:'
                               ' num_downloaded={0}'
                               ' total_downloaded={1}'
                               ' last_page={2}'.format(len(j_resp['users']),
                                                       len(users),
                                                       j_resp['lastPage']))

            if j_resp['lastPage'] == 'false':
                next_page_key = j_resp['nextPageKey']
            else:
                break
        else:
            raise Exception('Users download failed.  msg={0}'.format(
                j_resp['status_message']))

    return users
Beispiel #5
0
    def __postprocess(self, response):
        logger.log_verbose('Response Code: {0}'.format(response.status_code))
        logger.log_verbose('Response Encoding: {0}'.format(response.encoding))
        logger.log_verbose('Response Apparent: {0}'.format(
            response.apparent_encoding))

        content = {}
        try:
            #text = re.sub(r'("[\s\w]*)"([\s\w]*")',r"\1\2", unicode(response.text).strip())
            text = re.sub(r'("TM C)"([\s\w]*")', r"\1\2",
                          unicode(response.text))
            if 'algoName' in text:
                text = text.replace('},    ]', '}    ]')
            content = json.loads(text, strict=False)
            if response.status_code == 429 and 'message' in content:
                content['status'] = 'StatusFailed'
                content['status_message'] = content['message']
                del content['message']
        except Exception as err:
            logger.log(
                'Exception raised parsing response: msg={0} raw={1}'.format(
                    str(err), text))
            content['status'] = 'StatusFailed'
            content['status_message'] = str(err)
        else:
            logger.log_verbose('Response: {0}'.format(content))
        finally:
            self.__time1 = time.time()
            self.__throttle()

        return content
    def __init__(self, environment, app_key):
        self.environment = environment
        self.domain = get_domain(environment)
        self.user_id = 0
        self.company_id = 0

        if app_key is None:
            logger.log_verbose('Querying environment variable "{0}" for the AppKey'.format(AppKeyEnvVar))
            app_key = os.environ.get(AppKeyEnvVar)

        try:
            self.client, secret = app_key.split(':', 2)
            uuid.UUID(str(self.client))
            uuid.UUID(str(secret))
        except Exception:
            raise Exception('Invalid app_key')

        self.__endpoint = self.__endpoint.format(domain=self.domain, env=self.environment)
        self.__headers['x-api-key'] = self.client
        self.__body['app_key'] = app_key

        resp = requests.post(url = self.__endpoint, headers = self.__headers, data = self.__body)
        j_resp = resp.json()

        if 'status' in j_resp and j_resp['status'] == 'Ok':
            logger.log('Authentication succeeded...')
            self.access_token = j_resp['access_token']
            self.token_type = j_resp['token_type']
            self.expiry = j_resp['seconds_until_expiry']

            decoded = jwt.decode(self.access_token, verify=False)
            self.user_id = decoded['id']
            self.company_id = decoded['company_id']
            logger.log_verbose('Received token for'
                               ' user_id={0}'
                               ' company_id={1}'.format(
                               self.user_id,
                               self.company_id))
        else:
            error_message = None
            if 'message' in j_resp:
                error_message = j_resp['message']
            elif 'status_message' in j_resp:
                error_message = j_resp['status_message']
            else:
                error_message = json.dumps(j_resp)

            raise Exception('Token request failed.  msg={0}'.format(error_message))
Beispiel #7
0
def set_throttle_rate(val):
    global ThrottleRate
    ThrottleRate = val
    logger.log_verbose(
        'Setting request throttle rate to () seconds'.format(ThrottleRate))
Beispiel #8
0
def throttle_requests(val):
    global g_throttle
    g_throttle = val
    logger.log_verbose('Request throttling {0}'.format(
        'enabled' if g_throttle == True else 'disabled'))