Skip to content

Commit

Permalink
Fix the asset.setAsset method
Browse files Browse the repository at this point in the history
starting in version 5.1, the behavior of the endpoint changed. In order to handle the difference, the wrapper needs to act differently for different versions
  • Loading branch information
jadolg committed Oct 30, 2022
1 parent 36c1af1 commit 99c8e58
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.28.1
packaging~=21.3
13 changes: 11 additions & 2 deletions rocketchat_API/APISections/assets.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import mimetypes

from packaging import version

from rocketchat_API.APISections.base import RocketChatBase


class RocketChatAssets(RocketChatBase):
def assets_set_asset(self, asset_name, file, **kwargs):
"""Set an asset image by name."""
server_info = self.info().json()
content_type = mimetypes.MimeTypes().guess_type(file)

file_name = asset_name
if version.parse(server_info.get("info").get("version")) >= version.parse("5.1"):
file_name = "asset"

files = {
asset_name: (file, open(file, "rb"), content_type[0], {"Expires": "0"}),
file_name: (file, open(file, "rb"), content_type[0], {"Expires": "0"}),
}

return self.call_api_post(
"assets.setAsset", kwargs=kwargs, use_json=False, files=files
"assets.setAsset", kwargs=kwargs, assetName=asset_name, use_json=False, files=files
)

def assets_unset_asset(self, asset_name):
Expand Down
4 changes: 4 additions & 0 deletions rocketchat_API/APISections/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,7 @@ def login(self, user, password):
def logout(self, **kwargs):
"""Invalidate your REST rocketchat_API authentication token."""
return self.call_api_post("logout", kwargs=kwargs)

def info(self, **kwargs):
"""Information about the Rocket.Chat server."""
return self.call_api_get("info", api_path="/api/", kwargs=kwargs)
4 changes: 0 additions & 4 deletions rocketchat_API/APISections/miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

class RocketChatMiscellaneous(RocketChatBase):
# Miscellaneous information
def info(self, **kwargs):
"""Information about the Rocket.Chat server."""
return self.call_api_get("info", api_path="/api/", kwargs=kwargs)

def directory(self, query, **kwargs):
"""Search by users or channels on all server."""
if isinstance(query, dict):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
description="Python API wrapper for Rocket.Chat",
long_description=open("README.md", "r").read(),
long_description_content_type="text/markdown",
install_requires=("requests",),
install_requires=("requests", "packaging"),
)

0 comments on commit 99c8e58

Please sign in to comment.