<s> import ast <EOL> import copy <EOL> import json <EOL> import uuid <EOL> import requests <EOL> from oslo_config import cfg <EOL> from st2actions . runners import ActionRunner <EOL> from st2common import __version__ as st2_version <EOL> from st2common import log as logging <EOL> from st2common . constants . action import LIVEACTION_STATUS_SUCCEEDED <EOL> from st2common . constants . action import LIVEACTION_STATUS_FAILED <EOL> from st2common . constants . action import LIVEACTION_STATUS_TIMED_OUT <EOL> LOG = logging . getLogger ( __name__ ) <EOL> SUCCESS_STATUS_CODES = [ code for code in range ( <NUM_LIT:200> , <NUM_LIT> ) ] <EOL> RUNNER_ON_BEHALF_USER = '<STR_LIT:user>' <EOL> RUNNER_URL = '<STR_LIT:url>' <EOL> RUNNER_HEADERS = '<STR_LIT>' <EOL> RUNNER_COOKIES = '<STR_LIT>' <EOL> RUNNER_ALLOW_REDIRECTS = '<STR_LIT>' <EOL> RUNNER_HTTP_PROXY = '<STR_LIT>' <EOL> RUNNER_HTTPS_PROXY = '<STR_LIT>' <EOL> RUNNER_VERIFY_SSL_CERT = '<STR_LIT>' <EOL> ACTION_AUTH = '<STR_LIT>' <EOL> ACTION_BODY = '<STR_LIT:body>' <EOL> ACTION_TIMEOUT = '<STR_LIT>' <EOL> ACTION_METHOD = '<STR_LIT>' <EOL> ACTION_QUERY_PARAMS = '<STR_LIT>' <EOL> FILE_NAME = '<STR_LIT>' <EOL> FILE_CONTENT = '<STR_LIT>' <EOL> FILE_CONTENT_TYPE = '<STR_LIT>' <EOL> RESPONSE_BODY_PARSE_FUNCTIONS = { <EOL> '<STR_LIT:application/json>' : json . loads <EOL> } <EOL> def get_runner ( ) : <EOL> return HttpRunner ( str ( uuid . uuid4 ( ) ) ) <EOL> class HttpRunner ( ActionRunner ) : <EOL> def __init__ ( self , runner_id ) : <EOL> super ( HttpRunner , self ) . __init__ ( runner_id = runner_id ) <EOL> self . _on_behalf_user = cfg . CONF . system_user . user <EOL> self . _timeout = <NUM_LIT> <EOL> def pre_run ( self ) : <EOL> LOG . debug ( '<STR_LIT>' , self . liveaction_id ) <EOL> self . _on_behalf_user = self . runner_parameters . get ( RUNNER_ON_BEHALF_USER , <EOL> self . _on_behalf_user ) <EOL> self . _url = self . runner_parameters . get ( RUNNER_URL , None ) <EOL> self . _headers = self . runner_parameters . get ( RUNNER_HEADERS , { } ) <EOL> self . _cookies = self . runner_parameters . get ( RUNNER_COOKIES , None ) <EOL> self . _allow_redirects = self . runner_parameters . get ( RUNNER_ALLOW_REDIRECTS , False ) <EOL> self . _http_proxy = self . runner_parameters . get ( RUNNER_HTTP_PROXY , None ) <EOL> self . _https_proxy = self . runner_parameters . get ( RUNNER_HTTPS_PROXY , None ) <EOL> self . _verify_ssl_cert = self . runner_parameters . get ( RUNNER_VERIFY_SSL_CERT , None ) <EOL> def run ( self , action_parameters ) : <EOL> client = self . _get_http_client ( action_parameters ) <EOL> try : <EOL> result = client . run ( ) <EOL> except requests . exceptions . Timeout as e : <EOL> result = { '<STR_LIT:error>' : str ( e ) } <EOL> status = LIVEACTION_STATUS_TIMED_OUT <EOL> else : <EOL> status = HttpRunner . _get_result_status ( result . get ( '<STR_LIT>' , None ) ) <EOL> return ( status , result , None ) <EOL> def _get_http_client ( self , action_parameters ) : <EOL> body = action_parameters . get ( ACTION_BODY , None ) <EOL> timeout = float ( action_parameters . get ( ACTION_TIMEOUT , self . _timeout ) ) <EOL> method = action_parameters . get ( ACTION_METHOD , None ) <EOL> params = action_parameters . get ( ACTION_QUERY_PARAMS , None ) <EOL> auth = action_parameters . get ( ACTION_AUTH , { } ) <EOL> file_name = action_parameters . get ( FILE_NAME , None ) <EOL> file_content = action_parameters . get ( FILE_CONTENT , None ) <EOL> file_content_type = action_parameters . get ( FILE_CONTENT_TYPE , None ) <EOL> headers = copy . deepcopy ( self . _headers ) if self . _headers else { } <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % ( st2_version ) <EOL> headers [ '<STR_LIT>' ] = self . action_name <EOL> if file_name and file_content : <EOL> files = { } <EOL> if file_content_type : <EOL> value = ( file_content , file_content_type ) <EOL> else : <EOL> value = ( file_content ) <EOL> files [ file_name ] = value <EOL> else : <EOL> files = None <EOL> proxies = { } <EOL> if self . _http_proxy : <EOL> proxies [ '<STR_LIT:http>' ] = self . _http_proxy <EOL> if self . _https_proxy : <EOL> proxies [ '<STR_LIT>' ] = self . _https_proxy <EOL> return HTTPClient ( url = self . _url , method = method , body = body , params = params , <EOL> headers = headers , cookies = self . _cookies , auth = auth , <EOL> timeout = timeout , allow_redirects = self . _allow_redirects , <EOL> proxies = proxies , files = files , verify = self . _verify_ssl_cert ) <EOL> @ staticmethod <EOL> def _get_result_status ( status_code ) : <EOL> return LIVEACTION_STATUS_SUCCEEDED if status_code in SUCCESS_STATUS_CODES else LIVEACTION_STATUS_FAILED <EOL> class HTTPClient ( object ) : <EOL> def __init__ ( self , url = None , method = None , body = '<STR_LIT>' , params = None , headers = None , cookies = None , <EOL> auth = None , timeout = <NUM_LIT> , allow_redirects = False , proxies = None , <EOL> files = None , verify = False ) : <EOL> if url is None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if method is None : <EOL> if files or body : <EOL> method = '<STR_LIT:POST>' <EOL> else : <EOL> method = '<STR_LIT:GET>' <EOL> headers = headers or { } <EOL> normalized_headers = self . _normalize_headers ( headers = headers ) <EOL> if body and '<STR_LIT>' not in normalized_headers : <EOL> headers [ '<STR_LIT>' ] = len ( body ) <EOL> self . url = url <EOL> self . method = method <EOL> self . headers = headers <EOL> self . body = body <EOL> self . params = params <EOL> self . headers = headers <EOL> self . cookies = cookies <EOL> self . auth = auth <EOL> self . timeout = timeout <EOL> self . allow_redirects = allow_redirects <EOL> self . proxies = proxies <EOL> self . files = files <EOL> self . verify = verify <EOL> def run ( self ) : <EOL> results = { } <EOL> resp = None <EOL> json_content = self . _is_json_content ( ) <EOL> try : <EOL> if json_content : <EOL> data = self . _cast_object ( self . body ) <EOL> try : <EOL> data = json . dumps ( data ) <EOL> except ValueError : <EOL> msg = '<STR_LIT>' % ( data ) <EOL> raise ValueError ( msg ) <EOL> else : <EOL> data = self . body <EOL> resp = requests . request ( <EOL> self . method , <EOL> self . url , <EOL> params = self . params , <EOL> data = data , <EOL> headers = self . headers , <EOL> cookies = self . cookies , <EOL> auth = self . auth , <EOL> timeout = self . timeout , <EOL> allow_redirects = self . allow_redirects , <EOL> proxies = self . proxies , <EOL> files = self . files , <EOL> verify = self . verify <EOL> ) <EOL> headers = dict ( resp . headers ) <EOL> body , parsed = self . _parse_response_body ( headers = headers , body = resp . text ) <EOL> results [ '<STR_LIT>' ] = resp . status_code <EOL> results [ '<STR_LIT:body>' ] = body <EOL> results [ '<STR_LIT>' ] = parsed <EOL> results [ '<STR_LIT>' ] = headers <EOL> return results <EOL> except Exception as e : <EOL> LOG . exception ( '<STR_LIT>' , self . url , e ) <EOL> raise <EOL> finally : <EOL> if resp : <EOL> resp . close ( ) <EOL> def _parse_response_body ( self , headers , body ) : <EOL> """<STR_LIT>""" <EOL> body = body or '<STR_LIT>' <EOL> headers = self . _normalize_headers ( headers = headers ) <EOL> content_type = headers . get ( '<STR_LIT>' , None ) <EOL> parsed = False <EOL> if not content_type : <EOL> return ( body , parsed ) <EOL> content_type = content_type . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] <EOL> parse_func = RESPONSE_BODY_PARSE_FUNCTIONS . get ( content_type , None ) <EOL> if not parse_func : <EOL> return ( body , parsed ) <EOL> LOG . debug ( '<STR_LIT>' , content_type ) <EOL> try : <EOL> body = parse_func ( body ) <EOL> except Exception : <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> else : <EOL> parsed = True <EOL> return ( body , parsed ) <EOL> def _normalize_headers ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for key , value in headers . items ( ) : <EOL> result [ key . lower ( ) ] = value <EOL> return result <EOL> def _is_json_content ( self ) : <EOL> normalized = self . _normalize_headers ( self . headers ) <EOL> return normalized . get ( '<STR_LIT>' , None ) == '<STR_LIT:application/json>' <EOL> def _cast_object ( self , value ) : <EOL> if isinstance ( value , str ) or isinstance ( value , unicode ) : <EOL> try : <EOL> return json . loads ( value ) <EOL> except : <EOL> return ast . literal_eval ( value ) <EOL> else : <EOL> return value </s>
