flowdock.py
83 lines
| 2.6 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
import logging | ||||
import requests | ||||
from requests.auth import HTTPBasicAuth | ||||
import simplejson as json | ||||
r153 | from appenlight.models.integrations import IntegrationBase, IntegrationException | |||
r0 | ||||
_ = str | ||||
log = logging.getLogger(__name__) | ||||
class NotFoundException(Exception): | ||||
pass | ||||
class FlowdockIntegration(IntegrationBase): | ||||
r153 | __mapper_args__ = {"polymorphic_identity": "flowdock"} | |||
r0 | front_visible = False | |||
as_alert_channel = True | ||||
supports_report_alerting = True | ||||
action_notification = True | ||||
r153 | integration_action = "Message via Flowdock" | |||
r0 | ||||
@classmethod | ||||
def create_client(cls, api_token): | ||||
client = FlowdockClient(api_token) | ||||
return client | ||||
class FlowdockClient(object): | ||||
def __init__(self, api_token): | ||||
r153 | self.auth = HTTPBasicAuth(api_token, "") | |||
r0 | self.api_token = api_token | |||
r153 | self.api_url = "https://api.flowdock.com/v1/messages" | |||
r0 | ||||
r153 | def make_request(self, url, method="get", data=None): | |||
r0 | headers = { | |||
r153 | "Content-Type": "application/json", | |||
"User-Agent": "appenlight-flowdock", | ||||
r0 | } | |||
try: | ||||
if data: | ||||
data = json.dumps(data) | ||||
r153 | resp = getattr(requests, method)( | |||
url, data=data, headers=headers, timeout=10 | ||||
) | ||||
r0 | except Exception as e: | |||
raise IntegrationException( | ||||
r153 | _("Error communicating with Flowdock: %s") % (e,) | |||
) | ||||
r0 | if resp.status_code > 299: | |||
raise IntegrationException(resp.text) | ||||
return resp | ||||
def send_to_chat(self, payload): | ||||
r153 | url = "%(api_url)s/chat/%(api_token)s" % { | |||
"api_url": self.api_url, | ||||
"api_token": self.api_token, | ||||
} | ||||
return self.make_request(url, method="post", data=payload).json() | ||||
r0 | ||||
def send_to_inbox(self, payload): | ||||
r153 | f_args = {"api_url": self.api_url, "api_token": self.api_token} | |||
url = "%(api_url)s/team_inbox/%(api_token)s" % f_args | ||||
return self.make_request(url, method="post", data=payload).json() | ||||