def build(api_secrets_filename, okta_response_handler=None): """ :param api_secrets_filename: name api configuration file :param okta_response_handler: optional function to handle Okta response :return: ApiClient correctly configured with credentials and host """ # Load the configuration configuration = ApiConfigurationLoader().load(api_secrets_filename) # Prepare our authentication request token_request_body = f"grant_type=password&username={configuration.username}" \ f"&password={configuration.password}&scope=openid client groups offline_access" \ f"&client_id={configuration.client_id}&client_secret={configuration.client_secret}" headers = { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded" } # Make our authentication request okta_response = requests.post(configuration.token_url, data=token_request_body, headers=headers) if okta_response_handler is not None: okta_response_handler(okta_response) # Ensure that we have a 200 response code assert okta_response.status_code == 200 # convert the json encoded response to be able to extract the token values okta_json = okta_response.json() # Retrieve our api token from the authentication response api_token = RefreshingToken( token_url=configuration.token_url, client_id=configuration.client_id, client_secret=configuration.client_secret, initial_access_token=okta_json["access_token"], initial_token_expiry=okta_json["expires_in"], refresh_token=okta_json["refresh_token"]) # Initialise our API client using our token so that we can include it in all future requests config = lusid.Configuration() config.access_token = api_token config.host = configuration.api_url return lusid.ApiClient(config, header_name="X-LUSID-Application", header_value=configuration.app_name)
def __enter__(self): """ The __enter__ method is called when using Python's built in "with" statement e.g. with CachingApi() as api. :return: """ # If record_mode is true connect to LUSID if self.record_mode: self.api = lse.connect() else: # Otherwise import lusid self.api = lse.ExtendedAPI({}, lusid.ApiClient(lusid.Configuration()), lusid) # Replace the original 'call' with our interceptor self.api.call = self.Interceptor( original=self.api.call, wrapper=self.recorder if self.record_mode else self.reader) return self.api
def connect(config, **kwargs): if "api" not in config.keys(): config["api"] = {} config["api"]["tokenUrl"] = None config["api"]["username"] = None config["api"]["password"] = None config["api"]["clientId"] = None config["api"]["clientSecret"] = None config["api"]["apiUrl"] = None token_url = os.getenv("FBN_TOKEN_URL", config["api"]["tokenUrl"]) username = os.getenv("FBN_USERNAME", config["api"]["username"]) password = quote(os.getenv("FBN_PASSWORD", config["api"]["password"]), "*!") client_id = quote(os.getenv("FBN_CLIENT_ID", config["api"]["clientId"]), "*!") client_secret = quote( os.getenv("FBN_CLIENT_SECRET", config["api"]["clientSecret"]), "*!" ) api_url = os.getenv("FBN_LUSID_API_URL", config["api"]["apiUrl"]) token_request_body = ( "grant_type=password&username={0}".format(username) + "&password={0}&scope=openid client groups".format(password) + "&client_id={0}&client_secret={1}".format(client_id, client_secret) ) headers = { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded", } config = lusid.Configuration() config.access_token = RefreshingToken(token_url, token_request_body, headers) config.host = api_url return (lusid.ApiClient(config), lusid)
def authenticate_secrets(): class LusidApi(): def __init__(self, client): self.aggregation = lusid.AggregationApi(client) self.analytics_stores = lusid.AnalyticsStoresApi(client) self.metadata = lusid.ApplicationMetadataApi(client) self.corporate_action_sources = lusid.CorporateActionSourcesApi( client) self.data_types = lusid.DataTypesApi(client) self.derived_transaction_portfolios = lusid.DerivedTransactionPortfoliosApi( client) self.instruments = lusid.InstrumentsApi(client) self.login = lusid.InstrumentsApi(client) self.portfolio_groups = lusid.PortfolioGroupsApi(client) self.portfolios = lusid.PortfoliosApi(client) self.property_definitions = lusid.PropertyDefinitionsApi(client) self.quotes = lusid.QuotesApi(client) self.reconciliations = lusid.ReconciliationsApi(client) self.reference_portfolios = lusid.ReferencePortfolioApi(client) self.results = lusid.ResultsApi(client) self.schemas = lusid.SchemasApi(client) self.scopes = lusid.ScopesApi(client) self.search = lusid.SearchApi(client) self.system_configuration = lusid.SystemConfigurationApi(client) self.transaction_portfolios = lusid.TransactionPortfoliosApi( client) self.cut_labels = lusid.CutLabelDefinitionsApi(client) environment = os.getenv("FBN_DEPLOYMENT_ENVIRONMENT", None) if environment == "JupyterNotebook": config = lusid.Configuration() config.access_token = RefreshingToken() api_url = os.getenv("FBN_LUSID_API_URL", None) if api_url is None: raise KeyError( "Missing FBN_LUSID_API_URL environment variable, please set it to the LUSID base API url" ) config.host = api_url client = lusid.ApiClient(config, header_name="X-LUSID-Application", header_value="LusidJupyterNotebook") return LusidApi(client) # Load our configuration details from the environment variables token_url = os.getenv("FBN_TOKEN_URL", None) api_url = os.getenv("FBN_LUSID_API_URL", None) username = os.getenv("FBN_USERNAME", None) password_raw = os.getenv("FBN_PASSWORD", None) client_id_raw = os.getenv("FBN_CLIENT_ID", None) client_secret_raw = os.getenv("FBN_CLIENT_SECRET", None) # If any of the environmental variables are missing use a local secrets file if token_url is None or username is None or password_raw is None or client_id_raw is None \ or client_secret_raw is None or api_url is None: dir_path = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dir_path, "../secrets.json"), "r") as secrets: config = json.load(secrets) token_url = os.getenv("FBN_TOKEN_URL", config["api"]["tokenUrl"]) username = os.getenv("FBN_USERNAME", config["api"]["username"]) password = pathname2url( os.getenv("FBN_PASSWORD", config["api"]["password"])) client_id = pathname2url( os.getenv("FBN_CLIENT_ID", config["api"]["clientId"])) client_secret = pathname2url( os.getenv("FBN_CLIENT_SECRET", config["api"]["clientSecret"])) api_url = os.getenv("FBN_LUSID_API_URL", config["api"]["apiUrl"]) else: password = pathname2url(password_raw) client_id = pathname2url(client_id_raw) client_secret = pathname2url(client_secret_raw) # Prepare our authentication request token_request_body = ( "grant_type=password&username={0}".format(username) + "&password={0}&scope=openid client groups".format(password) + "&client_id={0}&client_secret={1}".format(client_id, client_secret)) headers = { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded" } # Make our authentication request okta_response = requests.post(token_url, data=token_request_body, headers=headers) # Ensure that we have a 200 response code assert okta_response.status_code == 200 # Retrieve our api token from the authentication response api_token = okta_response.json()["access_token"] # Initialise our API client using our token so that we can include it in all future requests config = lusid.Configuration() config.access_token = api_token config.host = api_url client = lusid.ApiClient(config) return LusidApi(client)
def connect(config): lusid_config = lusid.Configuration() lusid_config.access_token = config["token"] lusid_config.host = config["apiUrl"] return lusid.ApiClient(lusid_config), lusid
def build(self, api_configuration): """ :param api_configuration: name api configuration file :return: ApiClient correctly configured with credentials and host """ # Load our configuration details from the environment variables token_url = os.getenv("FBN_TOKEN_URL", None) api_url = os.getenv("FBN_LUSID_API_URL", None) username = os.getenv("FBN_USERNAME", None) password_raw = os.getenv("FBN_PASSWORD", None) client_id_raw = os.getenv("FBN_CLIENT_ID", None) client_secret_raw = os.getenv("FBN_CLIENT_SECRET", None) app_name = os.getenv("FBN_APP_NAME", "") # If any of the environmental variables are missing use a local secrets file if token_url is None or username is None or password_raw is None or client_id_raw is None \ or client_secret_raw is None or api_url is None: dir_path = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dir_path, api_configuration), "r") as secrets: config = json.load(secrets) token_url = os.getenv("FBN_TOKEN_URL", config["api"]["tokenUrl"]) username = os.getenv("FBN_USERNAME", config["api"]["username"]) password = pathname2url( os.getenv("FBN_PASSWORD", config["api"]["password"])) client_id = pathname2url( os.getenv("FBN_CLIENT_ID", config["api"]["clientId"])) client_secret = pathname2url( os.getenv("FBN_CLIENT_SECRET", config["api"]["clientSecret"])) api_url = os.getenv("FBN_LUSID_API_URL", config["api"]["apiUrl"]) app_name = os.getenv("FBN_APP_NAME", config["api"].get("applicationName", "")) else: password = pathname2url(password_raw) client_id = pathname2url(client_id_raw) client_secret = pathname2url(client_secret_raw) # Prepare our authentication request token_request_body = ( "grant_type=password&username={0}".format(username) + "&password={0}&scope=openid client groups".format(password) + "&client_id={0}&client_secret={1}".format(client_id, client_secret)) headers = { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded" } # Make our authentication request okta_response = requests.post(token_url, data=token_request_body, headers=headers) # Ensure that we have a 200 response code assert okta_response.status_code == 200 # Retrieve our api token from the authentication response api_token = okta_response.json()["access_token"] # Initialise our API client using our token so that we can include it in all future requests config = lusid.Configuration() config.access_token = api_token config.host = api_url return lusid.ApiClient(config, header_name="X-LUSID-Application", header_value=app_name)