Skip to content

Commit

Permalink
Added media endpoint (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Ciaran McCormick <ciaran@ciaranmccormick.com>
  • Loading branch information
ciaranmccormick and ciaranmccormick committed Jul 30, 2020
1 parent 812ee6d commit bcc30cb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
33 changes: 32 additions & 1 deletion hootsweet/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
from datetime import datetime
from typing import Any, Dict, List, Tuple

from hootsweet.constants import MessageState, Reviewer
from hootsweet.constants import ALLOWED_MIME_TYPES, MessageState, Reviewer
from hootsweet.exceptions import (
InvalidLanguage,
InvalidTimezone,
MIMETypeNotAllowed,
detect_and_raise_error,
)
from hootsweet.locale import is_valid_language, is_valid_timezone
Expand Down Expand Up @@ -396,3 +397,33 @@ def get_message_review_history(self, message_id: str) -> Dict:
"""
resource = "messages/%s/history" % message_id
return self._make_request(resource)

def create_media_upload_url(self, size_bytes: int, mime_type: str):
"""Creates an Amazon S3 upload URL that can be used to transfer media to
Hootsuite.
Args:
size_bytes (int): Size in bytes of the media file to be uploaded.
mime_type (str): MIME type of the media to be uploaded. Supported
media types are video/mp4, image/gif, image/jpeg, image/jpg, image/png.
"""

resource = "media"
if mime_type not in ALLOWED_MIME_TYPES:
acceptable_mimes = ", ".join(ALLOWED_MIME_TYPES)
raise MIMETypeNotAllowed(
f"{mime_type} is not allow use one of {acceptable_mimes}"
)
data = {"sizeBytes": size_bytes, "mimeType": mime_type}
return self._make_request(resource, method="POST", json=data)

def get_media_upload_status(self, media_id):
"""Retrieves the status of a media upload to Hootsuite.
Args:
media_id (str): The Media ID to retrieve.
"""
resource = "media/%s" % media_id
return self._make_request(resource, method="GET")
8 changes: 8 additions & 0 deletions hootsweet/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from enum import Enum

MP4 = "video/mp4"
GIF = "image/gif"
JPEG = "image/jpeg"
JPG = "image/jpg"
PNG = "image/png"

ALLOWED_MIME_TYPES = [MP4, GIF, JPEG, JPG, PNG]


class Reviewer(Enum):
EXTERNAL = 1
Expand Down
4 changes: 4 additions & 0 deletions hootsweet/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class InvalidTimezone(Exception):
pass


class MIMETypeNotAllowed(Exception):
pass


def detect_and_raise_error(response: Response):
status_code = response.status_code
if status_code == 400:
Expand Down
30 changes: 29 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
default_refresh_cb,
)
from hootsweet.constants import MessageState, Reviewer
from hootsweet.exceptions import InvalidLanguage, InvalidTimezone
from hootsweet.exceptions import InvalidLanguage, InvalidTimezone, MIMETypeNotAllowed
from requests import Response
from requests_oauthlib import OAuth2Session

Expand Down Expand Up @@ -56,6 +56,12 @@
("1234",),
{},
),
(
"get_media_upload_status",
"https://platform.hootsuite.com/v1/media/abc1234",
("abc1234",),
{},
),
]

DELETE_ENDPOINTS = [
Expand Down Expand Up @@ -297,3 +303,25 @@ def test_get_outbound_messages(mock_session):
"GET", expected_url, params=expected_params
)
assert actual == data["data"]


@patch("hootsweet.api.OAuth2Session", spec=OAuth2Session, token=test_token)
def test_create_media_upload_url(mock_session):
response = Mock(status_code=200, spec=Response)
mock_session.return_value.request.return_value = response
mock_session.return_value.token = {"expires_in": 10}
data = {"data": {}}
response.json.return_value = data

hoot_suite = HootSweet("client_id", "client_secret", token=test_token)

with pytest.raises(MIMETypeNotAllowed):
hoot_suite.create_media_upload_url(5000, "image/nnn")

args = (500, "image/png")
expected_url = "https://platform.hootsuite.com/v1/media"
expected_data = {"sizeBytes": args[0], "mimeType": args[1]}
hoot_suite.create_media_upload_url(*args)
mock_session.return_value.request.assert_called_once_with(
"POST", expected_url, json=expected_data
)

0 comments on commit bcc30cb

Please sign in to comment.