Skip to content

Commit f5e52c2

Browse files
author
Robin VAN DE MERGHEL
committed
fix: Patch client to use in in DIRAC
1 parent a2060ab commit f5e52c2

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

diracx-client/src/diracx/client/_generated/aio/operations/_patch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
__all__ = [
1212
"AuthOperations",
1313
"JobsOperations",
14+
"PilotsOperations"
1415
] # Add all objects you want publicly available to users at this package level
1516

1617
from ....patches.auth.aio import AuthOperations
1718
from ....patches.jobs.aio import JobsOperations
19+
from ....patches.pilots.aio import PilotsOperations
1820

1921

2022
def patch_sdk():

diracx-client/src/diracx/client/_generated/operations/_patch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
__all__ = [
1212
"AuthOperations",
1313
"JobsOperations",
14+
"PilotsOperations"
1415
] # Add all objects you want publicly available to users at this package level
1516

1617
from ...patches.auth.sync import AuthOperations
1718
from ...patches.jobs.sync import JobsOperations
19+
from ...patches.pilots.sync import PilotsOperations
1820

1921

2022
def patch_sdk():
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Patches for the autorest-generated pilots client.
2+
3+
This file can be used to customize the generated code for the pilots client.
4+
When adding new classes to this file, make sure to also add them to the
5+
__all__ list in the corresponding file in the patches directory.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
__all__ = [
11+
"PilotsOperations",
12+
]
13+
14+
from typing import Any, Unpack
15+
16+
from azure.core.tracing.decorator_async import distributed_trace_async
17+
18+
from ..._generated.aio.operations._operations import PilotsOperations as _PilotsOperations
19+
from .common import make_search_body, make_summary_body, SearchKwargs, SummaryKwargs
20+
21+
# We're intentionally ignoring overrides here because we want to change the interface.
22+
# mypy: disable-error-code=override
23+
24+
25+
class PilotsOperations(_PilotsOperations):
26+
@distributed_trace_async
27+
async def search(self, **kwargs: Unpack[SearchKwargs]) -> list[dict[str, Any]]:
28+
"""TODO"""
29+
return await super().search(**make_search_body(**kwargs))
30+
31+
@distributed_trace_async
32+
async def summary(self, **kwargs: Unpack[SummaryKwargs]) -> list[dict[str, Any]]:
33+
"""TODO"""
34+
return await super().summary(**make_summary_body(**kwargs))
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""Utilities which are common to the sync and async pilots operator patches."""
2+
3+
from __future__ import annotations
4+
5+
__all__ = [
6+
"make_search_body",
7+
"SearchKwargs",
8+
"make_summary_body",
9+
"SummaryKwargs",
10+
]
11+
12+
import json
13+
from io import BytesIO
14+
from typing import Any, IO, TypedDict, Unpack, cast, Literal
15+
16+
from diracx.core.models import SearchSpec
17+
18+
19+
class ResponseExtra(TypedDict, total=False):
20+
content_type: str
21+
headers: dict[str, str]
22+
params: dict[str, str]
23+
cls: Any
24+
25+
26+
class SearchBody(TypedDict, total=False):
27+
parameters: list[str] | None
28+
search: list[SearchSpec] | None
29+
sort: list[str] | None
30+
31+
32+
class SearchExtra(ResponseExtra, total=False):
33+
page: int
34+
per_page: int
35+
36+
37+
class SearchKwargs(SearchBody, SearchExtra): ...
38+
39+
40+
class UnderlyingSearchArgs(ResponseExtra, total=False):
41+
# FIXME: The autorest-generated has a bug that it expected IO[bytes] despite
42+
# the code being generated to support IO[bytes] | bytes.
43+
body: IO[bytes]
44+
45+
46+
def make_search_body(**kwargs: Unpack[SearchKwargs]) -> UnderlyingSearchArgs:
47+
body: SearchBody = {}
48+
for key in SearchBody.__optional_keys__:
49+
if key not in kwargs:
50+
continue
51+
key = cast(Literal["parameters", "search", "sort"], key)
52+
value = kwargs.pop(key)
53+
if value is not None:
54+
body[key] = value
55+
result: UnderlyingSearchArgs = {"body": BytesIO(json.dumps(body).encode("utf-8"))}
56+
result.update(cast(SearchExtra, kwargs))
57+
return result
58+
59+
60+
class SummaryBody(TypedDict, total=False):
61+
grouping: list[str]
62+
search: list[str]
63+
64+
65+
class SummaryKwargs(SummaryBody, ResponseExtra): ...
66+
67+
68+
class UnderlyingSummaryArgs(ResponseExtra, total=False):
69+
# FIXME: The autorest-generated has a bug that it expected IO[bytes] despite
70+
# the code being generated to support IO[bytes] | bytes.
71+
body: IO[bytes]
72+
73+
74+
def make_summary_body(**kwargs: Unpack[SummaryKwargs]) -> UnderlyingSummaryArgs:
75+
body: SummaryBody = {}
76+
for key in SummaryBody.__optional_keys__:
77+
if key not in kwargs:
78+
continue
79+
key = cast(Literal["grouping", "search"], key)
80+
value = kwargs.pop(key)
81+
if value is not None:
82+
body[key] = value
83+
result: UnderlyingSummaryArgs = {"body": BytesIO(json.dumps(body).encode("utf-8"))}
84+
result.update(cast(ResponseExtra, kwargs))
85+
return result
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Patches for the autorest-generated pilots client.
2+
3+
This file can be used to customize the generated code for the pilots client.
4+
When adding new classes to this file, make sure to also add them to the
5+
__all__ list in the corresponding file in the patches directory.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
__all__ = [
11+
"PilotsOperations",
12+
]
13+
14+
from typing import Any, Unpack
15+
16+
from azure.core.tracing.decorator import distributed_trace
17+
18+
from ..._generated.operations._operations import PilotsOperations as _PilotsOperations
19+
from .common import make_search_body, make_summary_body, SearchKwargs, SummaryKwargs
20+
21+
# We're intentionally ignoring overrides here because we want to change the interface.
22+
# mypy: disable-error-code=override
23+
24+
25+
class PilotsOperations(_PilotsOperations):
26+
@distributed_trace
27+
def search(self, **kwargs: Unpack[SearchKwargs]) -> list[dict[str, Any]]:
28+
"""TODO"""
29+
return super().search(**make_search_body(**kwargs))
30+
31+
@distributed_trace
32+
def summary(self, **kwargs: Unpack[SummaryKwargs]) -> list[dict[str, Any]]:
33+
"""TODO"""
34+
return super().summary(**make_summary_body(**kwargs))

0 commit comments

Comments
 (0)