Skip to content

Commit

Permalink
Merge pull request #42 from reagento/develop
Browse files Browse the repository at this point in the history
v0.5
  • Loading branch information
Tishka17 authored Oct 19, 2024
2 parents 8d0a128 + 692cc27 commit 0da3fb0
Show file tree
Hide file tree
Showing 32 changed files with 741 additions and 203 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: CI

on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]

jobs:
cpython:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
python-version:
- "3.10"
- "3.11"
- "3.12"
- "3.13"

steps:
- uses: actions/checkout@v4
- name: Set up ${{ matrix.python-version }} on ${{ matrix.os }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install '.' -r requirements_dev.txt
- name: Run ruff
run: |
ruff check .
- name: Run tests
run: |
pytest
42 changes: 42 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
line-length = 79
target-version="py38"
src = ["src"]

include = ["src/**.py", "tests/**.py"]
exclude = ["src/dishka/_adaptix/**"]

lint.select = [
"ALL"
]
lint.ignore = [
"ARG",
"ANN",
"D",
"EM101",
"EM102",
"PT001",
"PT023",
"SIM108",
"SIM114",
"TRY003",
"PLW2901",
"RET505",
"RET506",
"PLR0913",
"UP038",
"TCH001",
"FA100",
# tempraty disabled
"PGH005",
"PLR2004",
"N818", # compatibility issue
]

[lint.per-file-ignores]
"tests/**" = ["TID252", "PLR2004", "S101", "A002"]

[lint.isort]
no-lines-before = ["local-folder"]

[lint.flake8-tidy-imports]
ban-relative-imports = "parents"
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

64 changes: 63 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 Expand Up @@ -102,3 +127,40 @@ To set same behavior for all methods inherit from BoundMethod class, override `_
### Other params

You can use different body argument name if you want. Just pass `body_name` to the decorator.


### Special cases

#### `None` in query params

By default, AioHTTP doesn't skip query params, you can customize that overriding `_pre_process_request` in Method class

```python
class NoneAwareAiohttpMethod(AiohttpMethod):
async def _pre_process_request(self, request: HttpRequest) -> HttpRequest:
request.query_params = {
k: v for k, v in request.query_params.items() if v is not None
}
return request


class Client(AiohttpClient):
method_class = NoneAwareAiohttpMethod
```

#### Handling `No content`

By default, en each method json response is expected. Sometime you expect no content from server. Especially for 204.
You can handle it by overriding `_response_body` method, e.g.:

```python
class NoneAwareRequestsMethod(RequestsMethod):
def _response_body(self, response: Response) -> Any:
if response.status_code == http.HTTPStatus.NO_CONTENT:
return None
return super()._response_body(response)


class Client(RequestsClient):
method_class = NoneAwareRequestsMethod
```
8 changes: 0 additions & 8 deletions dataclass_rest/__init__.py

This file was deleted.

39 changes: 0 additions & 39 deletions dataclass_rest/rest.py

This file was deleted.

4 changes: 2 additions & 2 deletions examples/async_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ async def delete_todo(self, id: int):

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

@get("https://httpbin.org/get")
def get_httpbin(self) -> Any:
"""Используемый другой base_url"""
"""Используем другой base_url"""

@post("https://httpbin.org/post")
def upload_image(self, file: File):
Expand Down
38 changes: 38 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[build-system]
requires = ["setuptools>=66.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
include-package-data = true

[tool.setuptools.packages.find]
where = ["src"]

[project]
name = "dataclass_rest"
version = "0.4"
readme = "README.md"
authors = [
{ name = "Andrey Tikhonov", email = "[email protected]" },
]
license = { text = "Apache-2.0" }
description = "An utility for writing simple clients for REST like APIs"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"Topic :: Software Development :: Libraries",
"Typing :: Typed",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
]
dependencies = [
"adaptix",
]

[project.urls]
"Source" = "https://github.com/reagento/dataclass-rest"
"Homepage" = "https://github.com/reagento/dataclass-rest"
"Bug Tracker" = "https://github.com/reagento/dataclass-rest/issues"


6 changes: 0 additions & 6 deletions requirements.txt

This file was deleted.

10 changes: 10 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
adaptix
typing_extensions
aiohttp
requests
requests-mock
mypy
pytest
pytest-asyncio

ruff==0.5.*
37 changes: 0 additions & 37 deletions setup.py

This file was deleted.

12 changes: 12 additions & 0 deletions src/dataclass_rest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
__all__ = [
"File",
"rest",
"get",
"put",
"post",
"patch",
"delete",
]

from .http_request import File
from .rest import delete, get, patch, post, put, rest
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from adaptix import Retort

from .client_protocol import (
ClientProtocol, FactoryProtocol,
ClientProtocol,
FactoryProtocol,
)


Expand Down
Loading

0 comments on commit 0da3fb0

Please sign in to comment.