import logging from datetime import datetime from os import path from urllib.parse import urlencode import pandas from ui_automation_framework.utils import logger as cl from app.api_client import ApiClient log = cl.Logger(logging.DEBUG) prod_env = "https://proxy.apollo.stream" test_env = "https://test-proxy.apollo.stream" apollo_client_id = "" apollo_client_secret = "" apollo_token_url = "" test_user = "" test_password = "" test_client_id = "" test_client_secret = "" test_audience = "" test_grant_type = "" test_realm = "" test_host = "" class APIOverride(ApiClient): def __init__(self): self.api_prod_client = ApiClient(host_endpoint=prod_env) self.api_test_client = ApiClient(host_endpoint=test_env) def get_prod_response(self, url): return self.api_prod_client.get_response(endpoint=url, token=self.prod_authorize(), user="", env=prod_env)["body"] def get_test_response(self, url): return self.api_test_client.get_response(endpoint=url, token=self.test_authorize(), user="", env=test_env)["body"] def test_apollo_api_connection(self): response = self.get_prod_response("/sonytracks/analyze?playlisturi=spotify%3Aplaylist%3A37i9dQZF1DXcBWIGoYBM5M") df = pandas.DataFrame({"Response": response}) df.reset_index().to_excel("apollo_api_response.xlsx") def prod_authorize(self): today = str(datetime.today().date()) file_name = f"token_{today}.txt" if path.exists(file_name): file = open(file_name) access_token = file.readlines()[0] else: data = {"audience": "apollo|api", "grant_type": "client_credentials", "client_id": apollo_client_id, "client_secret": apollo_client_secret} headers = {"content-type": "application/x-www-form-urlencoded"} session_token = self.session.post(url=apollo_token_url, data=urlencode(data), headers=headers) self.logger.info(f"apollo token response: {session_token.json()}") access_token = session_token.json().get("access_token") f = open(file_name, "a") f.write(access_token) f.close() self.logger.info(access_token) if access_token is None: raise ValueError("Can not get the access token!") self.logger.info(f"apollo token now: {access_token}") return access_token def test_authorize(self): access_token = {} user = test_user # # if access_token.get(user): # return access_token.get(user) data = { "client_id": test_client_id, "client_secret": test_client_secret, "audience": test_audience, "grant_type": test_grant_type, "realm": test_realm, "username": user, "password": test_password, } headers = {"content-type": "application/x-www-form-urlencoded"} session_token = self.session.post(test_host, urlencode(data), headers=headers) access_token[user] = session_token.json().get("access_token") # refresh token -> access token new self.logger.info(access_token[user]) if access_token[user] is None: raise ValueError("Can not get the access token!") return access_token[user]