urban-json-http 0.1.0__py3-none-any.whl
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.
- urban_json_http/__init__.py +110 -0
- urban_json_http/py.typed +0 -0
- urban_json_http-0.1.0.dist-info/METADATA +79 -0
- urban_json_http-0.1.0.dist-info/RECORD +8 -0
- urban_json_http-0.1.0.dist-info/WHEEL +5 -0
- urban_json_http-0.1.0.dist-info/entry_points.txt +2 -0
- urban_json_http-0.1.0.dist-info/licenses/LICENSE +21 -0
- urban_json_http-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Plugin jslt : appels HTTP depuis les templates (``http`` / ``httpget``).
|
|
2
|
+
|
|
3
|
+
Le moteur .NET n'offre qu'un GET minimal (``loadhttpgetJson``). Ce plugin le
|
|
4
|
+
remplace par une formalisation **déclarative** de l'appel, suivant la convention
|
|
5
|
+
Fetch / ``RequestInit`` (le vocabulaire HTTP-en-JSON standard) :
|
|
6
|
+
|
|
7
|
+
* ``http(request)`` — ``request`` est un objet
|
|
8
|
+
``{method, url, headers, query, body, timeout}`` ; renvoie un objet réponse
|
|
9
|
+
``{status, headers, body}`` (``body`` = JSON parsé si la réponse est du JSON,
|
|
10
|
+
sinon le texte brut).
|
|
11
|
+
* ``httpget(url, headers)`` — convenance GET renvoyant directement le ``body``.
|
|
12
|
+
|
|
13
|
+
Client HTTP : `httpx`. Le corps (``body``) est envoyé en JSON si c'est un objet ou
|
|
14
|
+
un tableau, sinon tel quel.
|
|
15
|
+
|
|
16
|
+
Sécurité : ``http`` réalise de **vraies requêtes réseau** vers l'URL fournie.
|
|
17
|
+
Attention aux URL issues de données non fiables (SSRF). Un ``timeout`` par défaut
|
|
18
|
+
(30 s) est appliqué et surchargeable par requête.
|
|
19
|
+
|
|
20
|
+
Découvert automatiquement par ``jslt`` via l'entry point ``urban_json.functions``.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
from typing import Any
|
|
26
|
+
|
|
27
|
+
import httpx
|
|
28
|
+
|
|
29
|
+
__version__ = "0.1.0"
|
|
30
|
+
|
|
31
|
+
__all__ = ["execute_http", "execute_httpget", "register"]
|
|
32
|
+
|
|
33
|
+
#: Timeout par défaut (secondes) si la requête n'en précise pas.
|
|
34
|
+
DEFAULT_TIMEOUT = 30.0
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _build_client(timeout: float) -> httpx.Client:
|
|
38
|
+
"""Fabrique le client HTTP. Point d'injection pour les tests (transport mocké)."""
|
|
39
|
+
return httpx.Client(timeout=timeout)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _parse_body(response: httpx.Response) -> Any:
|
|
43
|
+
"""Corps de réponse : JSON parsé si le content-type est JSON, sinon texte."""
|
|
44
|
+
content_type = response.headers.get("content-type", "")
|
|
45
|
+
if "json" in content_type.lower():
|
|
46
|
+
try:
|
|
47
|
+
return response.json()
|
|
48
|
+
except ValueError:
|
|
49
|
+
return response.text
|
|
50
|
+
return response.text
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def execute_http(ctx: Any, request: Any = None) -> dict:
|
|
54
|
+
"""Effectue un appel HTTP décrit par un objet Fetch/``RequestInit``.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
request: objet ``{method, url, headers, query, body, timeout}``.
|
|
58
|
+
``method`` défaut ``GET`` ; ``headers``/``query`` sont des objets ;
|
|
59
|
+
``body`` est envoyé en JSON si objet/tableau, sinon tel quel.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
``{"status": int, "headers": {...}, "body": <json|texte>}``.
|
|
63
|
+
"""
|
|
64
|
+
if not isinstance(request, dict):
|
|
65
|
+
# ValueError (et non TypeError) : contrat « mauvais argument » uniforme.
|
|
66
|
+
raise ValueError("http: request doit être un objet {method, url, ...}") # noqa: TRY004
|
|
67
|
+
url = request.get("url")
|
|
68
|
+
if not url:
|
|
69
|
+
raise ValueError("http: 'url' est requis")
|
|
70
|
+
|
|
71
|
+
method = str(request.get("method") or "GET").upper()
|
|
72
|
+
headers = {str(k): str(v) for k, v in (request.get("headers") or {}).items()}
|
|
73
|
+
params = request.get("query") or {}
|
|
74
|
+
body = request.get("body")
|
|
75
|
+
timeout = request.get("timeout", DEFAULT_TIMEOUT)
|
|
76
|
+
|
|
77
|
+
kwargs: dict[str, Any] = {"headers": headers, "params": params}
|
|
78
|
+
if isinstance(body, (dict, list)):
|
|
79
|
+
kwargs["json"] = body
|
|
80
|
+
elif body is not None:
|
|
81
|
+
kwargs["content"] = body if isinstance(body, (str, bytes)) else str(body)
|
|
82
|
+
|
|
83
|
+
with _build_client(timeout) as client:
|
|
84
|
+
response = client.request(method, url, **kwargs)
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
"status": response.status_code,
|
|
88
|
+
"headers": dict(response.headers),
|
|
89
|
+
"body": _parse_body(response),
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def execute_httpget(ctx: Any, url: str | None = None, headers: Any = None) -> Any:
|
|
94
|
+
"""GET de convenance : renvoie directement le corps (port « amélioré » du .NET)."""
|
|
95
|
+
response = execute_http(ctx, {"method": "GET", "url": url, "headers": headers or {}})
|
|
96
|
+
return response["body"]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def register(registry: Any) -> None:
|
|
100
|
+
"""Enregistre les fonctions du plugin dans un registre jslt (entry point)."""
|
|
101
|
+
registry.register(
|
|
102
|
+
"http",
|
|
103
|
+
execute_http,
|
|
104
|
+
description="Appel HTTP (objet Fetch {method,url,headers,query,body}) -> {status,headers,body}.",
|
|
105
|
+
)
|
|
106
|
+
registry.register(
|
|
107
|
+
"httpget",
|
|
108
|
+
execute_httpget,
|
|
109
|
+
description="GET HTTP renvoyant directement le corps (JSON parsé ou texte).",
|
|
110
|
+
)
|
urban_json_http/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: urban-json-http
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Plugin urban-json : appels HTTP depuis les templates (http / httpget).
|
|
5
|
+
Author-email: Gael Beard <gaelgael5@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/gaelgael5/Jslt
|
|
8
|
+
Project-URL: Repository, https://github.com/gaelgael5/Jslt
|
|
9
|
+
Keywords: json,transform,template,jslt,http,rest,plugin
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: urban-json
|
|
26
|
+
Requires-Dist: httpx>=0.24
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# urban-json-http
|
|
30
|
+
|
|
31
|
+
Plugin [urban-json](https://github.com/gaelgael5/Jslt) : appels **HTTP** depuis les
|
|
32
|
+
templates. L'appel est formalisé comme un objet **Fetch / `RequestInit`** (le
|
|
33
|
+
vocabulaire HTTP-en-JSON standard).
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install urban-json-http # tire urban-json et httpx
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`urban-json` découvre le plugin automatiquement via les entry points (`urban_json.functions`).
|
|
42
|
+
|
|
43
|
+
## Utilisation
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from urban_json import JsltTemplate
|
|
47
|
+
|
|
48
|
+
tpl = JsltTemplate.parse('''
|
|
49
|
+
{
|
|
50
|
+
"r:": http({
|
|
51
|
+
"method": "GET",
|
|
52
|
+
"url": "https://api.example.com/users",
|
|
53
|
+
"headers": { "Authorization": $.token },
|
|
54
|
+
"query": { "page": 1 }
|
|
55
|
+
}),
|
|
56
|
+
"code": r:$.status,
|
|
57
|
+
"users": r:$.body.items
|
|
58
|
+
}
|
|
59
|
+
''')
|
|
60
|
+
tpl.transform({"token": "Bearer xyz"})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
- `http(request)` — `request` : `{ method, url, headers, query, body, timeout }`.
|
|
64
|
+
Renvoie `{ status, headers, body }` ; `body` est le JSON parsé si la réponse est
|
|
65
|
+
du JSON, sinon le texte.
|
|
66
|
+
- `httpget(url, headers)` — convenance GET renvoyant directement le `body`.
|
|
67
|
+
|
|
68
|
+
Le corps (`body`) de la requête est envoyé en **JSON** si c'est un objet ou un
|
|
69
|
+
tableau, sinon tel quel. Verbe par défaut : `GET`. Timeout par défaut : 30 s
|
|
70
|
+
(surchargeable via `"timeout"`).
|
|
71
|
+
|
|
72
|
+
## Sécurité
|
|
73
|
+
|
|
74
|
+
`http` réalise de **vraies requêtes réseau**. Méfiez-vous des URL construites à
|
|
75
|
+
partir de données non fiables (SSRF). Un timeout par défaut est appliqué.
|
|
76
|
+
|
|
77
|
+
## Licence
|
|
78
|
+
|
|
79
|
+
Apache-2.0.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
urban_json_http/__init__.py,sha256=XAsD13Lw7WfY88-HMRRi7px8OS0RTs8MswVhmN5SYGI,4120
|
|
2
|
+
urban_json_http/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
urban_json_http-0.1.0.dist-info/licenses/LICENSE,sha256=LdXMQzg0w6hrGI_vhgHsmOLKcHTkyF5PyUXkP9fV7mA,1068
|
|
4
|
+
urban_json_http-0.1.0.dist-info/METADATA,sha256=I_KEA7tQR0Lr1_na7EieqBAjHMXIPdfc9Wev7_1ykiE,2558
|
|
5
|
+
urban_json_http-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
urban_json_http-0.1.0.dist-info/entry_points.txt,sha256=FtPwpBt_RqFqBtdR1pe69cwwGKNxNGEzn3_anrvxkxs,55
|
|
7
|
+
urban_json_http-0.1.0.dist-info/top_level.txt,sha256=mI3L1HVhgq2wJ00KK-xN_3GBscx1jhc9gc43HkCWVJI,16
|
|
8
|
+
urban_json_http-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 black beard
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
urban_json_http
|