<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import json <EOL> import logging <EOL> from salt . ext . six . moves . urllib . parse import urljoin as _urljoin <EOL> from salt . ext . six . moves . urllib . parse import urlencode as _urlencode <EOL> from salt . ext . six . moves import range <EOL> import salt . ext . six . moves . http_client <EOL> import salt . utils . http <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return __virtualname__ <EOL> def _query ( function , <EOL> api_url = None , <EOL> api_key = None , <EOL> api_version = None , <EOL> room_id = None , <EOL> method = '<STR_LIT:GET>' , <EOL> data = None ) : <EOL> '''<STR_LIT>''' <EOL> headers = { } <EOL> query_params = { } <EOL> if not api_url : <EOL> try : <EOL> options = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> api_url = options . get ( '<STR_LIT>' ) <EOL> except ( NameError , KeyError , AttributeError ) : <EOL> pass <EOL> if not api_key or not api_version : <EOL> try : <EOL> options = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> if not api_key : <EOL> api_key = options . get ( '<STR_LIT>' ) <EOL> if not api_version : <EOL> api_version = options . get ( '<STR_LIT>' ) <EOL> except ( NameError , KeyError , AttributeError ) : <EOL> log . error ( "<STR_LIT>" ) <EOL> return False <EOL> if room_id : <EOL> room_id = '<STR_LIT>' . format ( str ( room_id ) ) <EOL> else : <EOL> room_id = '<STR_LIT>' <EOL> hipchat_functions = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:message>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:status>' , <EOL> } , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT:user>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:message>' : { <EOL> '<STR_LIT>' : room_id , <EOL> '<STR_LIT>' : None , <EOL> } , <EOL> } , <EOL> } <EOL> use_api_url = '<STR_LIT>' <EOL> if api_url : <EOL> use_api_url = api_url <EOL> base_url = _urljoin ( use_api_url , api_version + '<STR_LIT:/>' ) <EOL> path = hipchat_functions . get ( api_version ) . get ( function ) . get ( '<STR_LIT>' ) <EOL> url = _urljoin ( base_url , path , False ) <EOL> if api_version == '<STR_LIT>' : <EOL> query_params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> query_params [ '<STR_LIT>' ] = api_key <EOL> if method == '<STR_LIT:POST>' : <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> if data : <EOL> if data . get ( '<STR_LIT>' , None ) : <EOL> data [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> data = _urlencode ( data ) <EOL> elif api_version == '<STR_LIT>' : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' . format ( api_key ) <EOL> if data : <EOL> data = json . dumps ( data ) <EOL> if method == '<STR_LIT:POST>' : <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> else : <EOL> log . error ( '<STR_LIT>' ) <EOL> return False <EOL> result = salt . utils . http . query ( <EOL> url , <EOL> method , <EOL> params = query_params , <EOL> data = data , <EOL> decode = True , <EOL> status = True , <EOL> header_dict = headers , <EOL> opts = __opts__ , <EOL> ) <EOL> if result . get ( '<STR_LIT:status>' , None ) == salt . ext . six . moves . http_client . OK : <EOL> response = hipchat_functions . get ( api_version ) . get ( function ) . get ( '<STR_LIT>' ) <EOL> return result . get ( '<STR_LIT>' , { } ) . get ( response , None ) <EOL> elif result . get ( '<STR_LIT:status>' , None ) == salt . ext . six . moves . http_client . NO_CONTENT : <EOL> return False <EOL> else : <EOL> log . debug ( url ) <EOL> log . debug ( query_params ) <EOL> log . debug ( data ) <EOL> log . debug ( result ) <EOL> if result . get ( '<STR_LIT:error>' ) : <EOL> log . error ( result ) <EOL> return False <EOL> def list_rooms ( api_url = None , <EOL> api_key = None , <EOL> api_version = None ) : <EOL> '''<STR_LIT>''' <EOL> foo = _query ( function = '<STR_LIT>' , <EOL> api_url = api_url , <EOL> api_key = api_key , <EOL> api_version = api_version ) <EOL> log . debug ( '<STR_LIT>' . format ( foo ) ) <EOL> return foo <EOL> def list_users ( api_url = None , <EOL> api_key = None , <EOL> api_version = None ) : <EOL> '''<STR_LIT>''' <EOL> return _query ( function = '<STR_LIT>' , <EOL> api_url = api_url , <EOL> api_key = api_key , <EOL> api_version = api_version ) <EOL> def find_room ( name , <EOL> api_url = None , <EOL> api_key = None , <EOL> api_version = None ) : <EOL> '''<STR_LIT>''' <EOL> rooms = list_rooms ( api_url = api_url , <EOL> api_key = api_key , <EOL> api_version = api_version ) <EOL> if rooms : <EOL> for x in range ( <NUM_LIT:0> , len ( rooms ) ) : <EOL> if rooms [ x ] [ '<STR_LIT:name>' ] == name : <EOL> return rooms [ x ] <EOL> return False <EOL> def find_user ( name , <EOL> api_url = None , <EOL> api_key = None , <EOL> api_version = None ) : <EOL> '''<STR_LIT>''' <EOL> users = list_users ( api_url = api_url , <EOL> api_key = api_key , <EOL> api_version = api_version ) <EOL> if users : <EOL> for x in range ( <NUM_LIT:0> , len ( users ) ) : <EOL> if users [ x ] [ '<STR_LIT:name>' ] == name : <EOL> return users [ x ] <EOL> return False <EOL> def send_message ( room_id , <EOL> message , <EOL> from_name , <EOL> api_url = None , <EOL> api_key = None , <EOL> api_version = None , <EOL> color = '<STR_LIT>' , <EOL> notify = False ) : <EOL> '''<STR_LIT>''' <EOL> parameters = dict ( ) <EOL> parameters [ '<STR_LIT>' ] = room_id <EOL> parameters [ '<STR_LIT>' ] = from_name [ : <NUM_LIT:15> ] <EOL> parameters [ '<STR_LIT:message>' ] = message [ : <NUM_LIT> ] <EOL> parameters [ '<STR_LIT>' ] = '<STR_LIT:text>' <EOL> parameters [ '<STR_LIT>' ] = color <EOL> parameters [ '<STR_LIT>' ] = notify <EOL> result = _query ( function = '<STR_LIT:message>' , <EOL> api_url = api_url , <EOL> api_key = api_key , <EOL> api_version = api_version , <EOL> room_id = room_id , <EOL> method = '<STR_LIT:POST>' , <EOL> data = parameters ) <EOL> if result : <EOL> return True <EOL> else : <EOL> return False </s>
