<s> from . errors import maybe_throw <EOL> def add_json_headers ( kw ) : <EOL> if '<STR_LIT>' not in kw : <EOL> kw [ '<STR_LIT>' ] = { } <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' , '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> for header , value in headers . items ( ) : <EOL> kw [ '<STR_LIT>' ] [ header ] = value <EOL> return kw <EOL> class ResourceBase ( object ) : <EOL> def __init__ ( self , url , client , parent , api_path = None , <EOL> branches_api_path = None ) : <EOL> self . _client = client <EOL> self . _parent = parent <EOL> if api_path is None : <EOL> api_path = self . _client . core_api_path <EOL> branches_api_path = self . _client . branches_api_path <EOL> if branches_api_path is None : <EOL> branches_api_path = self . _client . branches_api_path <EOL> if url . startswith ( api_path ) : <EOL> self . _url = url <EOL> self . _branchesurl = url . replace ( api_path , branches_api_path ) <EOL> elif url . startswith ( self . _client . core_api_path ) : <EOL> self . _url = url . replace ( self . _client . core_api_path , api_path ) <EOL> self . _branchesurl = url . replace ( self . _client . core_api_path , <EOL> branches_api_path ) <EOL> else : <EOL> if url . startswith ( '<STR_LIT:/>' ) : <EOL> url = url [ <NUM_LIT:1> : ] <EOL> self . _url = '<STR_LIT>' . format ( api_path , url ) <EOL> self . _branchesurl = '<STR_LIT>' . format ( branches_api_path , url ) <EOL> def url ( self , resource_url = "<STR_LIT>" , is_branches = False ) : <EOL> if resource_url and not resource_url . startswith ( "<STR_LIT:/>" ) : <EOL> resource_url = "<STR_LIT:/>" + resource_url <EOL> if is_branches : <EOL> if self . _url . endswith ( "<STR_LIT:/>" ) : <EOL> url = self . _branchesurl [ : - <NUM_LIT:1> ] <EOL> else : <EOL> url = self . _branchesurl <EOL> else : <EOL> if self . _url . endswith ( "<STR_LIT:/>" ) : <EOL> url = self . _url [ : - <NUM_LIT:1> ] <EOL> else : <EOL> url = self . _url <EOL> return url + resource_url <EOL> def paginate ( self , resource_url , params = None , values_key = '<STR_LIT>' , <EOL> is_branches = False ) : <EOL> url = self . url ( resource_url , is_branches ) <EOL> more = True <EOL> start = None <EOL> while more : <EOL> kw = { } <EOL> if params : <EOL> kw [ '<STR_LIT>' ] = params <EOL> if start is not None : <EOL> kw . setdefault ( '<STR_LIT>' , { } ) <EOL> kw [ '<STR_LIT>' ] [ '<STR_LIT:start>' ] = start <EOL> response = self . _client . get ( url , ** kw ) <EOL> maybe_throw ( response ) <EOL> data = response . json ( ) <EOL> if not values_key in data : <EOL> return <EOL> for item in data [ values_key ] : <EOL> yield item <EOL> if data [ '<STR_LIT>' ] : <EOL> more = False <EOL> else : <EOL> more = True <EOL> start = data [ '<STR_LIT>' ] <EOL> class IterableResource ( object ) : <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . all ( ) <EOL> def all ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . paginate ( "<STR_LIT>" ) <EOL> def list ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . all ( ) ) <EOL> class FilteredIterableResource ( IterableResource ) : <EOL> def all ( self , filter = None ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if filter : <EOL> params [ '<STR_LIT>' ] = filter <EOL> return self . paginate ( "<STR_LIT>" , params ) <EOL> def list ( self , filter = None ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . all ( filter ) ) <EOL> class Nested ( object ) : <EOL> def __init__ ( self , cls , relative_path = '<STR_LIT>' ) : <EOL> if relative_path is None : <EOL> self . relative_path = '<STR_LIT>' <EOL> elif not relative_path : <EOL> self . relative_path = "<STR_LIT>" % cls . __name__ . lower ( ) <EOL> else : <EOL> if not relative_path . startswith ( "<STR_LIT:/>" ) : <EOL> relative_path = "<STR_LIT:/>" + relative_path <EOL> self . relative_path = relative_path <EOL> self . cls = cls <EOL> def __get__ ( self , instance , kind ) : <EOL> parent_url = instance . _url <EOL> if parent_url . endswith ( "<STR_LIT:/>" ) : <EOL> parent_url = parent_url [ : - <NUM_LIT:1> ] <EOL> url = parent_url + self . relative_path <EOL> return self . cls ( url = url , client = instance . _client , parent = instance ) </s>
