def test_all_links(self): link = '<https://api.github.com/organizations/913567/repos?page=3>; rel="next", <https://api.github.com/organizations/913567/repos?page=5>; rel="last", <https://api.github.com/organizations/913567/repos?page=1>; rel="first", <https://api.github.com/organizations/913567/repos?page=1>; rel="prev"' lp = LinkParser(link) self.assertEqual( lp.get_link('next'), 'https://api.github.com/organizations/913567/repos?page=3') self.assertEqual( lp.get_link('last'), 'https://api.github.com/organizations/913567/repos?page=5') self.assertEqual( lp.get_link('prev'), 'https://api.github.com/organizations/913567/repos?page=1') self.assertEqual( lp.get_link('first'), 'https://api.github.com/organizations/913567/repos?page=1')
def request_data(endpoint, request, timeout): next_link = endpoint + request token = get_auth_token() headers = {'Authorization': token} response = [] while next_link != '': try: r = requests.get(next_link, headers=headers, timeout=(3, timeout)) except requests.exceptions.Timeout as e: logger.warn('Request %s timed out after %f seconds.', next_link, timeout) return [598, response] except requests.exceptions.ConnectionError as e: logger.error('Caught %s', e.message) app_config.request_connection_failure = True raise app_config.request_connection_failure = False if r.status_code == 200: response.append(r.text) if 'link' in r.headers: link_string = r.headers['link'] lp = LinkParser(link_string) next_link = lp.get_link('next') else: next_link = '' else: logger.warn('Failed request with status code %d', r.status_code) return [r.status_code, response] return [200, response]
def test_empty_string(self): lp = LinkParser("") self.assertEqual(lp.get_link('next'), '') self.assertEqual(lp.get_link('prev'), '') self.assertEqual(lp.get_link('last'), '') self.assertEqual(lp.get_link('first'), '')
def test_mal_formed_link_missin_equal(self): link = '<https://api.github.com/organizations/913567/repos?page=3>; rel~"next"' lp = LinkParser(link) self.assertEqual(lp.get_link('next'), '')
def test_mal_formed_link_missing_close_angle_bracket(self): link = '<https://api.github.com/organizations/913567/repos?page=3; rel="next"' lp = LinkParser(link) self.assertEqual(lp.get_link('next'), '')