-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
225 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
Contains functions required for interacting with Slack. The main function | ||
used is the ``api_request`` function. This function handles the GET/POST | ||
interactions to Slack. | ||
''' | ||
|
||
# Import Python libs | ||
import json | ||
import logging | ||
import os | ||
|
||
# Import Tornado libs | ||
from tornado import gen | ||
import tornado.httpclient | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
SLACK_WEBHOOK_URL = os.environ.get('SLACK_WEBHOOK_URL') | ||
|
||
|
||
@gen.coroutine | ||
def api_request(method='GET', headers=None, post_data=None): | ||
''' | ||
The main function used to interact with the Slack API. This function | ||
performs the actual requests to Slack when responding to various events. | ||
method | ||
Type of HTTP method. Defaults to ``GET``. | ||
headers | ||
HTTP Headers needed to make the request. Defaults to ``None``. | ||
Note: There are a couple of hard-coded defaults defined here presently. | ||
This is subject to change once configuration files or additional | ||
environment variables are defined. ``Content-Type`` defaults to | ||
``application/json``, as required by Slack. | ||
post_data | ||
The data to pass to the Slack API request. Defaults to ``None``. | ||
This data will change based on the type of request made. For example, | ||
sending text to a Slack channel requires something like the following: | ||
``'{"text": "My comment message."}'``. Other API calls may require | ||
different options and structures. | ||
''' | ||
if headers is None: | ||
headers = {'Content-Type': 'application/json'} | ||
|
||
data = None | ||
if post_data: | ||
data = json.dumps(post_data) | ||
data = data.encode('utf-8') | ||
|
||
http_client = tornado.httpclient.AsyncHTTPClient() | ||
request = tornado.httpclient.HTTPRequest( | ||
SLACK_WEBHOOK_URL, | ||
method=method, | ||
headers=headers, | ||
body=data | ||
) | ||
yield http_client.fetch(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
Tests for the functions in tamarack.slack.py | ||
''' | ||
|
||
# Import Tornado libs | ||
import tornado.testing | ||
|
||
# Import Tamarack libs | ||
import tamarack.slack | ||
|
||
|
||
class TestAPIRequest(tornado.testing.AsyncTestCase): | ||
''' | ||
TestCase for the api_request function | ||
''' | ||
|
||
@tornado.testing.gen_test | ||
def test_request_no_data(self): | ||
''' | ||
Tests that a basic API call is made to Slack with minimal information | ||
''' | ||
slack_url = tamarack.slack.SLACK_WEBHOOK_URL | ||
tamarack.slack.SLACK_WEBHOOK_URL = 'https://slack.com/api/api.test' | ||
ret = yield tamarack.slack.api_request( | ||
method='POST', | ||
post_data={'text': 'foo'} | ||
) | ||
assert ret is None | ||
|
||
# Reset variables for clean tests | ||
tamarack.slack.SLACK_WEBHOOK_URL = slack_url |