Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 42 additions & 18 deletions rocketchat_API/APISections/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,37 @@ def call_api_get(self, method, api_path=None, **kwargs):
)
)

def call_api_post(self, method, files=None, use_json=None, **kwargs):
reduced_args = self.__reduce_kwargs(kwargs)
# Since pass is a reserved word in Python it has to be injected on the request dict
# Some methods use pass (users.register) and others password (users.create)
if "password" in reduced_args and method != "users.create":
reduced_args["pass"] = reduced_args["password"]
del reduced_args["password"]
if use_json is None:
# see https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests
# > The json parameter is ignored if either data or files is passed.
# If files are sent, json should not be used
use_json = files is None
if use_json:
def call_api_post(self, method, body=None, files=None, use_json=None, **kwargs):
"""Send a POST request to the API.

There are two modes of operation:

1. **Raw body** — pass ``body`` directly. The value is serialized as-is
via ``json=body``, which supports any JSON-serializable structure
(lists, dicts, etc.). ``kwargs`` are ignored in this mode.

2. **Keyword arguments** (default) — individual kwargs are collected
into a dict and sent as the request payload. By default the payload
is sent as JSON (``json=``), but when ``files`` are provided it
falls back to form-encoded ``data=`` because requests ignores the
``json`` parameter when ``files`` is set. You can override this
with ``use_json=True/False``.

:param method: API method path, appended to ``self.api_path``.
:param body: A raw JSON-serializable payload (e.g. a list).
When provided, ``kwargs``, ``files``, and ``use_json``
are ignored.
:param files: Files to upload (passed to ``requests``).
:param use_json: Force JSON (True) or form-encoded (False) encoding.
Defaults to JSON when no files are attached.
"""
url = self.server_url + self.api_path + method

if body is not None:
return json_or_error(
self.session.post(
self.server_url + self.api_path + method,
json=reduced_args,
files=files,
url,
json=body,
headers=self.headers,
verify=self.ssl_verify,
cert=self.cert,
Expand All @@ -226,10 +239,21 @@ def call_api_post(self, method, files=None, use_json=None, **kwargs):
)
)

reduced_args = self.__reduce_kwargs(kwargs)

# "pass" is a Python reserved word, but some endpoints (e.g.
# users.register) expect it instead of "password".
if "password" in reduced_args and method != "users.create":
reduced_args["pass"] = reduced_args.pop("password")

if use_json is None:
use_json = files is None

return json_or_error(
self.session.post(
self.server_url + self.api_path + method,
data=reduced_args,
url,
json=reduced_args if use_json else None,
data=None if use_json else reduced_args,
files=files,
headers=self.headers,
verify=self.ssl_verify,
Expand Down
12 changes: 12 additions & 0 deletions rocketchat_API/APISections/livechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ def livechat_inquiries_get_one(self, room_id, **kwargs):
return self.call_api_get(
"livechat/inquiries.getOne", roomId=room_id, kwargs=kwargs
)

def livechat_get_appearance(self, **kwargs):
"""Get the settings about the widget appearance. Permission required: view-livechat-manager"""
return self.call_api_get("livechat/appearance", kwargs=kwargs)

def livechat_set_appearance(self, settings):
"""Update the livechat widget appearance settings. Permission required: view-livechat-manager

:param settings: list of dicts with '_id' and 'value' keys,
e.g. [{"_id": "Livechat_title", "value": "Support"}]
"""
return self.call_api_post("livechat/appearance", body=settings)
15 changes: 15 additions & 0 deletions tests/test_livechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,18 @@ def test_livechat_get_inquiries(logged_rocket, livechat_inquiry):
def test_livechat_get_inquiries_non_existent_room(logged_rocket):
inquiry_not_found = logged_rocket.livechat_inquiries_get_one(room_id="nonexistent")
assert inquiry_not_found.get("inquiry") is None or "success" in inquiry_not_found


def test_livechat_get_appearance(logged_rocket):
result = logged_rocket.livechat_get_appearance()
assert "appearance" in result


def test_livechat_set_appearance(logged_rocket):
appearance = logged_rocket.livechat_get_appearance().get("appearance", [])
assert len(appearance) > 0, "Expected at least one appearance setting"

setting = appearance[0]
logged_rocket.livechat_set_appearance(
[{"_id": setting["_id"], "value": setting["value"]}]
)
Loading