rapid-api-client 0.3.4__tar.gz → 0.3.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: rapid-api-client
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: Rapidly develop your API clients using decorators and annotations
5
5
  Author: Sébastien MB
6
6
  Author-email: seb@essembeh.org
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "rapid-api-client"
3
- version = "0.3.4"
3
+ version = "0.3.6"
4
4
  description = "Rapidly develop your API clients using decorators and annotations"
5
5
  authors = ["Sébastien MB <seb@essembeh.org>"]
6
6
  readme = "README.md"
@@ -3,7 +3,7 @@ Model classes
3
3
  """
4
4
 
5
5
  from dataclasses import dataclass
6
- from typing import Any, Literal
6
+ from typing import Any
7
7
 
8
8
  from pydantic import BaseModel
9
9
 
@@ -49,8 +49,6 @@ class Body(BaseAnnotation):
49
49
  Annotation to declare an argument used as http content for post/put/...
50
50
  """
51
51
 
52
- target: Literal["content", "data", "files"] = "content"
53
-
54
52
  def serialize(self, body: Any) -> str | bytes:
55
53
  """
56
54
  Serialize the annotated parameter value
@@ -58,13 +56,24 @@ class Body(BaseAnnotation):
58
56
  return body
59
57
 
60
58
 
59
+ @dataclass
60
+ class FormBody(Body):
61
+ """
62
+ Annotation to declare an argument used as url-encoded parameter
63
+ If the annotated parameter is a Dict[str, str], its content will be merged to other form parameters,
64
+ else, its name (or alias) and its serialized value will be added to the the other form parameters.
65
+
66
+ """
67
+
68
+ alias: str | None = None
69
+
70
+
61
71
  @dataclass
62
72
  class FileBody(Body):
63
73
  """
64
74
  Annotation to declare an argument used as file to be uploaded
65
75
  """
66
76
 
67
- target: Literal["content", "data", "files"] = "files"
68
77
  alias: str | None = None
69
78
 
70
79
 
@@ -16,9 +16,13 @@ from typing import (
16
16
 
17
17
  from httpx import AsyncClient, Request, Response
18
18
  from pydantic import BaseModel, TypeAdapter
19
- from pydantic_xml import BaseXmlModel
20
19
 
21
- from .annotations import BaseAnnotation, Body, FileBody, Header, Path, Query
20
+ try:
21
+ import pydantic_xml
22
+ except ImportError: # pragma: nocover
23
+ pydantic_xml = None # type: ignore
24
+
25
+ from .annotations import BaseAnnotation, Body, FileBody, FormBody, Header, Path, Query
22
26
  from .utils import filter_none_values, find_annotation
23
27
 
24
28
  BM = TypeVar("BM", bound=BaseModel)
@@ -103,8 +107,14 @@ class RapidApi:
103
107
  if isinstance(annot, FileBody):
104
108
  files = build_kwargs.setdefault("files", {})
105
109
  files[annot.alias or param] = annot.serialize(value)
110
+ elif isinstance(annot, FormBody):
111
+ data = build_kwargs.setdefault("data", {})
112
+ if isinstance(value, dict):
113
+ data.update(value)
114
+ else:
115
+ data[annot.alias or param] = annot.serialize(value)
106
116
  else:
107
- build_kwargs[annot.target] = annot.serialize(value)
117
+ build_kwargs["content"] = annot.serialize(value)
108
118
 
109
119
  # handle extra optional kwargs
110
120
  if timeout is not None:
@@ -131,7 +141,9 @@ class RapidApi:
131
141
  return response.content
132
142
  if isinstance(response_class, TypeAdapter):
133
143
  return response_class.validate_json(response.content)
134
- if issubclass(response_class, BaseXmlModel):
144
+ if pydantic_xml is not None and issubclass(
145
+ response_class, pydantic_xml.BaseXmlModel
146
+ ):
135
147
  return response_class.from_xml(response.content)
136
148
  if issubclass(response_class, BaseModel):
137
149
  return response_class.model_validate_json(response.content)