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

Skip to content

Keep the code object oriented everywhere #604

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

Closed
bidoubiwa opened this issue Nov 21, 2022 · 5 comments · Fixed by #662
Closed

Keep the code object oriented everywhere #604

bidoubiwa opened this issue Nov 21, 2022 · 5 comments · Fixed by #662
Labels
maintenance Anything related to maintenance (CI, tests, refactoring...)

Comments

@bidoubiwa
Copy link
Contributor

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 ?

@bidoubiwa bidoubiwa added the maintenance Anything related to maintenance (CI, tests, refactoring...) label Nov 21, 2022
@sanders41
Copy link
Collaborator

There are several ways to go about this, none right or wrong just different choices. The way I chose to go about it is the Client and Index classes don't have task methods, instead the functions are called directly from the task module when needed. As an example here is my get_task.

Out of the options you have I would chose option one because a class with only static methods implies the class isn't needed (gain just choices not right or wrong). It would also mean not needing to pass the connection information every time if the TaskHandler is instantiated in the __init__ of the Client/Index.

@bidoubiwa
Copy link
Contributor Author

bidoubiwa commented Nov 21, 2022

In your suggestion, the user has to import the Task module to be able to use its functions?

from meilisearch_python_async.task import get_task

get_task(...)

@sanders41
Copy link
Collaborator

Yes, you would have to import it.

@bidoubiwa
Copy link
Contributor Author

By making this choice, are we not making the usage of the library more complex in favor of a certain code architecture?

@sanders41
Copy link
Collaborator

sanders41 commented Nov 22, 2022

In my opinion no, but that doesn't mean my opinion is everyone's opinion or the only correct opinion 🙂.

The reason I made this choice is a task is its own thing. It isn't related to a Clint it Index.

@Azanul Azanul mentioned this issue Jan 24, 2023
3 tasks
bors bot added a commit that referenced this issue Feb 20, 2023
662: Object oriented code r=alallema a=Azanul

# Pull Request

## Related issue
Fixes #604 

## What does this PR do?
- Converts root level functions to class methods

## PR checklist
Please check if your PR fulfills the following requirements:
- [X] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [X] Have you read the contributing guidelines?
- [X] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Azanul <[email protected]>
Co-authored-by: Azanul Haque <[email protected]>
Co-authored-by: Amélie <[email protected]>
@bors bors bot closed this as completed in cd8ac78 Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Anything related to maintenance (CI, tests, refactoring...)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants