def create_access_key(self, name, is_active=True, permitted=[], options={}): """ Creates a new access key. A master key must be set first. :param name: the name of the access key to create :param is_active: Boolean value dictating whether this key is currently active (default True) :param permitted: list of strings describing which operation types this key will permit Legal values include "writes", "queries", "saved_queries", "cached_queries", "datasets", and "schema". :param options: dictionary containing more details about the key's permitted and restricted functionality """ url = "{0}/{1}/projects/{2}/keys".format(self.base_url, self.api_version, self.project_id) headers = utilities.headers(self.master_key) payload_dict = { "name": name, "is_active": is_active, "permitted": permitted, "options": options } payload = json.dumps(payload_dict) response = self.fulfill(HTTPMethods.POST, url, data=payload, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def update_access_key_full(self, key, name, is_active, permitted, options): """ Replaces the 'name', 'is_active', 'permitted', and 'options' values of a given key. A master key must be set first. :param key: the 'key' value of the access key for which the values will be replaced :param name: the new name desired for this access key :param is_active: whether the key should become enabled (True) or revoked (False) :param permitted: the new list of permissions desired for this access key :param options: the new dictionary of options for this access key """ url = "{0}/{1}/projects/{2}/keys/{3}".format(self.base_url, self.api_version, self.project_id, key) headers = utilities.headers(self.master_key) payload_dict = { "name": name, "is_active": is_active, "permitted": permitted, "options": options } payload = json.dumps(payload_dict) response = self.fulfill(HTTPMethods.POST, url, data=payload, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def query(self, analysis_type, params, all_keys=False): """ Performs a query using the Keen IO analysis API. A read key must be set first. """ if not self.read_key: raise exceptions.InvalidEnvironmentError( "The Keen IO API requires a read key to perform queries. " "Please set a 'read_key' when initializing the " "KeenApi object.") url = "{0}/{1}/projects/{2}/queries/{3}".format( self.base_url, self.api_version, self.project_id, analysis_type) headers = utilities.headers(self.read_key) payload = params response = self.fulfill(HTTPMethods.GET, url, params=payload, headers=headers, timeout=self.get_timeout) self._error_handling(response) response = response.json() if not all_keys: response = response["result"] return response
def query(self, analysis_type, params, all_keys=False): """ Performs a query using the Keen IO analysis API. A read key must be set first. """ if not self._order_by_is_valid_or_none(params): raise ValueError( "order_by given is invalid or is missing required group_by.") if not self._limit_is_valid_or_none(params): raise ValueError( "limit given is invalid or is missing required order_by.") url = "{0}/{1}/projects/{2}/queries/{3}".format( self.base_url, self.api_version, self.project_id, analysis_type) headers = utilities.headers(self.read_key) payload = params response = self.fulfill(HTTPMethods.GET, url, params=payload, headers=headers, timeout=self.get_timeout) self._error_handling(response) response = response.json() if not all_keys: response = response["result"] return response
def list_access_keys(self): """ Returns a list of all access keys in this project. A master key must be set first. """ url = "{0}/{1}/projects/{2}/keys".format(self.base_url, self.api_version, self.project_id) headers = utilities.headers(self.master_key) response = self.fulfill(HTTPMethods.GET, url, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def get_all_collections(self): """ Extracts schema for all collections using the Keen IO API. A read key must be set first. """ url = "{0}/{1}/projects/{2}/events".format(self.base_url, self.api_version, self.project_id) headers = utilities.headers(self.read_key) response = self.fulfill(HTTPMethods.GET, url, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def get_access_key(self, access_key_id): """ Returns details on a particular access key. A master key must be set first. :param access_key_id: the 'key' value of the access key to retreive data from """ url = "{0}/{1}/projects/{2}/keys/{3}".format(self.base_url, self.api_version, self.project_id, access_key_id) headers = utilities.headers(self.master_key) response = self.fulfill(HTTPMethods.GET, url, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def unrevoke_access_key(self, access_key_id): """ Re-enables an access key. :param access_key_id: the 'key' value of the access key to re-enable (unrevoke) """ url = "{0}/{1}/projects/{2}/keys/{3}/unrevoke".format(self.base_url, self.api_version, self.project_id, access_key_id) headers = utilities.headers(self.master_key) response = self.fulfill(HTTPMethods.POST, url, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def get_collection(self, event_collection): """ Extracts info about a collection using the Keen IO API. A master key must be set first. :param event_collection: the name of the collection to retrieve info for """ url = "{0}/{1}/projects/{2}/events/{3}".format(self.base_url, self.api_version, self.project_id, event_collection) headers = utilities.headers(self.read_key) response = self.fulfill(HTTPMethods.GET, url, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def all(self): """ Gets all saved queries for a project from the Keen IO API. Master key must be set. """ keen_api = KeenApi(self.project_id, master_key=self.master_key) self._check_for_master_key() url = "{0}/{1}/projects/{2}/queries/saved".format( keen_api.base_url, keen_api.api_version, self.project_id) response = keen_api.fulfill("get", url, headers=utilities.headers(self.master_key)) return response.json()
def post_event(self, event): """ Posts a single event to the Keen IO API. The write key must be set first. :param event: an Event to upload """ url = "{0}/{1}/projects/{2}/events/{3}".format(self.base_url, self.api_version, self.project_id, event.event_collection) headers = utilities.headers(self.write_key) payload = event.to_json() response = self.fulfill(HTTPMethods.POST, url, data=payload, headers=headers, timeout=self.post_timeout) self._error_handling(response)
def _get_json(self, http_method, url, key, *args, **kwargs): response = self.api.fulfill(http_method, url, headers=headers(key), *args, **kwargs) self.api._error_handling(response) try: response = response.json() except ValueError: response = "No JSON available." return response
def create(self, query_name, saved_query): """ Creates the saved query via a PUT request to Keen IO Saved Query endpoint. Master key must be set. """ keen_api = KeenApi(self.project_id, master_key=self.master_key) self._check_for_master_key() url = "{0}/{1}/projects/{2}/queries/saved/{3}".format( keen_api.base_url, keen_api.api_version, self.project_id, query_name) response = keen_api.fulfill("put", url, headers=utilities.headers(self.master_key), data=saved_query) keen_api._error_handling(response) return response.json()
def results(self, query_name): """ Gets a single saved query with a 'result' object for a project from thei Keen IO API given a query name. Read or Master key must be set. """ keen_api = KeenApi(self.project_id, master_key=self.master_key) self._check_for_master_or_read_key() url = "{0}/{1}/projects/{2}/queries/saved/{3}/result".format( keen_api.base_url, keen_api.api_version, self.project_id, query_name) key = self.master_key if self.master_key else self.read_key response = keen_api.fulfill("get", url, headers=utilities.headers(key)) keen_api._error_handling(response) return response.json()
def revoke_access_key(self, key): """ Revokes an access key. "Bad dog! No biscuit!" :param key: the 'key' value of the access key to revoke """ url = "{0}/{1}/projects/{2}/keys/{3}/revoke".format( self.base_url, self.api_version, self.project_id, key) headers = utilities.headers(self.master_key) response = self.fulfill(HTTPMethods.POST, url, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def _get_json(self, http_method, url, key, *args, **kwargs): response = self.api.fulfill( http_method, url, headers=utilities.headers(key), *args, **kwargs) self.api._error_handling(response) try: response = response.json() except ValueError: response = "No JSON available." return response
def delete(self, query_name): """ Deletes a saved query from a project with a query name. Master key must be set. """ keen_api = KeenApi(self.project_id, master_key=self.master_key) self._check_for_master_key() url = "{0}/{1}/projects/{2}/queries/saved/{3}".format( keen_api.base_url, keen_api.api_version, self.project_id, query_name) response = keen_api.fulfill("delete", url, headers=utilities.headers(self.master_key)) keen_api._error_handling(response) return True
def delete_access_key(self, key): """ Deletes an access key. :param key: the 'key' value of the access key to delete """ url = "{0}/{1}/projects/{2}/keys/{3}".format(self.base_url, self.api_version, self.project_id, key) headers = utilities.headers(self.master_key) response = self.fulfill(HTTPMethods.DELETE, url, headers=headers, timeout=self.get_timeout) self._error_handling(response) return True
def delete_events(self, event_collection, params): """ Deletes events via the Keen IO API. A master key must be set first. :param event_collection: string, the event collection from which event are being deleted """ url = "{0}/{1}/projects/{2}/events/{3}".format(self.base_url, self.api_version, self.project_id, event_collection) headers = utilities.headers(self.master_key) response = self.fulfill(HTTPMethods.DELETE, url, params=params, headers=headers, timeout=self.post_timeout) self._error_handling(response) return True
def query(self, analysis_type, params, all_keys=False): """ Performs a query using the Keen IO analysis API. A read key must be set first. """ url = "{0}/{1}/projects/{2}/queries/{3}".format( self.base_url, self.api_version, self.project_id, analysis_type) headers = utilities.headers(self.read_key) payload = params response = self.fulfill(HTTPMethods.GET, url, params=payload, headers=headers, timeout=self.get_timeout) self._error_handling(response) response = response.json() if not all_keys: response = response["result"] return response
def update_access_key_full(self, access_key_id, name, is_active, permitted, options): """ Replaces the 'name', 'is_active', 'permitted', and 'options' values of a given key. A master key must be set first. :param access_key_id: the 'key' value of the access key for which the values will be replaced :param name: the new name desired for this access key :param is_active: whether the key should become enabled (True) or revoked (False) :param permitted: the new list of permissions desired for this access key :param options: the new dictionary of options for this access key """ url = "{0}/{1}/projects/{2}/keys/{3}".format(self.base_url, self.api_version, self.project_id, access_key_id) headers = utilities.headers(self.master_key) payload_dict = { "name": name, "is_active": is_active, "permitted": permitted, "options": options } payload = json.dumps(payload_dict) response = self.fulfill(HTTPMethods.POST, url, data=payload, headers=headers, timeout=self.get_timeout) self._error_handling(response) return response.json()
def post_event(self, event): """ Posts a single event to the Keen IO API. The write key must be set first. :param event: an Event to upload """ if not self.write_key: raise exceptions.InvalidEnvironmentError( "The Keen IO API requires a write key to send events. " "Please set a 'write_key' when initializing the " "KeenApi object.") url = "{0}/{1}/projects/{2}/events/{3}".format(self.base_url, self.api_version, self.project_id, event.event_collection) headers = utilities.headers(self.write_key) payload = event.to_json() response = self.fulfill(HTTPMethods.POST, url, data=payload, headers=headers, timeout=self.post_timeout) self._error_handling(response)
def query(self, analysis_type, params, all_keys=False): """ Performs a query using the Keen IO analysis API. A read key must be set first. """ if not self._order_by_is_valid_or_none(params): raise ValueError("order_by given is invalid or is missing required group_by.") if not self._limit_is_valid_or_none(params): raise ValueError("limit given is invalid or is missing required order_by.") url = "{0}/{1}/projects/{2}/queries/{3}".format(self.base_url, self.api_version, self.project_id, analysis_type) headers = utilities.headers(self.read_key) payload = params response = self.fulfill(HTTPMethods.GET, url, params=payload, headers=headers, timeout=self.get_timeout) self._error_handling(response) response = response.json() if not all_keys: response = response["result"] return response