46
46
47
47
from servicex .models import TransformRequest , TransformStatus , CachedDataset
48
48
49
- T = TypeVar ('T' )
49
+ T = TypeVar ("T" )
50
50
51
- def requires_resource (resource_name : str ) -> Callable [[Callable [..., T ]], Callable [..., T ]]:
51
+
52
+ def requires_resource (
53
+ resource_name : str ,
54
+ ) -> Callable [[Callable [..., T ]], Callable [..., T ]]:
52
55
"""
53
56
Decorator to check if a specific API resource is available on the server before executing the method.
54
57
@@ -68,9 +71,10 @@ def decorator(func: Callable[..., T]) -> Callable[..., T]:
68
71
func_name = func .__name__
69
72
70
73
# Class-level cache for sync method resources
71
- sync_cache_key = f' _sync_resources_for_{ resource_name } '
74
+ sync_cache_key = f" _sync_resources_for_{ resource_name } "
72
75
73
76
if is_async :
77
+
74
78
@wraps (func )
75
79
async def async_wrapper (self , * args : Any , ** kwargs : Any ) -> T :
76
80
# Get resources and check availability in one operation
@@ -82,14 +86,15 @@ async def async_wrapper(self, *args: Any, **kwargs: Any) -> T:
82
86
83
87
return cast (Callable [..., T ], async_wrapper )
84
88
else :
89
+
85
90
@wraps (func )
86
91
def sync_wrapper (self , * args : Any , ** kwargs : Any ) -> T :
87
92
# Initialize class-level cache attributes if needed
88
93
cls = self .__class__
89
94
if not hasattr (cls , sync_cache_key ):
90
95
setattr (cls , sync_cache_key , (None , 0 )) # (resources, timestamp)
91
96
92
- cache_ttl = getattr (self , ' _resources_cache_ttl' , 300 )
97
+ cache_ttl = getattr (self , " _resources_cache_ttl" , 300 )
93
98
cached_resources , timestamp = getattr (cls , sync_cache_key )
94
99
current_time = time .time ()
95
100
@@ -117,8 +122,10 @@ def sync_wrapper(self, *args: Any, **kwargs: Any) -> T:
117
122
118
123
class ResourceNotAvailableError (Exception ):
119
124
"""Exception raised when a required resource is not available on the server."""
125
+
120
126
pass
121
127
128
+
122
129
class AuthorizationError (BaseException ):
123
130
pass
124
131
@@ -140,7 +147,7 @@ def __init__(self, url: str, refresh_token: Optional[str] = None):
140
147
141
148
self ._available_resources : Optional [Dict [str , Any ]] = None
142
149
self ._resources_last_updated : Optional [float ] = None
143
- self ._resources_cache_ttl = 60 * 5
150
+ self ._resources_cache_ttl = 60 * 5
144
151
145
152
async def get_resources (self ) -> Dict [str , Any ]:
146
153
"""
@@ -153,16 +160,18 @@ async def get_resources(self) -> Dict[str, Any]:
153
160
current_time = time .time ()
154
161
155
162
# Return cached resources if they exist and are not expired
156
- if (self ._available_resources is not None and
157
- self ._resources_last_updated is not None and
158
- current_time - self ._resources_last_updated < self ._resources_cache_ttl ):
163
+ if (
164
+ self ._available_resources is not None
165
+ and self ._resources_last_updated is not None
166
+ and current_time - self ._resources_last_updated < self ._resources_cache_ttl
167
+ ):
159
168
return self ._available_resources
160
169
161
170
# Fetch resources from server
162
171
headers = await self ._get_authorization ()
163
172
async with ClientSession () as session :
164
173
async with session .get (
165
- headers = headers , url = f"{ self .url } /servicex/resources"
174
+ headers = headers , url = f"{ self .url } /servicex/resources"
166
175
) as r :
167
176
if r .status == 403 :
168
177
raise AuthorizationError (
0 commit comments