<s> import flask <EOL> import rauth <EOL> import json <EOL> import os <EOL> from user import User <EOL> from subscriptions import Subscriptions <EOL> OAUTH_ACCESS_TOKEN_URL = "<STR_LIT>" <EOL> OAUTH_AUTHORIZE_URL = "<STR_LIT>" <EOL> OAUTH_REDIRECT_URI = "<STR_LIT>" <EOL> OAUTH_API_BASE_URL = "<STR_LIT>" <EOL> OAUTH_SCOPES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class Application ( object ) : <EOL> def __init__ ( self , <EOL> name = "<STR_LIT>" , <EOL> client_id = None , <EOL> client_secret = None , <EOL> scopes = OAUTH_SCOPES , <EOL> debug = True , <EOL> template_folder = '<STR_LIT>' , <EOL> ** flaskargs ) : <EOL> self . name = name <EOL> self . debug = debug <EOL> self . web = flask . Flask ( self . name , ** flaskargs ) <EOL> self . template_folder = template_folder <EOL> self . logger = self . web . logger <EOL> self . scopes = scopes <EOL> self . subscriptions = Subscriptions ( app = self ) <EOL> self . oauth = rauth . OAuth2Service ( name = self . name , <EOL> client_id = client_id , <EOL> client_secret = client_secret , <EOL> access_token_url = OAUTH_ACCESS_TOKEN_URL , <EOL> authorize_url = OAUTH_AUTHORIZE_URL , <EOL> base_url = OAUTH_API_BASE_URL ) <EOL> @ property <EOL> def oauth_redirect_uri ( self ) : <EOL> return "<STR_LIT>" % ( "<STR_LIT>" if self . secure else "<STR_LIT:http>" , self . host ) <EOL> def _oauth_authorize ( self ) : <EOL> """<STR_LIT>""" <EOL> params = { <EOL> '<STR_LIT>' : "<STR_LIT:U+0020>" . join ( self . scopes ) , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . oauth_redirect_uri , <EOL> '<STR_LIT>' : '<STR_LIT:code>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> url = self . oauth . get_authorize_url ( ** params ) <EOL> return flask . redirect ( url ) <EOL> def _oauth_callback ( self ) : <EOL> """<STR_LIT>""" <EOL> tokens = self . oauth . get_raw_access_token ( data = { <EOL> '<STR_LIT:code>' : flask . request . args . get ( '<STR_LIT:code>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . oauth_redirect_uri , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) . json ( ) <EOL> user = User ( tokens = tokens , app = self ) <EOL> self . subscriptions . init_user ( user ) <EOL> return self . subscriptions . call_endpoint ( "<STR_LIT>" , user ) or "<STR_LIT>" <EOL> def prepare ( self , host = "<STR_LIT:localhost>" , port = <NUM_LIT> , debug = None , secure = False , public = False ) : <EOL> """<STR_LIT>""" <EOL> self . port = port <EOL> self . host = host <EOL> self . secure = secure <EOL> self . public = public <EOL> if port != <NUM_LIT> : <EOL> self . host = "<STR_LIT>" % ( self . host , self . port ) <EOL> self . web . add_url_rule ( '<STR_LIT>' , '<STR_LIT>' , self . _oauth_authorize ) <EOL> self . web . add_url_rule ( '<STR_LIT>' , '<STR_LIT>' , self . _oauth_callback ) <EOL> self . web . debug = debug or self . debug <EOL> def run ( self , ** kwargs ) : <EOL> self . prepare ( ** kwargs ) <EOL> self . web . run ( port = self . port , host = ( "<STR_LIT>" if self . public else "<STR_LIT:127.0.0.1>" ) ) </s>
