Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 11 additions & 2 deletions meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import json
from typing import Any, Callable, Dict, List, Optional, Union
from functools import lru_cache
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import requests

Expand All @@ -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

}

def send_request(
Expand Down Expand Up @@ -94,6 +95,14 @@ def delete(
) -> Any:
return self.send_request(requests.delete, path, body)

@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.

@staticmethod
def __to_json(request: requests.Response) -> Any:
if request.content == b"":
Expand Down
10 changes: 7 additions & 3 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import hmac
import json
import re
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Tuple, Union
from urllib import parse

from meilisearch._httprequests import HttpRequests
Expand All @@ -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[Tuple[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 Optional, Tuple


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[Tuple[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)"
)