<s> import json <EOL> import logging <EOL> from urbanairship import common <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class NamedUser ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , airship , named_user_id = None ) : <EOL> self . _airship = airship <EOL> self . named_user_id = named_user_id <EOL> def associate ( self , channel_id , device_type ) : <EOL> """<STR_LIT>""" <EOL> if self . named_user_id is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> payload = { } <EOL> payload [ '<STR_LIT>' ] = channel_id <EOL> payload [ '<STR_LIT>' ] = device_type <EOL> payload [ '<STR_LIT>' ] = self . named_user_id <EOL> body = json . dumps ( <EOL> { <EOL> '<STR_LIT>' : channel_id , <EOL> '<STR_LIT>' : device_type , <EOL> '<STR_LIT>' : self . named_user_id <EOL> } <EOL> ) . encode ( '<STR_LIT:utf-8>' ) <EOL> response = self . _airship . _request ( <EOL> '<STR_LIT:POST>' , body , common . NAMED_USER_ASSOCIATE_URL , '<STR_LIT:application/json>' , <EOL> version = <NUM_LIT:3> <EOL> ) <EOL> return response <EOL> def disassociate ( self , channel_id , device_type ) : <EOL> """<STR_LIT>""" <EOL> payload = { } <EOL> payload [ '<STR_LIT>' ] = channel_id <EOL> payload [ '<STR_LIT>' ] = device_type <EOL> if self . named_user_id is not None : <EOL> payload [ '<STR_LIT>' ] = self . named_user_id <EOL> body = json . dumps ( payload ) . encode ( '<STR_LIT:utf-8>' ) <EOL> response = self . _airship . _request ( <EOL> '<STR_LIT:POST>' , body , common . NAMED_USER_DISASSOCIATE_URL , <EOL> '<STR_LIT:application/json>' , version = <NUM_LIT:3> <EOL> ) <EOL> return response <EOL> def lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . _airship . _request ( <EOL> '<STR_LIT:GET>' , None , common . NAMED_USER_URL , '<STR_LIT:application/json>' , version = <NUM_LIT:3> , <EOL> params = { '<STR_LIT:id>' : self . named_user_id } <EOL> ) <EOL> return response . json ( ) <EOL> def tag ( self , group , add = None , remove = None , set = None ) : <EOL> """<STR_LIT>""" <EOL> payload = { } <EOL> if self . named_user_id is not None : <EOL> audience = { '<STR_LIT>' : self . named_user_id } <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> payload [ '<STR_LIT>' ] = audience <EOL> if add is not None : <EOL> if set is not None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> payload [ '<STR_LIT>' ] = { group : add } <EOL> if remove is not None : <EOL> if set is not None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> payload [ '<STR_LIT>' ] = { group : remove } <EOL> if set is not None : <EOL> payload [ '<STR_LIT>' ] = { group : set } <EOL> if not add and not remove and not set : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> body = json . dumps ( payload ) . encode ( '<STR_LIT:utf-8>' ) <EOL> response = self . _airship . _request ( <EOL> '<STR_LIT:POST>' , body , common . NAMED_USER_TAG_URL , '<STR_LIT:application/json>' , version = <NUM_LIT:3> <EOL> ) <EOL> return response . json ( ) <EOL> @ classmethod <EOL> def from_payload ( cls , payload ) : <EOL> """<STR_LIT>""" <EOL> for key in payload : <EOL> setattr ( cls , key , payload [ key ] ) <EOL> return cls <EOL> class NamedUserList ( object ) : <EOL> """<STR_LIT>""" <EOL> start_url = None <EOL> next_url = None <EOL> airship = None <EOL> data = None <EOL> def __init__ ( self , airship ) : <EOL> self . _airship = airship <EOL> self . start_url = common . NAMED_USER_URL <EOL> self . next_url = self . start_url <EOL> self . _token_iter = iter ( ( ) ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> try : <EOL> return NamedUser . from_payload ( next ( self . _token_iter ) ) <EOL> except StopIteration : <EOL> self . _fetch_next_page ( ) <EOL> return NamedUser . from_payload ( next ( self . _token_iter ) ) <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __next__ ( ) <EOL> def _fetch_next_page ( self ) : <EOL> if self . next_url : <EOL> self . _load_page ( self . next_url ) <EOL> self . next_url = self . _page . get ( '<STR_LIT>' ) <EOL> def _load_page ( self , url ) : <EOL> response = self . _airship . _request ( <EOL> method = '<STR_LIT:GET>' , <EOL> body = None , <EOL> url = url , <EOL> version = <NUM_LIT:3> <EOL> ) <EOL> self . _page = page = response . json ( ) <EOL> self . _token_iter = iter ( page [ '<STR_LIT>' ] ) </s>
