<s> from __future__ import absolute_import <EOL> import urllib <EOL> import urllib2 <EOL> import json <EOL> class WePayError ( Exception ) : <EOL> def __init__ ( self , type , message ) : <EOL> Exception . __init__ ( self , message ) <EOL> self . type = type <EOL> class WePayAPI ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , production = True , access_token = None ) : <EOL> """<STR_LIT>""" <EOL> self . access_token = access_token <EOL> if production : <EOL> self . wepay_url = "<STR_LIT>" <EOL> else : <EOL> self . wepay_url = "<STR_LIT>" <EOL> def call ( self , uri , params = { } , token = None ) : <EOL> """<STR_LIT>""" <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> url = self . wepay_url + uri <EOL> if self . access_token or token : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' + ( token if token else self . access_token ) <EOL> params = json . dumps ( params ) <EOL> request = urllib2 . Request ( url , params , headers ) <EOL> try : <EOL> response = urllib2 . urlopen ( request ) . read ( ) <EOL> return json . loads ( response ) <EOL> except urllib2 . HTTPError as e : <EOL> response = json . loads ( e . read ( ) ) <EOL> raise WePayError ( response [ '<STR_LIT:error>' ] , response [ '<STR_LIT>' ] ) <EOL> def get_authorization_url ( self , redirect_uri , client_id , options = { } , scope = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> options [ '<STR_LIT>' ] = scope <EOL> options [ '<STR_LIT>' ] = redirect_uri <EOL> options [ '<STR_LIT>' ] = client_id <EOL> return self . wepay_url + '<STR_LIT>' + urllib . urlencode ( options ) <EOL> def get_token ( self , redirect_uri , client_id , client_secret , code ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : redirect_uri , <EOL> '<STR_LIT>' : client_id , <EOL> '<STR_LIT>' : client_secret , <EOL> '<STR_LIT:code>' : code } <EOL> response = self . call ( '<STR_LIT>' , params ) <EOL> self . access_token = response [ '<STR_LIT>' ] <EOL> return response <EOL> class WePay ( object ) : <EOL> def __init__ ( self , app ) : <EOL> self . consumer_key = app . config . get ( '<STR_LIT>' ) <EOL> self . consumer_secret = app . config . get ( '<STR_LIT>' ) <EOL> self . production = app . config . get ( '<STR_LIT>' , False ) <EOL> self . access_token = app . config . get ( '<STR_LIT>' ) <EOL> self . wepay = WePayAPI ( production = self . production , <EOL> access_token = self . access_token ) <EOL> def call ( self , uri , params = { } , token = None ) : <EOL> return self . wepay . call ( uri , params , token ) <EOL> def get_authorization_urls ( self , redirect_uri , options = { } , scope = '<STR_LIT>' ) : <EOL> client_id = self . consumer_key <EOL> return self . wepay . get_authorization_url ( redirect_uri , <EOL> client_id , <EOL> options = { } , <EOL> scope = scope ) <EOL> def get_token ( self , redirect_uri , code ) : <EOL> client_id = self . consumer_key <EOL> client_secret = self . consumer_secret <EOL> code = code <EOL> return self . wepay . get_token ( redirect_uri , <EOL> client_id , <EOL> client_secret , <EOL> code ) </s>
