<s> import os <EOL> import json <EOL> import util <EOL> import api <EOL> import tornado . httpclient <EOL> from abc import abstractmethod <EOL> class FilesMockProvider ( api . FilesMockProvider ) : <EOL> PATH = "<STR_LIT>" <EOL> LIST_METHODS = "<STR_LIT>" <EOL> PARSE_ERROR = ( - <NUM_LIT> , "<STR_LIT>" ) <EOL> METHOD_NOT_FOUND = ( - <NUM_LIT> , "<STR_LIT>" ) <EOL> CONTENT_TYPE = ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> def __init__ ( self , api_dir ) : <EOL> self . _api_dir = api_dir <EOL> self . error = <NUM_LIT:0> <EOL> def __call__ ( self , request = None , method_name = "<STR_LIT>" ) : <EOL> methods_dir = os . path . join ( self . _api_dir , self . PATH ) <EOL> if not method_name and request is not None : <EOL> method_name = self . get_method_name ( request . body ) <EOL> if not method_name : <EOL> return self . _error ( self . PARSE_ERROR ) <EOL> content = self . _get_content ( methods_dir , method_name ) <EOL> if content : <EOL> return self . _response ( content ) <EOL> else : <EOL> return self . _error ( self . METHOD_NOT_FOUND ) <EOL> @ abstractmethod <EOL> def _fault ( self , error ) : <EOL> pass <EOL> @ abstractmethod <EOL> def _dump ( self , data , rpcid = <NUM_LIT:1> ) : <EOL> pass <EOL> @ abstractmethod <EOL> def get_method_name ( request_body ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _error ( self , error ) : <EOL> self . error = error [ <NUM_LIT:0> ] <EOL> return self . _response ( self . _fault ( error ) ) <EOL> def _response ( self , content ) : <EOL> return api . Response ( self . _dump ( content ) , [ self . CONTENT_TYPE ] ) <EOL> def _get_content ( self , methods_dir , method_name ) : <EOL> available_methods = self . _list_available_methods ( methods_dir ) <EOL> if method_name == self . LIST_METHODS : <EOL> return available_methods <EOL> elif method_name in available_methods : <EOL> content = util . read_file ( os . path . join ( <EOL> methods_dir , method_name ) ) <EOL> if content is not None : <EOL> try : <EOL> data = json . loads ( content ) <EOL> except ValueError : <EOL> data = content <EOL> return data <EOL> def _list_available_methods ( self , methods_dir ) : <EOL> if os . path . isdir ( methods_dir ) : <EOL> available_methods = os . listdir ( methods_dir ) <EOL> else : <EOL> available_methods = [ ] <EOL> available_methods . append ( self . LIST_METHODS ) <EOL> return available_methods <EOL> class UpstreamServerProvider ( api . UpstreamServerProvider ) : <EOL> def __init__ ( self , upstream_server ) : <EOL> super ( UpstreamServerProvider , self ) . __init__ ( upstream_server ) <EOL> self . _http_client = None <EOL> self . _request_handler_callback = None <EOL> @ property <EOL> def http_client ( self ) : <EOL> if self . _http_client is None : <EOL> self . _http_client = tornado . httpclient . AsyncHTTPClient ( ) <EOL> return self . _http_client <EOL> def __call__ ( self , data , request_handler_callback ) : <EOL> self . _request_handler_callback = request_handler_callback <EOL> self . http_client . fetch ( <EOL> "<STR_LIT>" % ( self . upstream_server , data [ "<STR_LIT>" ] ) , <EOL> callback = self . _on_response , method = data [ "<STR_LIT>" ] , <EOL> body = data [ "<STR_LIT:body>" ] , headers = data [ "<STR_LIT>" ] , follow_redirects = True ) <EOL> def _on_response ( self , response ) : <EOL> self . _request_handler_callback ( <EOL> api . Response ( response . body , response . headers . items ( ) ) ) </s>
