"""Api Gateway for sending wordpress form data.""" import os import requests from .integration_test_helper import IntegrationTestHelper class ApiGateway: """Api Gateway Class.""" def __init__(self, env='qa'): """Initialise gateway environment.""" self.prefix = '' self.token_endpoint = '' self.api_gateway = '' if env == 'qa': self.setup_qa_var() if env == 'prod': self.setup_prod_var() self.client_id = os.environ.get('AUTH_CLIENT_ID') self.client_secret = os.environ.get('AUTH_CLIENT_SECRET') def setup_qa_var(self): """Initialise qa variables.""" self.prefix = 'qa-' self.token_endpoint = f'https://{self.prefix}orchard.auth0.com' \ f'/oauth/token' self.api_gateway = f'https://{self.prefix}gda-gateway.theorchard.io/' \ f'artist-submission' def setup_prod_var(self): """Initialise prod variables.""" self.prefix = 'prod-' self.token_endpoint = 'https://workstation.auth0.com/oauth/token' self.api_gateway = 'https://gda-gateway.theorchard.io/' \ 'artist-submission' def get_jwt_token(self): """Generate jwt token.""" header = {'content-type': 'application/json'} jwt_gateway = f'https://{self.prefix}gda-gateway-auth0-jwt-authorizer' data = {'client_id': self.client_id, 'client_secret': self.client_secret, 'audience': jwt_gateway, 'grant_type': 'client_credentials'} r = requests.post(self.token_endpoint, json=data, headers=header) r.raise_for_status() return r.json()['access_token'] def post_form_to_gda_gateway(self, spotify_id, email=''): """Post salesforce form data to api gateway.""" token = self.get_jwt_token() if not email: email = IntegrationTestHelper.random_string('@integrationtest.com') header = {'Authorization': f'Bearer {token}'} data = {'email': email, 'first_name': 'LambdaTestQAE2E', 'last_name': 'LambdaTestQA', 'company': 'Test Company Integration', 'role': 'Artist or Band', 'spotify_url': f'https://open.spotify.com/artist/{spotify_id}', 'country': 'USA', 'language': 'en', 'signing_entity': 'gbr', 'currency': 'USD', 'external_test': True} r = requests.post(self.api_gateway, json=data, headers=header) r.raise_for_status() print(f'Generated email = {email}') return email