Skip to content

Commit

Permalink
Fix server response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
leshchenko1979 committed Mar 18, 2024
1 parent 34592f1 commit 38ca64f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
17 changes: 9 additions & 8 deletions fast_bitrix24/server_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ def extract_results(self) -> Union[Dict, List[Dict]]:
if not self.is_batch():
return self.extract_from_single_response(self.result)

if self.get_by_ID:
return self.extract_from_single_response(self.result["result"])
else:
if not self.get_by_ID:
return self.extract_from_batch_response(self.result["result"])

extracted = self.extract_from_single_response(self.result["result"])

return extracted[0] if isinstance(extracted, list) else extracted

def raise_for_errors(self):
errors = self.extract_errors()
if errors:
Expand All @@ -67,17 +69,16 @@ def extract_errors(self):
def is_batch(self) -> bool:
return isinstance(self.result, dict) and "result" in self.result

@staticmethod
def extract_from_single_response(result: dict):
def extract_from_single_response(self, result: dict):
# если результат вызова содержит только словарь {ключ: список},
# то вернуть этот список.
# См. https://github.com/leshchenko1979/fast_bitrix24/issues/132

# метод `crm.stagehistory.list` возвращает dict["items", list] --
# разворачиваем его в список

if ServerResponseParser.is_nested(result):
contents = ServerResponseParser.extract_from_nested(result)
if self.is_nested(result):
contents = self.extract_from_nested(result)
if isinstance(contents, list):
return contents

Expand All @@ -88,7 +89,7 @@ def is_nested(result) -> bool:
return isinstance(result, dict) and len(result) == 1

@staticmethod
def extract_from_nested(result):
def extract_from_nested(result: dict):
return next(iter(result.values()))


Expand Down
12 changes: 5 additions & 7 deletions tests/test_server_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,14 @@ def test_crm_dealcategory_stage_list(bx_dummy):
assert isinstance(results, dict)


@pytest.mark.skip(
reason="Ошибка возникает только при заворачивании в батч более одного элемента, "
"но это должно вылечиться тем, что мы перестанем такое заворачивать в батч"
)
def test_crm_dealcategory_stage_list_2(bx_dummy):
from tests.real_responses.crm_dealcategory_stage_list_2 import response
def test_crm_dealcategory_stage_list_batch_of_one(bx_dummy):
from tests.real_responses.crm_dealcategory_stage_list_batch_of_one import response

bx_dummy.srh = MockSRH(response)
results = bx_dummy.get_by_ID("crm.dealcategory.stage.list", [0])
assert isinstance(results, dict)

# should return a list of dicts
assert isinstance(results, list) and isinstance(results[0], dict)


def test_catalog_document_element_list(bx_dummy):
Expand Down

0 comments on commit 38ca64f

Please sign in to comment.