Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def __init__(self, config: Config) -> None:
self.config = config
self.headers = {
"Authorization": f"Bearer {self.config.api_key}",
"User-Agent": qualified_version(),
"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.

}

def send_request(
Expand Down
8 changes: 6 additions & 2 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class Client:
"""

def __init__(
self, url: str, api_key: Optional[str] = None, timeout: Optional[int] = None
self,
url: str,
api_key: Optional[str] = None,
timeout: Optional[int] = None,
client_agents: Optional[List[str]] = None,
) -> None:
"""
Parameters
Expand All @@ -39,7 +43,7 @@ def __init__(
api_key:
The optional API key for Meilisearch
"""
self.config = Config(url, api_key, timeout=timeout)
self.config = Config(url, api_key, timeout=timeout, client_agents=client_agents)

self.http = HttpRequests(self.config)

Expand Down
9 changes: 7 additions & 2 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Optional
from typing import List, Optional


class Config:
Expand Down Expand Up @@ -35,7 +35,11 @@ class Paths:
swap = "swap-indexes"

def __init__(
self, url: str, api_key: Optional[str] = None, timeout: Optional[int] = None
self,
url: str,
api_key: Optional[str] = None,
timeout: Optional[int] = None,
client_agents: Optional[List[str]] = None,
) -> None:
"""
Parameters
Expand All @@ -49,4 +53,5 @@ def __init__(
self.url = url
self.api_key = api_key
self.timeout = timeout
self.client_agents = client_agents
self.paths = self.Paths()
17 changes: 17 additions & 0 deletions tests/client/test_http_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,20 @@ def test_get_headers_from_http_requests_instance():

assert http.headers["Authorization"] == f"Bearer {MASTER_KEY}"
assert http.headers["User-Agent"] == qualified_version()


def test_get_headers_with_multiple_user_agent():
"""Tests getting defined headers from instance in HttpRequests."""
config = Config(
BASE_URL,
MASTER_KEY,
timeout=None,
client_agents=["Meilisearch Package1 (v1.1.1)", "Meilisearch Package2 (v2.2.2)"],
)
http = HttpRequests(config=config)

assert http.headers["Authorization"] == f"Bearer {MASTER_KEY}"
assert (
http.headers["User-Agent"]
== qualified_version() + ";Meilisearch Package1 (v1.1.1);Meilisearch Package2 (v2.2.2)"
)