libentry 1.22.3__py3-none-any.whl → 1.23__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.
libentry/api.py CHANGED
@@ -225,7 +225,7 @@ class BaseClient:
225
225
  verify: bool,
226
226
  ) -> Union[bytes, Iterable[bytes]]:
227
227
  headers = self.headers if headers is None else {**self.headers, **headers}
228
- response = self.URLLIB3_POOL[int(verify)].request(
228
+ response = self.URLLIB3_POOL[int(verify)].http_request(
229
229
  method=method,
230
230
  url=url,
231
231
  body=body,
libentry/json.py CHANGED
@@ -13,6 +13,7 @@ from base64 import b64decode, b64encode
13
13
  from functools import partial
14
14
 
15
15
  import numpy as np
16
+ from pydantic import BaseModel
16
17
 
17
18
  _BINDINGS = []
18
19
 
@@ -26,6 +27,9 @@ def bind(name, type_):
26
27
 
27
28
 
28
29
  def custom_encode(o) -> dict:
30
+ if isinstance(o, BaseModel):
31
+ return o.model_dump()
32
+
29
33
  for name, type_, support in _BINDINGS:
30
34
  if isinstance(o, type_):
31
35
  doc = support.encode(o)
@@ -0,0 +1 @@
1
+ #!/usr/bin/env python3
libentry/mcp/api.py ADDED
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env python3
2
+
3
+ __author__ = "xi"
4
+ __all__ = [
5
+ "APIInfo",
6
+ "api",
7
+ "route",
8
+ "get",
9
+ "post",
10
+ "tool",
11
+ "resource",
12
+ "list_api_info",
13
+ ]
14
+
15
+ from functools import partial
16
+ from typing import Callable, List, Literal, Optional, Tuple
17
+
18
+ from pydantic import BaseModel, ConfigDict
19
+
20
+ API_INFO_FIELD = "__api_info__"
21
+
22
+
23
+ class APIInfo(BaseModel):
24
+ path: str
25
+ methods: List[Literal["GET", "POST"]]
26
+ name: str
27
+ description: Optional[str]
28
+ tag: Optional[str] = None
29
+
30
+ model_config = ConfigDict(extra="allow")
31
+
32
+
33
+ def api(
34
+ path: Optional[str] = None,
35
+ methods: List[Literal["GET", "POST"]] = ("GET", "POST"),
36
+ name: Optional[str] = None,
37
+ description: Optional[str] = None,
38
+ tag: Optional[str] = None,
39
+ **kwargs
40
+ ) -> Callable:
41
+ def decorator(fn: Callable):
42
+ if not hasattr(fn, "__name__"):
43
+ raise RuntimeError("At least one of \"path\" or \"fn.__name__\" should be given.")
44
+ fn_name = getattr(fn, "__name__")
45
+ fn_doc = getattr(fn, "__doc__")
46
+
47
+ api_info = APIInfo(
48
+ path=path or f"/{fn_name}",
49
+ methods=methods,
50
+ name=name or fn_name,
51
+ description=description or fn_doc,
52
+ tag=tag
53
+ )
54
+ api_info.model_extra.update(kwargs)
55
+ setattr(fn, API_INFO_FIELD, api_info)
56
+ return fn
57
+
58
+ return decorator
59
+
60
+
61
+ route = api
62
+ get = partial(api, methods=["GET"])
63
+ post = partial(api, methods=["POST"])
64
+
65
+
66
+ def tool(path=None, name=None, description=None, **kwargs):
67
+ return api(
68
+ path=path,
69
+ methods=["POST"],
70
+ name=name,
71
+ description=description,
72
+ tag="tool",
73
+ **kwargs
74
+ )
75
+
76
+
77
+ def resource(uri, name=None, description=None, mime_type=None, size=None, **kwargs):
78
+ return api(
79
+ path=None,
80
+ methods=["POST"],
81
+ name=name,
82
+ description=description,
83
+ tag="resource",
84
+ mimeType=mime_type,
85
+ size=size,
86
+ uri=uri,
87
+ **kwargs
88
+ )
89
+
90
+
91
+ def list_api_info(obj) -> List[Tuple[Callable, APIInfo]]:
92
+ api_list = []
93
+ for name in dir(obj):
94
+ fn = getattr(obj, name)
95
+ if not callable(fn):
96
+ continue
97
+ if not hasattr(fn, API_INFO_FIELD):
98
+ continue
99
+ api_info = getattr(fn, API_INFO_FIELD)
100
+ api_list.append((fn, api_info))
101
+ return api_list