Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Keep the code object oriented everywhere #604

Closed
@bidoubiwa

Description

@bidoubiwa

in meilisearch/tasks.py we create root level functions. This is the only file in this project where we do not do object-oriented programming.

For example, we call get_task in the get_task method

def get_tasks(
self, parameters: dict[str, Any] | None = None
) -> dict[str, list[dict[str, Any]]]:
"""Get all tasks.
Parameters
----------
parameters (optional):
parameters accepted by the get tasks route: https://docs.meilisearch.com/reference/api/tasks.html#get-all-tasks.
`indexUid` should be set as a List.
Returns
-------
task:
Dictionary with limit, from, next and results containing a list of all enqueued, processing, succeeded or failed tasks.
Raises
------
MeiliSearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
"""
return get_tasks(self.config, parameters=parameters)

This leads to:

  • Possible hard to understand code, where does the get_task come from? Is it an infinite loop?
  • Providing to the function the already initialized parameters of the Client class
  • An inconsistency in pattern with the rest of the code

Suggestion:

Create a TaskHandler class in meilisearch/task.py that initializes the necessary information in its instance:

class TaskHandler():
    def __init__(self, url: str, api_key: str | None = None, timeout: int | None = None) -> None:
        """
        Parameters
        ----------
        url:
            The url to the Meilisearch API (ex: http://localhost:7700)
        api_key:
            The optional API key for Meilisearch
        """
        self.config = Config(url, api_key, timeout=timeout)

        self.http = HttpRequests(self.config)

add get_task and get_tasks as a method of this class. Remove the required Config parameter.

import the TaskHandler in the client file.

from meilisearch.task import TaskHandler

in the client method get_task, call the function the following way:

 def get_tasks(
        self, parameters: dict[str, Any] | None = None
    ) -> dict[str, list[dict[str, Any]]]:
        return self.taskHandler.get_task(uid)

Another solution would be to make all these functions static, in which way they would keep their current function prototype, there would be no need for an initialized and it will be called like this:

return TaskHandler.get_task(client, uid)

What do you think about this @sanders41 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    maintenanceAnything related to maintenance (CI, tests, refactoring...)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions