def test_save_items(self): mock = MagicMock() mock.request.return_value = mock mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1} requestor = HttpRequestor(client=mock, save_fn=mock) result = requestor.save_data_items(Constants.DUMMY) self.assertTrue(result)
def test_get_all_items(self): mock = MagicMock() mock.request.return_value = mock mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1} requestor = HttpRequestor(client=mock) result = requestor.get_all_items(Constants.DUMMY) self.assertIsNotNone(result)
def dump_mongo_data(): request = HttpRequestor() url = get_production_service_url(INFRASTRUCTURE, 0) + '&limit=90' result = request.get(url) infrastructure_list = result.json()[INFRASTRUCTURE] with open('./data/infrastructure.json', 'w+') as infra_file: infra_file.write(json.dumps(infrastructure_list, default=str))
def get_wu_name(name): wu_name = 'Unknown' url = f'https://wunameaas.herokuapp.com/wuami/{name}' web_client = HttpRequestor() result = web_client.get(url) soup = BeautifulSoup(result.text, 'html.parser') for bold_item in soup.find_all('b'): wu_name = bold_item.get_text() return wu_name
def wait_for_local_service_ping(target, service, sleep_time=10, retries=3): call_count = 1 requestor = HttpRequestor() print(f'Waiting for {service} ping to succeed...') ping_url = f'{target}/{service}/ping/' healthy = requestor.get(ping_url).status_code == Constants.SUCCESS_RESPONSE_CODE while not healthy: sleep(sleep_time) call_count += 1 healthy = requestor.get(ping_url).status_code == Constants.SUCCESS_RESPONSE_CODE if call_count > retries: return
def test_real_web_send(self): mock = MagicMock() mock.get_secret.return_value = '' stats_client = StatsWebClient(secrets_manager=mock, http_client=HttpRequestor()) self.assertTrue( stats_client.add_gauge('odb', 20, {Constants.PRODUCT: 'odb'}))
def check_session(session_token, http_client=HttpRequestor(), logger=Logger()): try: response = http_client.get(Settings.SESSION_URL.format(session_token=session_token)).json() return response.get(Constants.STATUS, Constants.EMPTY) == Constants.OKTA_ACTIVE_STATUS except Exception as ex: # pylint: disable=broad-except logger.error(f'Error checking session. ex: {ex}') return False
def call_velocity_endpoint(step, state, token, build_tag, web_client=HttpRequestor()): url = build_velocity_url(step, state, token, build_tag) result = web_client.post(url) return result.status_code
def restore_service_data(service, endpoint=Constants.EMPTY): requestor = HttpRequestor() url = get_raw_service_url(service) + Constants.RESTORE_ROUTE if endpoint: url = get_raw_service_url(service) + endpoint print(f'Restoring data for service {url}...') file_path = \ f'backup/{service}{endpoint.strip(Constants.FORWARD_SLASH)}.json' if os.path.exists(file_path): with open(file_path) as data_file: json_data = json.load(data_file) result = requestor.post(url, data=json.dumps(json_data), headers=requestor.get_request_headers()) if result.status_code != Constants.CREATED_SUCCESS_CODE: print(f'Restore of {url} failed!') return print(f'Restore of {url} succeeded!')
def create_event(self, payload, requestor=HttpRequestor()): headers = self.generate_headers() url = self.url proxy = {'http': Settings.PROXY_URL} response = requestor.post(url, data=json.dumps(payload), headers=headers, proxies=proxy) if response.status_code == 202: return True return False
def __init__(self, account_name, http=HttpRequestor(), secrets_manager=SecretManager()): self.http = http self.secrets_manager = secrets_manager self.account_name = account_name self.account_id = get_newrelic_account_id(self.account_name) self.api_key = get_newrelic_api_key(self.account_name, self.secrets_manager) self.logger = Logger(Settings.SERVICE_NAME)
def get_service_versions(target_url, service_list, web_client=HttpRequestor()): versions = dict() for service in service_list: url = f'{target_url.strip(Constants.URL_SEPARATOR)}/{service}{VERSION_ENDPOINT}' try: result = web_client.get(url) if result.status_code == Constants.SUCCESS_RESPONSE_CODE: versions[service] = get_version_from_payload(result) else: versions[service] = None except RequestException: versions[service] = None return versions
def _create_service_client(component_type, **kwargs): if component_type == Components.ServiceClients.SQS: return MessageQueue(**kwargs) if component_type == Components.ServiceClients.NEWRELIC: return NewRelicQueryExecutor(**kwargs) if component_type == Components.ServiceClients.SUMOLOGIC: return SumologicQueryExecutor(**kwargs) if component_type == Components.ServiceClients.HTTP: return HttpRequestor(**kwargs) if component_type == Components.ServiceClients.AUTH: return Authorization() if component_type == Components.ServiceClients.COLLECT_MANAGER: return CollectionManager(**kwargs) return None
def test_perform_web_request(self): mock = MagicMock() mock.request.return_value = mock mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1} requestor = HttpRequestor(client=mock) result = requestor.perform_web_request(Constants.DUMMY, Constants.DUMMY) self.assertIsNotNone(result) requestor = HttpRequestor(client=mock, circuit_breaker=mock) result = requestor.perform_web_request(Constants.DUMMY, Constants.DUMMY) self.assertIsNotNone(result)
def collect_service_endpoint_data(service, endpoint, log=True): requestor = HttpRequestor() if log: print(f'Collecting {service}/{endpoint}...') requestor.save_data_items(service, endpoint, False)
def setUp(self): mock = MagicMock() mock.get.return_value = mock mock.request.return_value = Constants.DUMMY self.requestor = HttpRequestor(client=mock)
def collect_service_data(service, log=True): requestor = HttpRequestor() if log: print(f'Collecting {service}...') requestor.save_data_items(service)
def test_get_auth_value(self): self.assertIsNotNone(HttpRequestor.get_auth_value(Constants.DUMMY, Constants.DUMMY))
def test_real_get(self): requestor = HttpRequestor() result = requestor.get(GOOGLE_URL) self.assertEqual(result.status_code, Constants.SUCCESS_RESPONSE_CODE)
def __init__(self, **kwargs): self.web_client = kwargs.get(Constants.HTTP_CLIENT, HttpRequestor()) self.secrets_manager = kwargs.get(Constants.SECRETS_MANAGER, SecretManager())
def __init__(self, slack_client=SlackApi(Settings.SLACK_API_TOKEN), web_client=HttpRequestor()): self.client = slack_client self.web_client = web_client
def run_version_check(target_url, expected_version, service_list, web_client=HttpRequestor()): versions = get_service_versions(target_url, service_list, web_client) return check_version_list(versions, expected_version)
class HttpRequestorTests(TestCase): def setUp(self): mock = MagicMock() mock.get.return_value = mock mock.request.return_value = Constants.DUMMY self.requestor = HttpRequestor(client=mock) def test_get(self): result = self.requestor.get(Constants.DUMMY, headers=[]) self.assertEqual(result, Constants.DUMMY) def test_post(self): result = self.requestor.post(Constants.DUMMY, data=[], headers=[]) self.assertEqual(result, Constants.DUMMY) def test_put(self): result = self.requestor.put(Constants.DUMMY, headers=[]) self.assertEqual(result, Constants.DUMMY) def test_delete(self): result = self.requestor.delete(Constants.DUMMY, headers=[]) self.assertEqual(result, Constants.DUMMY) def test_get_items(self): mock = MagicMock() mock.request.return_value = mock mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1} requestor = HttpRequestor(client=mock) result = requestor.get_items(Constants.DUMMY, True, Constants.DUMMY) self.assertIsNotNone(result) result = requestor.get_items(Constants.DUMMY, False, Constants.DUMMY) self.assertIsNotNone(result) def test_get_all_items(self): mock = MagicMock() mock.request.return_value = mock mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1} requestor = HttpRequestor(client=mock) result = requestor.get_all_items(Constants.DUMMY) self.assertIsNotNone(result) def test_save_items(self): mock = MagicMock() mock.request.return_value = mock mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1} requestor = HttpRequestor(client=mock, save_fn=mock) result = requestor.save_data_items(Constants.DUMMY) self.assertTrue(result) def test_perform_web_request(self): mock = MagicMock() mock.request.return_value = mock mock.json.return_value = {Constants.DUMMY: [1], 'total': 50, 'limit': 1} requestor = HttpRequestor(client=mock) result = requestor.perform_web_request(Constants.DUMMY, Constants.DUMMY) self.assertIsNotNone(result) requestor = HttpRequestor(client=mock, circuit_breaker=mock) result = requestor.perform_web_request(Constants.DUMMY, Constants.DUMMY) self.assertIsNotNone(result) def test_get_auth_value(self): self.assertIsNotNone(HttpRequestor.get_auth_value(Constants.DUMMY, Constants.DUMMY)) @skipUnless(Settings.RUN_SKIPPED, Constants.RUN_SKIPPED_MSG) def test_real_get(self): requestor = HttpRequestor() result = requestor.get(GOOGLE_URL) self.assertEqual(result.status_code, Constants.SUCCESS_RESPONSE_CODE)
def __init__(self, http=HttpRequestor()): self.http = http self.logger = Logger(Settings.SERVICE_NAME) self.headers = {Constants.CONTENT_TYPE_HEADER: Constants.CONTENT_TYPE_JSON, Constants.ACCEPT_HEADER: Constants.CONTENT_TYPE_JSON}
def __init__(self, http_client=HttpRequestor(), log_url=Settings.REMOTE_LOG_URL): self.client = http_client self.log_url = log_url