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

Adding a header parameters to the client constructor #771

Merged
merged 8 commits into from
Jun 1, 2023

Conversation

alallema
Copy link
Contributor

To enable and facilitate the integration of a user-agent into all SDKs. see related issue and specifically for the docs-scraper integration a new parameter client_agent is now available in the client constructor.

@alallema alallema added the enhancement New feature or request label May 31, 2023
@alallema alallema requested a review from brunoocasali May 31, 2023 11:51
@alallema alallema force-pushed the feature/add_header_option_on_client_constructor branch from 7353639 to d79b5e4 Compare May 31, 2023 13:12
Comment on lines 22 to 23
"User-Agent": qualified_version()
+ (";" + ";".join(config.client_agents) if config.client_agents else ""),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not very satisfied with this line, if you know a better way, @sanders41 maybe, please lead the way!

Copy link
Member

Choose a reason for hiding this comment

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

Does this work?

"User-Agent": ";".join(config.client_agents.append(qualified_version()))

Just ensure the client_agents is at least a [] array

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right now the user agent is a string like: Meilisearch Python (v0.26.0);Meilisearch Package1 (v1.1.1);Meilisearch Package2 (v2.2.2)

Are you saying you want [Meilisearch Python (v0.26.0), Meilisearch Package1 (v1.1.1), Meilisearch Package2 (v2.2.2)]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think he meant the client's input parameter, making adding several headers possible.

Copy link
Member

Choose a reason for hiding this comment

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

The outcome should be a string, but until the last part (sending the data to the header) we could work with an array.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, if I'm understanding correctly this should be good then. The user specifies it as a tuple like ("Meilisearch Package1 (v1.1.1)", "Meilisearch Package2 (v2.2.2)") and can add as many as needed.

Copy link
Collaborator

@sanders41 sanders41 left a comment

Choose a reason for hiding this comment

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

Alright, these suggested changes are all based around the same idea of caching the user agent. Doing this means that the string only needs to be built once, and not for every request leading to better performance. All parameters to lru_cache need to be hashable so this is why I changed from List to Tuple (a bonus here is tuples are also more perfomant).

I couldn't really think of a cleaner way than what you did to build the string itself, so this suggestion is for performance, with the string building being mostly the same.

If you don't like this suggestion and don't want to use it it won't hurt my feelings 😄

meilisearch/_httprequests.py Outdated Show resolved Hide resolved
meilisearch/client.py Outdated Show resolved Hide resolved
meilisearch/config.py Outdated Show resolved Hide resolved
meilisearch/config.py Outdated Show resolved Hide resolved
tests/client/test_http_requests.py Outdated Show resolved Hide resolved
@alallema alallema force-pushed the feature/add_header_option_on_client_constructor branch from 86a89b6 to dce8fe0 Compare May 31, 2023 14:18
@alallema
Copy link
Contributor Author

If you don't like this suggestion and don't want to use it it won't hurt my feelings 😄

No, I prefer this solution much more. Thanks a lot ❤️

Comment on lines 98 to 105
@lru_cache(maxsize=1)
def _build_user_agent(self, client_agents: Optional[Tuple[str]] = None) -> str:
user_agent = qualified_version()
if not client_agents:
return user_agent

return f"{user_agent};{';'.join(client_agents)}"

Copy link
Collaborator

Choose a reason for hiding this comment

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

You will want to move this out of the class or it can lead to memory leaks. If you want me to push an update so you can see what I mean let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes, I would like that, I'm not really sure where to put it.

@@ -19,7 +20,7 @@ def __init__(self, config: Config) -> None:
self.config = config
self.headers = {
"Authorization": f"Bearer {self.config.api_key}",
"User-Agent": qualified_version(),
"User-Agent": self._build_user_agent(config.client_agents),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"User-Agent": self._build_user_agent(config.client_agents),
"User-Agent": _build_user_agent(config.client_agents),

Copy link
Collaborator

Choose a reason for hiding this comment

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

No because _build_user_agent shouldn't be part of the class

Copy link
Collaborator

Choose a reason for hiding this comment

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

The reason way is a bit hard to explain here, but this video does a really good job explaining it https://www.youtube.com/watch?v=sVjtp6tGo0g

@sanders41
Copy link
Collaborator

Alright @alallema I pushed an update for you.

@alallema
Copy link
Contributor Author

Alright @alallema I pushed an update for you.

Perfect! I think I get it now I didn't finish the video yet but it's already clearer ❤️

Copy link
Member

@brunoocasali brunoocasali left a comment

Choose a reason for hiding this comment

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

Thanks @sanders41 for your inputs here and also for the video, very clear explanation!

LGTM! @alallema

@alallema
Copy link
Contributor Author

alallema commented Jun 1, 2023

bors merge

@meili-bors
Copy link
Contributor

meili-bors bot commented Jun 1, 2023

@meili-bors meili-bors bot merged commit 7ae903a into main Jun 1, 2023
@meili-bors meili-bors bot deleted the feature/add_header_option_on_client_constructor branch June 1, 2023 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants