def get_version(self, auth=False): """ get api version info auth (bool): use authenticated endpoint for benchmark purposes returns json version info """ if auth: url = self.url_prefix + "/authversion" else: url = self.url_prefix + "/version" resp = self._get(url) raise_errors(resp) return resp.json()
def _delete(self, url, timeout=None, **kwargs): """ wrap requests session to re-authenticate on credential expiration """ if not url.startswith("http"): url = self.url_prefix + url if timeout is None: timeout = self.timeout try: resp = self.session.delete(url, timeout=timeout, **kwargs) raise_errors(resp) except CredentialError: self.__authenticate() raise return resp
def get_all_authorized_tenants(cls, username=None, password=None, url_prefix="https://api.cogniac.io/1"): """ return the list of valid tenants for the specified user credentials and url_prefix """ if 'COG_API_KEY' in os.environ: resp = requests.get(url_prefix + "/users/current/tenants", headers={"Authorization": "Key %s" % os.environ['COG_API_KEY']}) raise_errors(resp) return resp.json() if username is None and password is None: # credentials not specified, use environment variables if found try: username = os.environ['COG_USER'] password = os.environ['COG_PASS'] except: raise Exception("No Cogniac Credentials. Try setting COG_USER and COG_PASS environment.") resp = requests.get(url_prefix + "/users/current/tenants", auth=HTTPBasicAuth(username, password)) raise_errors(resp) return resp.json()
def __authenticate(self): # Authenticate to the cogniac system using a username and password or an API KEY # Save the http Authorization headers that can be used for subsequent http requests to the cogniac API. tenant_data = {"tenant_id": self.tenant_id} if self.api_key: # trade API KEY for user+tenant token resp = requests.get(self.url_prefix + "/token", params=tenant_data, headers={"Authorization": "Key %s" % self.api_key}, timeout=self.timeout) else: # trade username/password for user+tenant token resp = requests.get(self.url_prefix + "/token", params=tenant_data, auth=HTTPBasicAuth(self.username, self.password), timeout=self.timeout) raise_errors(resp) token = resp.json() headers = {"Authorization": "Bearer %s" % token['access_token']} self.session = requests.Session() self.session.headers.update(headers)