Skip to content

Commit

Permalink
tests, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lubaskinc0de committed Jul 29, 2024
1 parent 3e0be5f commit c9b6654
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,32 @@ class RealClient(RequestsClient):

@post("todos")
def create_todo(self, body: Todo) -> Todo:
"""Создаем Todo"""
pass
```

You can use Callable ```(...) -> str``` as the url source,
all parameters passed to the client method can be obtained inside the Callable

```python
from requests import Session
from dataclass_rest import get
from dataclass_rest.http.requests import RequestsClient

def url_generator(todo_id: int) -> str:
return f"/todos/{todo_id}/"


class RealClient(RequestsClient):
def __init__(self):
super().__init__("https://dummyjson.com/", Session())

@get(url_generator)
def todo(self, todo_id: int) -> Todo:
pass


client = RealClient()
client.todo(5)
```

## Asyncio
Expand Down
94 changes: 94 additions & 0 deletions tests/requests/test_callable_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from typing import List, Optional

import pytest
import requests
import requests_mock

from dataclass_rest import get
from dataclass_rest.http.requests import RequestsClient


def static_url() -> str:
return "/get"


def param_url(entry_id: int) -> str:
return f"/get/{entry_id}"


def kwonly_param_url(entry_id: Optional[int] = None) -> str:
if entry_id:
return f"/get/{entry_id}"
return "/get/random"


def test_simple(session: requests.Session, mocker: requests_mock.Mocker):
class Api(RequestsClient):
@get(static_url)
def get_x(self) -> List[int]:
raise NotImplementedError

mocker.get("http://example.com/get", text="[1,2]", complete_qs=True)
client = Api(base_url="http://example.com", session=session)
assert client.get_x() == [1, 2]


@pytest.mark.parametrize(
("value", "expected"),
[
(
1,
1,
),
(
2,
2,
),
],
)
def test_with_param(
session: requests.Session,
mocker: requests_mock.Mocker,
value: int,
expected: int,
):
class Api(RequestsClient):
@get(param_url)
def get_entry(self, entry_id: int) -> int:
raise NotImplementedError

url = f"http://example.com/get/{expected}"
mocker.get(url, text=str(expected), complete_qs=True)

client = Api(base_url="http://example.com", session=session)
assert client.get_entry(value) == expected


def test_excess_param(session: requests.Session, mocker: requests_mock.Mocker):
class Api(RequestsClient):
@get(param_url)
def get_entry(
self, entry_id: int, some_param: Optional[int] = None,
) -> int:
raise NotImplementedError

mocker.get(
"http://example.com/get/1?some_param=2", text="1", complete_qs=True,
)

client = Api(base_url="http://example.com", session=session)
assert client.get_entry(1, 2) == 1


def test_kwonly_param(session: requests.Session, mocker: requests_mock.Mocker):
class Api(RequestsClient):
@get(kwonly_param_url)
def get_entry(self, *, entry_id: Optional[int] = None) -> int:
raise NotImplementedError

mocker.get("http://example.com/get/1", text="1", complete_qs=True)
mocker.get("http://example.com/get/random", text="2", complete_qs=True)

client = Api(base_url="http://example.com", session=session)
assert client.get_entry(entry_id=1) == 1
assert client.get_entry() == 2

0 comments on commit c9b6654

Please sign in to comment.