Skip to content

Commit

Permalink
Refactor ChaliceClient to handle JSON requests and remove helper func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
gabino committed Jan 22, 2025
1 parent 09796c1 commit 9e510e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 42 deletions.
36 changes: 16 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime as dt
import functools
import json
from typing import Callable, Generator

import pytest
Expand All @@ -15,8 +16,6 @@
)
from examples.models import Account, Biller, Card, File, User

from .helpers import accept_json

FuncDecorator = Callable[..., Generator]


Expand Down Expand Up @@ -187,36 +186,33 @@ def headers(self):


class ChaliceClient(OriginalChaliceClient):
def post(self, url: str, **kwargs) -> ChaliceResponse:
response = self.http.post(url, **kwargs)
def _request_with_json(
self, method: str, url: str, **kwargs
) -> ChaliceResponse:
body = json.dumps(kwargs.pop('json')) if 'json' in kwargs else None
headers = {'Content-Type': 'application/json'}
response = getattr(self.http, method)(
url, body=body, headers=headers, **kwargs
)
return ChaliceResponse(response)

def post(self, url: str, **kwargs) -> ChaliceResponse:
return self._request_with_json('post', url, **kwargs)

def get(self, url: str, **kwargs) -> ChaliceResponse:
response = self.http.get(url, **kwargs)
return ChaliceResponse(response)

def patch(self, url: str, **kwargs) -> ChaliceResponse:
response = self.http.patch(url, **kwargs)
return ChaliceResponse(response)
return self._request_with_json('patch', url, **kwargs)

def delete(self, url: str, **kwargs) -> ChaliceResponse:
response = self.http.delete(url, **kwargs)
return ChaliceResponse(response)
return self._request_with_json('delete', url, **kwargs)


@pytest.fixture()
def chalice_client() -> Generator[ChaliceClient, None, None]:
from examples.chalice import app

with ChaliceClient(app) as client:
client.http.post = accept_json( # type: ignore[assignment]
client.http.post
)
client.http.patch = accept_json( # type: ignore[assignment]
client.http.patch
)

client.http.delete = accept_json( # type: ignore[assignment]
client.http.delete
)
yield client # type: ignore[misc]
client = ChaliceClient(app)
yield client
22 changes: 0 additions & 22 deletions tests/helpers.py

This file was deleted.

0 comments on commit 9e510e3

Please sign in to comment.