Skip to content

Commit

Permalink
Add Client.request_kwargs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler committed Nov 14, 2022
1 parent bdcf746 commit 1f66c8f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
7 changes: 6 additions & 1 deletion inori/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import uuid
from typing import Dict, List, Union
from typing import Dict, List, Optional, Union

import shibari

Expand Down Expand Up @@ -68,6 +68,9 @@ class Client:
Attributes:
headers: Dictionary containing all Client-level headers.
request_kwargs: Dictionary of any arguments to send with every
request. Applies to all registered Routes.
logger: Unique logger instance for each Client instance.
request_metadata: Dictionary of relevant metadata from
Expand Down Expand Up @@ -97,6 +100,8 @@ def __init__(self, base_uri: str):

self.headers = HeaderDict()

self.request_kwargs: dict[Optional[str], Optional[str]] = {}

self.logger = logging.getLogger(f'{__name__} {str(uuid.uuid4())}')
self.logger.addHandler(logging.NullHandler())

Expand Down
7 changes: 6 additions & 1 deletion inori/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,16 @@ def request(self,

self.client.log_request(request_metadata)

evaluated_kwargs: Dict[str, str] = {
**kwargs,
**self.client.request_kwargs,
}

response = self.session.request(
http_method,
self.url,
headers=evaluated_headers,
**kwargs,
**evaluated_kwargs,
)

response_metadata = {
Expand Down
25 changes: 23 additions & 2 deletions tests/route/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,29 @@ class MockResponse:


class MockSession:
def request(self, http_method, url, headers):
return MockResponse(http_method, url, headers, 200, 'Fake Response')
def __init__(self, *args, **kwargs):
pass

def request(self, http_method, url, headers, **kwargs):
return MockResponse(
http_method, url, headers, 200, f'Mock Response: kwargs={kwargs}',
)


@mock.patch('requests.Session', MockSession)
def test_client_request_kwargs():
"""
Given a client has request_kwargs defined
When a request is made
Then request_kwargs are used in the request
"""
client = Client('https://foo.com/v1/')
client.request_kwargs = {'verify': False}

route = client.add_route('bar')

result = route.get()
assert result.text == f'Mock Response: kwargs={client.request_kwargs}'


@mock.patch('requests.Session', MockSession)
Expand Down

0 comments on commit 1f66c8f

Please sign in to comment.