<s> import sys <EOL> import requests <EOL> import json <EOL> try : <EOL> from urllib import urlencode <EOL> except ImportError : <EOL> from urllib . parse import urlencode <EOL> from os import environ <EOL> from base64 import b64encode <EOL> from six . moves . urllib . parse import urlencode <EOL> from nylas . _client_sdk_version import __VERSION__ <EOL> from . util import url_concat , generate_id <EOL> from . restful_model_collection import RestfulModelCollection <EOL> from . restful_models import ( Calendar , Contact , Event , Message , Thread , File , <EOL> Account , APIAccount , SingletonAccount , Folder , <EOL> Label , Draft ) <EOL> from . errors import ( APIClientError , ConnectionError , NotAuthorizedError , <EOL> InvalidRequestError , NotFoundError , MethodNotSupportedError , <EOL> ServerError , ServiceUnavailableError , ConflictError , <EOL> SendingQuotaExceededError , ServerTimeoutError , <EOL> MessageRejectedError ) <EOL> DEBUG = environ . get ( '<STR_LIT>' ) <EOL> API_SERVER = "<STR_LIT>" <EOL> def _validate ( response ) : <EOL> status_code_to_exc = { <NUM_LIT> : InvalidRequestError , <EOL> <NUM_LIT> : NotAuthorizedError , <EOL> <NUM_LIT> : MessageRejectedError , <EOL> <NUM_LIT> : NotAuthorizedError , <EOL> <NUM_LIT> : NotFoundError , <EOL> <NUM_LIT> : MethodNotSupportedError , <EOL> <NUM_LIT> : ConflictError , <EOL> <NUM_LIT> : SendingQuotaExceededError , <EOL> <NUM_LIT> : ServerError , <EOL> <NUM_LIT> : ServiceUnavailableError , <EOL> <NUM_LIT> : ServerTimeoutError } <EOL> request = response . request <EOL> url = request . url <EOL> status_code = response . status_code <EOL> data = request . body <EOL> if DEBUG : <EOL> print ( "<STR_LIT>" . format ( request . method , url , data , <EOL> status_code , response . text ) ) <EOL> try : <EOL> data = json . loads ( data ) if data else None <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> if status_code == <NUM_LIT:200> : <EOL> return response <EOL> elif status_code in status_code_to_exc : <EOL> cls = status_code_to_exc [ status_code ] <EOL> try : <EOL> response = json . loads ( response . text ) <EOL> kwargs = dict ( url = url , status_code = status_code , <EOL> data = data ) <EOL> for key in [ '<STR_LIT:message>' , '<STR_LIT>' ] : <EOL> if key in response : <EOL> kwargs [ key ] = response [ key ] <EOL> raise cls ( ** kwargs ) <EOL> except ( ValueError , TypeError ) : <EOL> raise cls ( url = url , status_code = status_code , <EOL> data = data , message = "<STR_LIT>" ) <EOL> else : <EOL> raise APIClientError ( url = url , status_code = status_code , <EOL> data = data , message = "<STR_LIT>" ) <EOL> def nylas_excepted ( f ) : <EOL> def caught ( * args , ** kwargs ) : <EOL> try : <EOL> return f ( * args , ** kwargs ) <EOL> except requests . exceptions . ConnectionError : <EOL> server = args [ <NUM_LIT:0> ] . api_server <EOL> raise ConnectionError ( url = server ) <EOL> return caught <EOL> class APIClient ( json . JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app_id = environ . get ( '<STR_LIT>' ) , <EOL> app_secret = environ . get ( '<STR_LIT>' ) , <EOL> access_token = environ . get ( '<STR_LIT>' ) , <EOL> api_server = API_SERVER , <EOL> auth_server = None ) : <EOL> if "<STR_LIT>" not in api_server : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . api_server = api_server <EOL> self . authorize_url = api_server + '<STR_LIT>' <EOL> self . access_token_url = api_server + '<STR_LIT>' <EOL> self . app_secret = app_secret <EOL> self . app_id = app_id <EOL> self . session = requests . Session ( ) <EOL> self . version = __VERSION__ <EOL> major , minor , revision , _ , __ = sys . version_info <EOL> version_header = '<STR_LIT>' . format ( self . version , <EOL> major , minor , <EOL> revision ) <EOL> self . session . headers = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : version_header } <EOL> self . access_token = access_token <EOL> self . admin_session = requests . Session ( ) <EOL> if app_secret is not None : <EOL> self . admin_session . headers = { '<STR_LIT>' : '<STR_LIT>' + <EOL> app_secret , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : version_header } <EOL> @ property <EOL> def access_token ( self ) : <EOL> return self . _access_token <EOL> @ access_token . setter <EOL> def access_token ( self , value ) : <EOL> self . _access_token = value <EOL> if value : <EOL> self . session . headers . update ( { '<STR_LIT>' : '<STR_LIT>' + <EOL> value } ) <EOL> else : <EOL> if '<STR_LIT>' in self . session . headers : <EOL> del self . session . headers [ '<STR_LIT>' ] <EOL> def authentication_url ( self , redirect_uri , login_hint = '<STR_LIT>' ) : <EOL> args = { '<STR_LIT>' : redirect_uri , <EOL> '<STR_LIT>' : self . app_id , <EOL> '<STR_LIT>' : '<STR_LIT:code>' , <EOL> '<STR_LIT>' : '<STR_LIT:email>' , <EOL> '<STR_LIT>' : login_hint , <EOL> '<STR_LIT:state>' : generate_id ( ) } <EOL> return url_concat ( self . authorize_url , args ) <EOL> def token_for_code ( self , code ) : <EOL> args = { '<STR_LIT>' : self . app_id , <EOL> '<STR_LIT>' : self . app_secret , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : code } <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> resp = requests . post ( self . access_token_url , data = urlencode ( args ) , <EOL> headers = headers ) . json ( ) <EOL> self . auth_token = resp [ u'<STR_LIT>' ] <EOL> return self . auth_token <EOL> def is_opensource_api ( self ) : <EOL> if self . app_id is None and self . app_secret is None : <EOL> return True <EOL> return False <EOL> @ property <EOL> def account ( self ) : <EOL> return self . _get_resource ( SingletonAccount , '<STR_LIT>' ) <EOL> @ property <EOL> def accounts ( self ) : <EOL> if self . is_opensource_api ( ) : <EOL> return RestfulModelCollection ( APIAccount , self ) <EOL> else : <EOL> return RestfulModelCollection ( Account , self ) <EOL> @ property <EOL> def threads ( self ) : <EOL> return RestfulModelCollection ( Thread , self ) <EOL> @ property <EOL> def folders ( self ) : <EOL> return RestfulModelCollection ( Folder , self ) <EOL> @ property <EOL> def labels ( self ) : <EOL> return RestfulModelCollection ( Label , self ) <EOL> @ property <EOL> def messages ( self ) : <EOL> return RestfulModelCollection ( Message , self ) <EOL> @ property <EOL> def files ( self ) : <EOL> return RestfulModelCollection ( File , self ) <EOL> @ property <EOL> def drafts ( self ) : <EOL> return RestfulModelCollection ( Draft , self ) <EOL> @ property <EOL> def contacts ( self ) : <EOL> return RestfulModelCollection ( Contact , self ) <EOL> @ property <EOL> def events ( self ) : <EOL> return RestfulModelCollection ( Event , self ) <EOL> @ property <EOL> def calendars ( self ) : <EOL> return RestfulModelCollection ( Calendar , self ) <EOL> def _get_http_session ( self , api_root ) : <EOL> if api_root == '<STR_LIT:a>' : <EOL> return self . admin_session <EOL> else : <EOL> return self . session <EOL> @ nylas_excepted <EOL> def _get_resources ( self , cls , ** filters ) : <EOL> if cls . api_root != '<STR_LIT:a>' : <EOL> url = "<STR_LIT>" . format ( self . api_server , cls . collection_name ) <EOL> else : <EOL> url = "<STR_LIT>" . format ( self . api_server , self . app_id , <EOL> cls . collection_name ) <EOL> url = url_concat ( url , filters ) <EOL> response = self . _get_http_session ( cls . api_root ) . get ( url ) <EOL> results = _validate ( response ) . json ( ) <EOL> return list ( <EOL> filter ( <EOL> lambda x : x is not None , <EOL> map ( lambda x : cls . create ( self , ** x ) , results ) <EOL> ) <EOL> ) <EOL> @ nylas_excepted <EOL> def _get_resource_raw ( self , cls , id , extra = None , <EOL> headers = None , ** filters ) : <EOL> """<STR_LIT>""" <EOL> headers = headers or { } <EOL> headers . update ( self . session . headers ) <EOL> postfix = "<STR_LIT>" . format ( extra ) if extra else '<STR_LIT>' <EOL> if cls . api_root != '<STR_LIT:a>' : <EOL> url = "<STR_LIT>" . format ( self . api_server , cls . collection_name , id , <EOL> postfix ) <EOL> else : <EOL> url = "<STR_LIT>" . format ( self . api_server , self . app_id , <EOL> cls . collection_name , id , postfix ) <EOL> url = url_concat ( url , filters ) <EOL> response = self . _get_http_session ( cls . api_root ) . get ( url , headers = headers ) <EOL> return _validate ( response ) <EOL> def _get_resource ( self , cls , id , ** filters ) : <EOL> response = self . _get_resource_raw ( cls , id , ** filters ) <EOL> result = response . json ( ) <EOL> if isinstance ( result , list ) : <EOL> result = result [ <NUM_LIT:0> ] <EOL> return cls . create ( self , ** result ) <EOL> def _get_resource_data ( self , cls , id , <EOL> extra = None , headers = None , ** filters ) : <EOL> response = self . _get_resource_raw ( cls , id , extra = extra , <EOL> headers = headers , ** filters ) <EOL> return response . content <EOL> @ nylas_excepted <EOL> def _create_resource ( self , cls , data , ** kwargs ) : <EOL> url = "<STR_LIT>" . format ( self . api_server , cls . collection_name ) <EOL> if len ( kwargs . keys ( ) ) > <NUM_LIT:0> : <EOL> url = "<STR_LIT>" . format ( url , urlencode ( kwargs ) ) <EOL> session = self . _get_http_session ( cls . api_root ) <EOL> if cls == File : <EOL> response = session . post ( url , files = data ) <EOL> else : <EOL> data = json . dumps ( data ) <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> headers . update ( self . session . headers ) <EOL> response = session . post ( url , data = data , headers = headers ) <EOL> result = _validate ( response ) . json ( ) <EOL> if cls . collection_name == '<STR_LIT>' : <EOL> return result <EOL> return cls . create ( self , ** result ) <EOL> @ nylas_excepted <EOL> def _create_resources ( self , cls , data ) : <EOL> url = "<STR_LIT>" . format ( self . api_server , cls . collection_name ) <EOL> session = self . _get_http_session ( cls . api_root ) <EOL> if cls == File : <EOL> response = session . post ( url , files = data ) <EOL> else : <EOL> data = json . dumps ( data ) <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> headers . update ( self . session . headers ) <EOL> response = session . post ( url , data = data , headers = headers ) <EOL> results = _validate ( response ) . json ( ) <EOL> return list ( map ( lambda x : cls . create ( self , ** x ) , results ) ) <EOL> @ nylas_excepted <EOL> def _delete_resource ( self , cls , id , data = None , ** kwargs ) : <EOL> name = cls . collection_name <EOL> url = "<STR_LIT>" . format ( self . api_server , name , id ) <EOL> if len ( kwargs . keys ( ) ) > <NUM_LIT:0> : <EOL> url = "<STR_LIT>" . format ( url , urlencode ( kwargs ) ) <EOL> session = self . _get_http_session ( cls . api_root ) <EOL> if data : <EOL> _validate ( session . delete ( url , json = data ) ) <EOL> else : <EOL> _validate ( session . delete ( url ) ) <EOL> @ nylas_excepted <EOL> def _update_resource ( self , cls , id , data , ** kwargs ) : <EOL> name = cls . collection_name <EOL> url = "<STR_LIT>" . format ( self . api_server , name , id ) <EOL> if len ( kwargs . keys ( ) ) > <NUM_LIT:0> : <EOL> url = "<STR_LIT>" . format ( url , urlencode ( kwargs ) ) <EOL> session = self . _get_http_session ( cls . api_root ) <EOL> response = session . put ( url , json = data ) <EOL> result = _validate ( response ) . json ( ) <EOL> return cls . create ( self , ** result ) <EOL> @ nylas_excepted <EOL> def _call_resource_method ( self , cls , id , method_name , data ) : <EOL> """<STR_LIT>""" <EOL> name = cls . collection_name <EOL> if cls . api_root != '<STR_LIT:a>' : <EOL> url = "<STR_LIT>" . format ( self . api_server , name , id , method_name ) <EOL> else : <EOL> url = "<STR_LIT>" . format ( self . api_server , self . app_id , <EOL> cls . collection_name , id , method_name ) <EOL> session = self . _get_http_session ( cls . api_root ) <EOL> response = session . post ( url , json = data ) <EOL> return _validate ( response ) . json ( ) </s>
