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 = { "Content-Type": "application/json", "Authorization": self.write_key } payload = event.to_json() response = requests.post(url, data=payload, headers=headers) if response.status_code != 201: error = response.json() raise exceptions.KeenApiError(error)
def query(self, analysis_type, params): """ 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 = {"Authorization": self.read_key} payload = params response = self.fulfill(HTTPMethods.GET, url, params=payload, headers=headers, timeout=self.get_timeout) if response.status_code != 200: error = response.json() raise exceptions.KeenApiError(error) return response.json()["result"]
def post_events(self, events): """ Posts a single event to the Keen IO API. The write key must be set first. :param events: 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".format(self.base_url, self.api_version, self.project_id) headers = { "Content-Type": "application/json", "Authorization": self.write_key } payload = json.dumps(events) response = self.fulfill(HTTPMethods.POST, url, data=payload, headers=headers, timeout=self.post_timeout) if response.status_code != 200: error = response.json() raise exceptions.KeenApiError(error)
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 = { "Content-Type": "application/json", "Authorization": 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 _throw_key_missing(key, relying_on_master): message = ("The Keen IO API requires a {0} key to perform queries. " "Please set a '{0}_key' when initializing the " "KeenApi object.") if relying_on_master: message += ' The "master_key" is set, but one should prefer the key with least privilege.' raise exceptions.InvalidEnvironmentError(message.format(key))
def get_all_collections(self): """ Extracts schema for all collections using the Keen IO API. A master key must be set first. """ if not self.master_key: raise exceptions.InvalidEnvironmentError( "The Keen IO API requires a master key to get event collection schema. " "Please set a 'master_key' when initializing the " "KeenApi object.") url = "{0}/{1}/projects/{2}/events".format(self.base_url, self.api_version, self.project_id) headers = {"Authorization": self.master_key} response = self.fulfill(HTTPMethods.GET, url, headers=headers, timeout=self.get_timeout) self.error_handling(response) return response.json()
def post_events(self, events): """ Posts a single event to the Keen IO API. The write key must be set first. :param events: 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".format(self.base_url, self.api_version, self.project_id) headers = utilities.headers(self.write_key) payload = json.dumps(events) response = self.fulfill(HTTPMethods.POST, url, data=payload, headers=headers, timeout=self.post_timeout) self._error_handling(response) return self._get_response_json(response)
def _check_for_master_key(self): if not self.master_key: raise exceptions.InvalidEnvironmentError( "The Keen IO API requires a master key to perform this operation. " "Please set a 'master_key' when initializing the " "KeenApi object.")
def _check_for_master_or_read_key(self): if not (self.read_key or self.master_key): raise exceptions.InvalidEnvironmentError( "The Keen IO API requires a read key or master key to perform this operation on saved queries. " "Please set a 'read_key' or 'master_key' when initializing the " "KeenApi object.")