Skip to content

Commit

Permalink
fix: only replaced ua if needed (#163)
Browse files Browse the repository at this point in the history
* fix: don't replace ua when already modified

* add eof

* fix for providers
  • Loading branch information
GaspardBT authored Dec 4, 2024
1 parent dcaabe1 commit e0e7886
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

from .types import BeforeRequestContext, BeforeRequestHook

prefix = "mistral-client-python/"

class CustomUserAgentHook(BeforeRequestHook):
def before_request(
self, hook_ctx: BeforeRequestContext, request: httpx.Request
) -> Union[httpx.Request, Exception]:
current = request.headers["user-agent"]
if current.startswith(prefix):
return request

request.headers["user-agent"] = (
"mistral-client-python/" + request.headers["user-agent"].split(" ")[1]
prefix + current.split(" ")[1]
)
return request

return request
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

from .types import BeforeRequestContext, BeforeRequestHook

prefix = "mistral-client-python/"

class CustomUserAgentHook(BeforeRequestHook):
def before_request(
self, hook_ctx: BeforeRequestContext, request: httpx.Request
) -> Union[httpx.Request, Exception]:
current = request.headers["user-agent"]
if current.startswith(prefix):
return request

request.headers["user-agent"] = (
"mistral-client-python/" + request.headers["user-agent"].split(" ")[1]
prefix + current.split(" ")[1]
)

return request
8 changes: 7 additions & 1 deletion src/mistralai/_hooks/custom_user_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

from .types import BeforeRequestContext, BeforeRequestHook

prefix = "mistral-client-python/"

class CustomUserAgentHook(BeforeRequestHook):
def before_request(
self, hook_ctx: BeforeRequestContext, request: httpx.Request
) -> Union[httpx.Request, Exception]:
current = request.headers["user-agent"]
if current.startswith(prefix):
return request

request.headers["user-agent"] = (
"mistral-client-python/" + request.headers["user-agent"].split(" ")[1]
prefix + current.split(" ")[1]
)

return request

0 comments on commit e0e7886

Please sign in to comment.