<s> import json <EOL> import ssl <EOL> import requests <EOL> from requests . adapters import HTTPAdapter <EOL> from requests . packages . urllib3 . poolmanager import PoolManager <EOL> from keen import exceptions <EOL> from requests . compat import json <EOL> __author__ = '<STR_LIT>' <EOL> class HTTPMethods ( object ) : <EOL> """<STR_LIT>""" <EOL> GET = '<STR_LIT>' <EOL> POST = '<STR_LIT>' <EOL> DELETE = '<STR_LIT>' <EOL> class KeenAdapter ( HTTPAdapter ) : <EOL> """<STR_LIT>""" <EOL> def init_poolmanager ( self , connections , maxsize , block = False ) : <EOL> """<STR_LIT>""" <EOL> self . poolmanager = PoolManager ( num_pools = connections , <EOL> maxsize = maxsize , <EOL> block = block , <EOL> ssl_version = ssl . PROTOCOL_TLSv1 ) <EOL> class KeenApi ( object ) : <EOL> """<STR_LIT>""" <EOL> base_url = "<STR_LIT>" <EOL> api_version = "<STR_LIT>" <EOL> def __init__ ( self , project_id , write_key = None , read_key = None , <EOL> base_url = None , api_version = None , get_timeout = None , post_timeout = None , <EOL> master_key = None ) : <EOL> """<STR_LIT>""" <EOL> super ( KeenApi , self ) . __init__ ( ) <EOL> self . project_id = project_id <EOL> self . write_key = write_key <EOL> self . read_key = read_key <EOL> self . master_key = master_key <EOL> if base_url : <EOL> self . base_url = base_url <EOL> if api_version : <EOL> self . api_version = api_version <EOL> self . get_timeout = get_timeout <EOL> self . post_timeout = post_timeout <EOL> self . session = self . _create_session ( ) <EOL> def fulfill ( self , method , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self . session , method ) ( * args , ** kwargs ) <EOL> def post_event ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if not self . write_key : <EOL> raise exceptions . InvalidEnvironmentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> url = "<STR_LIT>" . format ( self . base_url , self . api_version , <EOL> self . project_id , <EOL> event . event_collection ) <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" , "<STR_LIT>" : self . write_key } <EOL> payload = event . to_json ( ) <EOL> response = self . fulfill ( HTTPMethods . POST , url , data = payload , headers = headers , timeout = self . post_timeout ) <EOL> self . _error_handling ( response ) <EOL> def post_events ( self , events ) : <EOL> """<STR_LIT>""" <EOL> if not self . write_key : <EOL> raise exceptions . InvalidEnvironmentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> url = "<STR_LIT>" . format ( self . base_url , self . api_version , <EOL> self . project_id ) <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" , "<STR_LIT>" : self . write_key } <EOL> payload = json . dumps ( events ) <EOL> response = self . fulfill ( HTTPMethods . POST , url , data = payload , headers = headers , timeout = self . post_timeout ) <EOL> self . _error_handling ( response ) <EOL> def query ( self , analysis_type , params , all_keys = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . read_key : <EOL> raise exceptions . InvalidEnvironmentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> url = "<STR_LIT>" . format ( self . base_url , self . api_version , <EOL> self . project_id , analysis_type ) <EOL> headers = { "<STR_LIT>" : self . read_key } <EOL> payload = params <EOL> response = self . fulfill ( HTTPMethods . GET , url , params = payload , headers = headers , timeout = self . get_timeout ) <EOL> self . _error_handling ( response ) <EOL> response = response . json ( ) <EOL> if not all_keys : <EOL> response = response [ "<STR_LIT:result>" ] <EOL> return response <EOL> def delete_events ( self , event_collection , params ) : <EOL> """<STR_LIT>""" <EOL> self . _check_for_master_key ( ) <EOL> url = "<STR_LIT>" . format ( self . base_url , <EOL> self . api_version , <EOL> self . project_id , <EOL> event_collection ) <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" , "<STR_LIT>" : self . master_key } <EOL> response = self . fulfill ( HTTPMethods . DELETE , url , params = params , headers = headers , timeout = self . post_timeout ) <EOL> self . _error_handling ( response ) <EOL> return True <EOL> def get_collection ( self , event_collection ) : <EOL> """<STR_LIT>""" <EOL> self . _check_for_master_key ( ) <EOL> url = "<STR_LIT>" . format ( self . base_url , self . api_version , <EOL> self . project_id , event_collection ) <EOL> headers = { "<STR_LIT>" : self . master_key } <EOL> response = self . fulfill ( HTTPMethods . GET , url , headers = headers , timeout = self . get_timeout ) <EOL> self . _error_handling ( response ) <EOL> return response . json ( ) <EOL> def get_all_collections ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _check_for_master_key ( ) <EOL> url = "<STR_LIT>" . format ( self . base_url , self . api_version , <EOL> self . project_id ) <EOL> headers = { "<STR_LIT>" : self . master_key } <EOL> response = self . fulfill ( HTTPMethods . GET , url , headers = headers , timeout = self . get_timeout ) <EOL> self . _error_handling ( response ) <EOL> return response . json ( ) <EOL> def _error_handling ( self , res ) : <EOL> """<STR_LIT>""" <EOL> if res . status_code // <NUM_LIT:100> != <NUM_LIT:2> : <EOL> try : <EOL> error = res . json ( ) <EOL> except ValueError : <EOL> error = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' . format ( res . text [ : <NUM_LIT:1000> ] ) , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> raise exceptions . KeenApiError ( error ) <EOL> def _create_session ( self ) : <EOL> """<STR_LIT>""" <EOL> s = requests . Session ( ) <EOL> s . mount ( '<STR_LIT>' , KeenAdapter ( ) ) <EOL> return s <EOL> def _check_for_master_key ( self ) : <EOL> if not self . master_key : <EOL> raise exceptions . InvalidEnvironmentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) </s>
