Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, project_id, write_key=None, read_key=None,
persistence_strategy=None, api_class=KeenApi, get_timeout=305, post_timeout=305,
master_key=None, base_url=None):
""" Initializes a KeenClient object.
:param project_id: the Keen IO project ID
:param write_key: a Keen IO Scoped Key for Writes
:param read_key: a Keen IO Scoped Key for Reads
:param persistence_strategy: optional, the strategy to use to persist
the event
:param get_timeout: optional, the timeout on GET requests
:param post_timeout: optional, the timeout on POST requests
:param master_key: a Keen IO Master API Key
"""
super(KeenClient, self).__init__()
# do some validation
self.check_project_id(project_id)
# Set up an api client to be used for querying and optionally passed
# into a default persistence strategy.
self.api = api_class(project_id, write_key=write_key, read_key=read_key,
get_timeout=get_timeout, post_timeout=post_timeout,
master_key=master_key, base_url=base_url)
if persistence_strategy:
# validate the given persistence strategy
if not isinstance(persistence_strategy, BasePersistenceStrategy):
raise exceptions.InvalidPersistenceStrategyError()
if not persistence_strategy:
# setup a default persistence strategy
def get_keen_client():
keen_project = keen_settings['private'].get('project_id')
read_key = keen_settings['private'].get('read_key')
master_key = keen_settings['private'].get('master_key')
write_key = keen_settings['private'].get('write_key')
if keen_project and read_key and master_key:
client = KeenClient(
project_id=keen_project,
read_key=read_key,
master_key=master_key,
write_key=write_key
)
else:
raise ValueError('Cannot connect to Keen clients - all keys not provided.')
return client
def _initialize_client_from_environment():
''' Initialize a KeenClient instance using environment variables. '''
global _client, project_id, write_key, read_key, master_key, base_url
if _client is None:
# check environment for project ID and keys
project_id = project_id or os.environ.get("KEEN_PROJECT_ID")
write_key = write_key or os.environ.get("KEEN_WRITE_KEY")
read_key = read_key or os.environ.get("KEEN_READ_KEY")
master_key = master_key or os.environ.get("KEEN_MASTER_KEY")
base_url = base_url or os.environ.get("KEEN_BASE_URL")
if not project_id:
raise InvalidEnvironmentError("Please set the KEEN_PROJECT_ID environment variable or set keen.project_id!")
_client = KeenClient(project_id,
write_key=write_key,
read_key=read_key,
master_key=master_key,
base_url=base_url)
def run(self):
keen_client = KeenClient(
project_id=self.project_id,
write_key=self.write_key,
)
timezone = pytz.timezone(self.data['user']['timezone'])
self.events = []
with click.progressbar(self.data['days'],
label='Preparing keen.io events',
fill_char=click.style('#', fg='blue')) as days:
for day in days:
dt = self._parse_date(day['date'], timezone)
self.append_event(dt, 'total', {
'seconds': day['grand_total']['total_seconds'],
def send_events(self, events):
keen_project = keen_settings['private']['project_id']
write_key = keen_settings['private']['write_key']
if keen_project and write_key:
client = KeenClient(
project_id=keen_project,
write_key=write_key,
)
logger.info('Adding {} events to the {} collection'.format(len(events), self.collection_name))
client.add_events({self.collection_name: events})
else:
logger.info('Keen not enabled - would otherwise be adding the following {} events to the {} collection'.format(len(events), self.collection_name))
print(events)
@task(ignore_result=True)
def keen_add_events(events):
KeenClient(project_id=settings.KEEN_PROJECT_ID, write_key=settings.KEEN_WRITE_KEY).add_events(events)