<s> """<STR_LIT>""" <EOL> import json <EOL> from . import util <EOL> from . exceptions import * <EOL> class Wallet : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , identifier , password , service_url , second_password = None , api_code = None ) : <EOL> """<STR_LIT>""" <EOL> self . identifier = identifier <EOL> self . password = password <EOL> self . service_url = service_url <EOL> self . second_password = second_password <EOL> self . api_code = api_code <EOL> def send ( self , to , amount , from_address = None , fee = None , note = None ) : <EOL> """<STR_LIT>""" <EOL> recipient = { to : amount } <EOL> return self . send_many ( recipient , from_address , fee , note ) <EOL> def send_many ( self , recipients , from_address = None , fee = None , note = None ) : <EOL> """<STR_LIT>""" <EOL> params = self . build_basic_request ( read_only = False ) <EOL> if len ( recipients ) == <NUM_LIT:1> : <EOL> to_address , amount = recipients . popitem ( ) <EOL> params [ '<STR_LIT:to>' ] = to_address <EOL> params [ '<STR_LIT>' ] = amount <EOL> method = '<STR_LIT>' <EOL> else : <EOL> params [ '<STR_LIT>' ] = json . dumps ( recipients ) <EOL> method = '<STR_LIT>' <EOL> if from_address is not None : <EOL> params [ '<STR_LIT>' ] = from_address <EOL> if fee is not None : <EOL> params [ '<STR_LIT>' ] = fee <EOL> if note is not None : <EOL> params [ '<STR_LIT>' ] = note <EOL> response = util . call_api ( "<STR_LIT>" . format ( self . identifier , method ) , params , <EOL> base_url = self . service_url ) <EOL> json_response = json . loads ( response ) <EOL> self . parse_error ( json_response ) <EOL> payment_response = PaymentResponse ( json_response [ '<STR_LIT:message>' ] , <EOL> json_response [ '<STR_LIT>' ] , <EOL> json_response . get ( '<STR_LIT>' ) ) <EOL> return payment_response <EOL> def get_balance ( self ) : <EOL> """<STR_LIT>""" <EOL> response = util . call_api ( "<STR_LIT>" . format ( self . identifier ) , self . build_basic_request ( ) , <EOL> base_url = self . service_url ) <EOL> json_response = json . loads ( response ) <EOL> self . parse_error ( json_response ) <EOL> return json_response . get ( '<STR_LIT>' ) <EOL> def list_addresses ( self , confirmations = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> params = self . build_basic_request ( ) <EOL> params [ '<STR_LIT>' ] = confirmations <EOL> response = util . call_api ( "<STR_LIT>" . format ( self . identifier ) , params , base_url = self . service_url ) <EOL> json_response = json . loads ( response ) <EOL> self . parse_error ( json_response ) <EOL> addresses = [ ] <EOL> for a in json_response [ '<STR_LIT>' ] : <EOL> address = Address ( a [ '<STR_LIT>' ] , a [ '<STR_LIT:address>' ] , a [ '<STR_LIT:label>' ] , a [ '<STR_LIT>' ] ) <EOL> addresses . append ( address ) <EOL> return addresses <EOL> def get_address ( self , address , confirmations = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> params = self . build_basic_request ( ) <EOL> params [ '<STR_LIT:address>' ] = address <EOL> params [ '<STR_LIT>' ] = confirmations <EOL> response = util . call_api ( "<STR_LIT>" . format ( self . identifier ) , params , <EOL> base_url = self . service_url ) <EOL> json_response = json . loads ( response ) <EOL> self . parse_error ( json_response ) <EOL> return Address ( json_response [ '<STR_LIT>' ] , <EOL> json_response [ '<STR_LIT:address>' ] , <EOL> None , <EOL> json_response [ '<STR_LIT>' ] ) <EOL> def new_address ( self , label = None ) : <EOL> """<STR_LIT>""" <EOL> params = self . build_basic_request ( ) <EOL> if label is not None : <EOL> params [ '<STR_LIT:label>' ] = label <EOL> response = util . call_api ( "<STR_LIT>" . format ( self . identifier ) , params , base_url = self . service_url ) <EOL> json_response = json . loads ( response ) <EOL> self . parse_error ( json_response ) <EOL> return Address ( <NUM_LIT:0> , json_response [ '<STR_LIT:address>' ] , json_response . get ( '<STR_LIT:label>' ) , <NUM_LIT:0> ) <EOL> def archive_address ( self , address ) : <EOL> """<STR_LIT>""" <EOL> params = self . build_basic_request ( ) <EOL> params [ '<STR_LIT:address>' ] = address <EOL> response = util . call_api ( "<STR_LIT>" . format ( self . identifier ) , params , <EOL> base_url = self . service_url ) <EOL> json_response = json . loads ( response ) <EOL> self . parse_error ( json_response ) <EOL> return json_response [ '<STR_LIT>' ] <EOL> def unarchive_address ( self , address ) : <EOL> """<STR_LIT>""" <EOL> params = self . build_basic_request ( ) <EOL> params [ '<STR_LIT:address>' ] = address <EOL> response = util . call_api ( "<STR_LIT>" . format ( self . identifier ) , params , <EOL> base_url = self . service_url ) <EOL> json_response = json . loads ( response ) <EOL> self . parse_error ( json_response ) <EOL> return json_response [ '<STR_LIT>' ] <EOL> def build_basic_request ( self , read_only = True ) : <EOL> params = { '<STR_LIT:password>' : self . password } <EOL> if self . second_password is not None and read_only is False : <EOL> params [ '<STR_LIT>' ] = self . second_password <EOL> if self . api_code is not None : <EOL> params [ '<STR_LIT>' ] = self . api_code <EOL> return params <EOL> @ staticmethod <EOL> def parse_error ( json_response ) : <EOL> error = json_response . get ( '<STR_LIT:error>' ) <EOL> if error is not None : <EOL> raise APIException ( error , <NUM_LIT:0> ) <EOL> class PaymentResponse : <EOL> def __init__ ( self , message , tx_hash , notice ) : <EOL> self . message = message <EOL> self . tx_hash = tx_hash <EOL> self . notice = notice <EOL> class Address : <EOL> def __init__ ( self , balance , address , label , total_received ) : <EOL> self . balance = balance <EOL> self . address = address <EOL> self . label = label <EOL> self . total_received = total_received </s>
