Skip to content

Commit

Permalink
Fix client instances sharing same kwarg values (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
vs4vijay authored Nov 14, 2024
1 parent a49b58d commit ce97fb4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/declarativex/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,16 @@ def merge(self, other: "ClientConfiguration") -> "ClientConfiguration":
take precedence over the values of this configuration.
"""
return ClientConfiguration(
base_url=self.base_url if self.base_url else other.base_url,
auth=self.auth if self.auth else other.auth,
base_url=other.base_url if other.base_url else self.base_url,
auth=other.auth if other.auth else self.auth,
default_query_params={
**other.default_query_params,
**self.default_query_params,
**other.default_query_params,
},
default_headers={**other.default_headers, **self.default_headers},
middlewares=[*other.middlewares, *self.middlewares],
default_headers={**self.default_headers, **other.default_headers},
middlewares=other.middlewares,
error_mappings={**other.error_mappings, **self.error_mappings},
proxies=merge_proxies(other.proxies, self.proxies),
proxies=merge_proxies(self.proxies, other.proxies),
)

@classmethod
Expand Down
33 changes: 33 additions & 0 deletions tests/test_sync_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,36 @@ def get_users(files: Annotated[dict, Files]) -> dict:
in send_mock.send.call_args_list[0].args[0].headers["content-type"]
)
assert file_content in send_mock.send.call_args_list[0].args[0].read()


def test_multiple_instances():
PMAN_URL = "https://postman-echo.com"
INVALID_URL = "https://invalid.com"

class EchoClient(BaseClient):
@http("get", "/get")
def sample_get(self) -> dict:
...

auth1 = httpx.BasicAuth("client1", "secret1")
client1 = EchoClient(
base_url=PMAN_URL,
default_headers={"x-request-origin": "client1"},
default_query_params={"x-my-param": "client1-param"},
auth=auth1,
)

result1 = client1.sample_get()
assert result1["headers"]["x-request-origin"] == "client1"
assert result1["args"]["x-my-param"] == "client1-param"

auth2 = httpx.BasicAuth("client2", "secret2")
client2 = EchoClient(
base_url=INVALID_URL,
default_headers={"x-request-origin": "client2"},
default_query_params={"x-my-param": "client2-param"},
auth=auth2,
)

with pytest.raises(httpx.RequestError):
client2.sample_get()

0 comments on commit ce97fb4

Please sign in to comment.