Skip to content

Commit 4c6b360

Browse files
authored
Add livechat_get_appearance and livechat_set_appearance methods and raw body POST support (#376)
1 parent b28a3c4 commit 4c6b360

3 files changed

Lines changed: 69 additions & 18 deletions

File tree

rocketchat_API/APISections/base.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,37 @@ def call_api_get(self, method, api_path=None, **kwargs):
200200
)
201201
)
202202

203-
def call_api_post(self, method, files=None, use_json=None, **kwargs):
204-
reduced_args = self.__reduce_kwargs(kwargs)
205-
# Since pass is a reserved word in Python it has to be injected on the request dict
206-
# Some methods use pass (users.register) and others password (users.create)
207-
if "password" in reduced_args and method != "users.create":
208-
reduced_args["pass"] = reduced_args["password"]
209-
del reduced_args["password"]
210-
if use_json is None:
211-
# see https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests
212-
# > The json parameter is ignored if either data or files is passed.
213-
# If files are sent, json should not be used
214-
use_json = files is None
215-
if use_json:
203+
def call_api_post(self, method, body=None, files=None, use_json=None, **kwargs):
204+
"""Send a POST request to the API.
205+
206+
There are two modes of operation:
207+
208+
1. **Raw body** — pass ``body`` directly. The value is serialized as-is
209+
via ``json=body``, which supports any JSON-serializable structure
210+
(lists, dicts, etc.). ``kwargs`` are ignored in this mode.
211+
212+
2. **Keyword arguments** (default) — individual kwargs are collected
213+
into a dict and sent as the request payload. By default the payload
214+
is sent as JSON (``json=``), but when ``files`` are provided it
215+
falls back to form-encoded ``data=`` because requests ignores the
216+
``json`` parameter when ``files`` is set. You can override this
217+
with ``use_json=True/False``.
218+
219+
:param method: API method path, appended to ``self.api_path``.
220+
:param body: A raw JSON-serializable payload (e.g. a list).
221+
When provided, ``kwargs``, ``files``, and ``use_json``
222+
are ignored.
223+
:param files: Files to upload (passed to ``requests``).
224+
:param use_json: Force JSON (True) or form-encoded (False) encoding.
225+
Defaults to JSON when no files are attached.
226+
"""
227+
url = self.server_url + self.api_path + method
228+
229+
if body is not None:
216230
return json_or_error(
217231
self.session.post(
218-
self.server_url + self.api_path + method,
219-
json=reduced_args,
220-
files=files,
232+
url,
233+
json=body,
221234
headers=self.headers,
222235
verify=self.ssl_verify,
223236
cert=self.cert,
@@ -226,10 +239,21 @@ def call_api_post(self, method, files=None, use_json=None, **kwargs):
226239
)
227240
)
228241

242+
reduced_args = self.__reduce_kwargs(kwargs)
243+
244+
# "pass" is a Python reserved word, but some endpoints (e.g.
245+
# users.register) expect it instead of "password".
246+
if "password" in reduced_args and method != "users.create":
247+
reduced_args["pass"] = reduced_args.pop("password")
248+
249+
if use_json is None:
250+
use_json = files is None
251+
229252
return json_or_error(
230253
self.session.post(
231-
self.server_url + self.api_path + method,
232-
data=reduced_args,
254+
url,
255+
json=reduced_args if use_json else None,
256+
data=None if use_json else reduced_args,
233257
files=files,
234258
headers=self.headers,
235259
verify=self.ssl_verify,

rocketchat_API/APISections/livechat.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ def livechat_inquiries_get_one(self, room_id, **kwargs):
7070
return self.call_api_get(
7171
"livechat/inquiries.getOne", roomId=room_id, kwargs=kwargs
7272
)
73+
74+
def livechat_get_appearance(self, **kwargs):
75+
"""Get the settings about the widget appearance. Permission required: view-livechat-manager"""
76+
return self.call_api_get("livechat/appearance", kwargs=kwargs)
77+
78+
def livechat_set_appearance(self, settings):
79+
"""Update the livechat widget appearance settings. Permission required: view-livechat-manager
80+
81+
:param settings: list of dicts with '_id' and 'value' keys,
82+
e.g. [{"_id": "Livechat_title", "value": "Support"}]
83+
"""
84+
return self.call_api_post("livechat/appearance", body=settings)

tests/test_livechat.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,18 @@ def test_livechat_get_inquiries(logged_rocket, livechat_inquiry):
125125
def test_livechat_get_inquiries_non_existent_room(logged_rocket):
126126
inquiry_not_found = logged_rocket.livechat_inquiries_get_one(room_id="nonexistent")
127127
assert inquiry_not_found.get("inquiry") is None or "success" in inquiry_not_found
128+
129+
130+
def test_livechat_get_appearance(logged_rocket):
131+
result = logged_rocket.livechat_get_appearance()
132+
assert "appearance" in result
133+
134+
135+
def test_livechat_set_appearance(logged_rocket):
136+
appearance = logged_rocket.livechat_get_appearance().get("appearance", [])
137+
assert len(appearance) > 0, "Expected at least one appearance setting"
138+
139+
setting = appearance[0]
140+
logged_rocket.livechat_set_appearance(
141+
[{"_id": setting["_id"], "value": setting["value"]}]
142+
)

0 commit comments

Comments
 (0)