def _get_v1_objects(self, url, params, request_timer_suffix, bookmarked_cursor): headers = { 'content-type': 'application/json', 'authorization': 'Bearer {}'.format(self._access_token) } if bookmarked_cursor: batch_token = bookmarked_cursor else: batch_token = '__initial__' session = requests.Session() session.headers.update(headers) while batch_token: if batch_token != '__initial__': params['batch_token'] = batch_token with singer.http_request_timer('GET ' + request_timer_suffix): result = self._retryable_v1_method(session, url, params) batch_token = get_batch_token_from_headers(result.headers) yield (result.json(), batch_token)
def get_cash_drawer_shifts(self, location_id, start_time, bookmarked_cursor): if bookmarked_cursor: cursor = bookmarked_cursor else: cursor = '__initial__' # initial value so while loop is always entered one time end_time = utils.strftime(utils.now(), utils.DATETIME_PARSE) while cursor: if cursor == '__initial__': # initial text was needed to go into the while loop, but api needs # it to be a valid bookmarked cursor or None cursor = bookmarked_cursor with singer.http_request_timer('GET cash drawer shifts'): result = self._retryable_v2_method( lambda bdy: self._client.cash_drawers. list_cash_drawer_shifts( location_id=location_id, begin_time=start_time, end_time=end_time, cursor=cursor, limit=1000, ), None, ) yield (result.body.get('items', []), result.body.get('cursor')) cursor = result.body.get('cursor')
def login(self): LOGGER.info("Refreshing token") self.client_id = self.config.get('client_id') self.client_secret = self.config.get('client_secret') self.tenant_id = self.config.get('tenant_id') try: body = { 'grant_type': 'client_credentials', 'client_id': self.client_id, 'client_secret': self.client_secret, 'scope': SCOPE } with singer.http_request_timer('POST get access token'): result = self.make_request( method='POST', url=TOKEN_URL.format(tenant_id=self.tenant_id), data=body) self.access_token = result.get('access_token') finally: self.login_timer = threading.Timer(TOKEN_EXPIRATION_PERIOD, self.login) self.login_timer.start()
def _get_v2_objects(self, request_timer_suffix, request_method, body, body_key): cursor = body.get('cursor', '__initial__') while cursor: if cursor != '__initial__': body['cursor'] = cursor with singer.http_request_timer('GET ' + request_timer_suffix): result = self._retryable_v2_method(request_method, body) cursor = result.body.get('cursor') yield (result.body.get(body_key, []), cursor)
def _get_access_token(self): body = { 'client_id': self._client_id, 'client_secret': self._client_secret, 'grant_type': 'refresh_token', 'refresh_token': self._refresh_token } client = Client(environment=self._environment) with singer.http_request_timer('GET access token'): result = client.o_auth.obtain_token(body) if result.is_error(): error_message = result.errors if result.errors else result.body raise RuntimeError(error_message) return result.body['access_token']
def login(self): LOGGER.info("Refreshing token") self.client_id = self.config.get('client_id') self.client_secret = self.config.get('client_secret') try: data = {'grant_type': 'client_credentials'} with singer.http_request_timer('POST get access token'): result = self.make_request(method='POST', url=TOKEN_URL, data=data, auth=(self.client_id, self.client_secret)) self.access_token = result['access_token'] #self.access_token = result.get('access_token') finally: self.login_timer = threading.Timer(TOKEN_EXPIRATION_PERIOD, self.login) self.login_timer.start()