Skip to content

Commit

Permalink
Merge ca99a10 into b5c8d4f
Browse files Browse the repository at this point in the history
  • Loading branch information
girlpunk committed Mar 6, 2018
2 parents b5c8d4f + ca99a10 commit e34453d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
72 changes: 70 additions & 2 deletions src/pymonzo/monzo_api.py
Expand Up @@ -186,7 +186,7 @@ def _refresh_oath_token(self):
self._token = token
self._save_token_on_disk()

def _get_response(self, method, endpoint, params=None):
def _get_response(self, method, endpoint, params=None, body=None):
"""
Helper method to handle HTTP requests and catch API errors
Expand All @@ -202,7 +202,10 @@ def _get_response(self, method, endpoint, params=None):
url = urljoin(self.api_url, endpoint)

try:
response = getattr(self._session, method)(url, params=params)
if body:
response = getattr(self._session, method)(url, params=params, body=body)
else:
response = getattr(self._session, method)(url, params=params)

# Check if Monzo API returned HTTP 401, which could mean that the
# token is expired
Expand Down Expand Up @@ -396,3 +399,68 @@ def transaction(self, transaction_id, expand_merchant=False):
)

return MonzoTransaction(data=response.json()['transaction'])

def create_feed_item(
self,
account_id,
title,
image_url,
body=None,
background_color=None,
title_color=None,
body_color=None,
url=None,
type="basic"
):
"""Create an item in an account's activity feed
Official docs: https://monzo.com/docs/#create-feed-item
:param account_id: The account to create a feed item for.
:type account_id: str
:param title: The title to display.
:type title: str
:param image_url: URL of the image to display. This will be displayed as an icon in the
feed, and on the expanded page if no url has been provided.
:type image_url str
:param body: The body text of the feed item.
:type body: str
:param background_color: Hex value for the background colour of the feed item. Defaults
to standard app colours (ie. white background).
:type background_color: str
:param title_color: Hex value for the colour of the title text. Defaults to standard
app colours.
:type title_color: str
:param body_color: Hex value for the colour of the body text. Defaults to standard
app colours.
:type body_color: str
:param type: Type of feed item. Currently only basic is supported.
:type type: str
:param url: A URL to open when the feed item is tapped. If no URL is provided, the app
will display a fallback view based on the title & body.
:type url: str
"""
endpoint = "/feed"

post_data = {
"account_id": account_id,
"type": type,
"params[title]": title,
"params[image_url]": image_url
}

if url:
post_data['url'] = url
if body:
post_data['params[body]'] = body
if background_color:
post_data['params[background_color]'] = background_color
if title_color:
post_data['params[title_color]'] = title_color
if body_color:
post_data['params[body_color]'] = body_color

self._get_response(
method='post', endpoint=endpoint,
body=post_data
)
35 changes: 35 additions & 0 deletions tests/test_monzo_api.py
Expand Up @@ -21,6 +21,7 @@ class TestMonzoAPI:
"""
Test `monzo_api.MonzoAPI` class.
"""

@pytest.fixture(scope='session')
def monzo(self):
"""Helper fixture that returns a `MonzoAPI` instance"""
Expand Down Expand Up @@ -491,3 +492,37 @@ def test_class_transaction_method(self, mocker, mocked_monzo, transaction_api_re
expected_result = MonzoTransaction(transaction_api_response['transaction'])

assert result == expected_result

def test_class_create_feed_item_method(self, mocker, mocked_monzo, accounts_api_response):
"""Test class `create_feed_item` method"""
mocked_get_response = mocker.patch(
'pymonzo.monzo_api.MonzoAPI._get_response',
)
mocked_get_response.return_value.json.return_value = None

title = "title"
image_url = "https://example.com/image_url"
body = "body"
background_color = "background_color"
title_color = "title_color"
body_color = "body_color"
url = "https://example.com/url"

mocked_monzo.create_feed_item(accounts_api_response['accounts'][0]['id'], title, image_url,
body, background_color, title_color, body_color, url)

mocked_get_response.assert_called_once_with(
method='post',
endpoint='/feed',
body={
'account_id': accounts_api_response['accounts'][0]['id'],
'type': 'basic',
'params[title]': title,
'params[image_url]': image_url,
'url': url,
'params[body]': body,
'params[background_color]': background_color,
'params[title_color]': title_color,
'params[body_color]': body_color,
},
)

0 comments on commit e34453d

Please sign in to comment.