Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Исправить подсчет времени выполнения запросов в батче #230

Merged
merged 4 commits into from
Mar 23, 2024
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
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#### Параметры
* `method: str` - метод REST API для запроса к серверу.

* `params: dict` - параметры для передачи методу. Используется именно тот формат, который указан в документации к REST API Битрикс24. `get_all()` не поддерживает параметры `start`, `limit` и `order`.
* `params: dict` - параметры для передачи методу. Используется именно тот формат, который указан в документации к REST API Битрикс24. `get_all()` не поддерживает параметры `start` и `order`.

Возвращает полный список сущностей, имеющихся на сервере, согласно заданным методу и параметрам.

Expand Down
16 changes: 13 additions & 3 deletions fast_bitrix24/srh.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@

logger.debug("Response: %s", json)

request_run_time = json["time"]["operating"]
self.method_throttlers[method].add_request_record(request_run_time)
self.leaky_bucket_throttler.add_request_record()
self.add_throttler_records(method, params, json)

return json

Expand All @@ -175,6 +173,18 @@

raise

def add_throttler_records(self, method, params: dict, json: dict):
leshchenko1979 marked this conversation as resolved.
Show resolved Hide resolved
if "result_time" in json:
for cmd_name, cmd_url in params["cmd"].items():
item_method = cmd_url.split("?")[0]
item_time = json["result_time"][cmd_name]
self.method_throttlers[item_method].add_request_record(item_time)

Check warning on line 181 in fast_bitrix24/srh.py

View check run for this annotation

Codecov / codecov/patch

fast_bitrix24/srh.py#L178-L181

Added lines #L178 - L181 were not covered by tests
else:
request_run_time = json["time"]["operating"]
self.method_throttlers[method].add_request_record(request_run_time)

self.leaky_bucket_throttler.add_request_record()
Comment on lines +176 to +186
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (edge_case_not_handled): Ensure error handling in 'add_throttler_records' method.

Given the critical nature of throttling in API interactions, consider adding error handling within 'add_throttler_records' to manage unexpected 'json' structures or missing keys.


def success(self):
"""Увеличить счетчик удачных попыток."""

Expand Down
23 changes: 21 additions & 2 deletions fast_bitrix24/user_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
class GetAllUserRequest(UserRequestAbstract):
@icontract.require(
lambda self: not self.st_params
or set(self.st_params.keys()).isdisjoint({"START", "LIMIT", "ORDER"}),
"get_all() doesn't support parameters " "'start', 'limit' or 'order'",
or set(self.st_params.keys()).isdisjoint({"START", "ORDER"}),
"get_all() doesn't support parameters 'start' or 'order'",
)
@icontract.require(
lambda self: not self.st_method.startswith("tasks.elapseditem."),
Expand All @@ -131,6 +131,25 @@
stacklevel=get_warning_stack_level(TOP_MOST_LIBRARY_MODULES),
)

if self.st_params and "LIMIT" in self.st_params:
warnings.warn(

Check warning on line 135 in fast_bitrix24/user_request.py

View check run for this annotation

Codecov / codecov/patch

fast_bitrix24/user_request.py#L135

Added line #L135 was not covered by tests
"Bitrix servers don't seem to support the 'LIMIT' parameter.",
UserWarning,
stacklevel=get_warning_stack_level(TOP_MOST_LIBRARY_MODULES),
)

if (
self.st_params
and "SELECT" in self.st_params
and "*" in self.st_params["SELECT"]
):
warnings.warn(

Check warning on line 146 in fast_bitrix24/user_request.py

View check run for this annotation

Codecov / codecov/patch

fast_bitrix24/user_request.py#L146

Added line #L146 was not covered by tests
"You are selecting all fields. Beware that this is time-consuming and "
"may lead to penalties from the Bitrix server.",
UserWarning,
stacklevel=get_warning_stack_level(TOP_MOST_LIBRARY_MODULES),
)

return True

async def run(self):
Expand Down
Loading