Source code for saltext.mattermost.utils.mattermost
"""Library for interacting with Mattermost Incoming Webhooks"""importhttp.clientimportloggingimporturllib.parseimportsalt.utils.httpfromsalt.versionimport__version__log=logging.getLogger(__name__)
[docs]defquery(hook=None,api_url=None,data=None):""" Mattermost object method function to construct and execute on the API URL. :param api_url: The Mattermost API URL :param hook: The Mattermost hook. :param data: The data to be sent for POST method. :return: The json response from the API call or False. """method="POST"ret={"message":"","res":True}base_url=urllib.parse.urljoin(api_url,"/hooks/")url=urllib.parse.urljoin(base_url,str(hook))result=salt.utils.http.query(url,method,data=data,decode=True,status=True)ifresult.get("status",None)==http.client.OK:ret["message"]=f"Message posted {data} correctly"returnretifresult.get("status",None)==http.client.NO_CONTENT:returnTruelog.debug(url)log.debug(data)log.debug(result)if"dict"inresult:_result=result["dict"]if"error"in_result:ret["message"]=result["error"]ret["res"]=Falsereturnretret["message"]="Message not posted"else:ret["message"]="invalid_auth"ret["res"]=Falsereturnret