11
22from datetime import datetime
3+ import json
34
45from . import pagination
56
@@ -105,8 +106,12 @@ def analyze(self, analyzers, notification_url=None):
105106 r = self ._client (
106107 'post' ,
107108 f'assets/{ self .uid } /analyze' ,
108- params = {'notification_url' : notification_url },
109- json_type_body = [a .to_json_type () for a in analyzers ]
109+ data = {
110+ 'analyzers' : json .dumps ([
111+ a .to_json_type () for a in analyzers
112+ ]),
113+ 'notification_url' : notification_url
114+ }
110115 )
111116
112117 if not notification_url :
@@ -183,6 +188,37 @@ def all(self, client, secure=None, type=None, q=None, rate_buffer=0):
183188
184189 return assets
185190
191+ @classmethod
192+ def analyze_many (
193+ cls ,
194+ client ,
195+ uids ,
196+ analyzers ,
197+ local = False ,
198+ notification_url = None
199+ ):
200+ """Analyze one or more assets"""
201+
202+ if local :
203+ analyzers_json = json .dumps ({
204+ uid : [a .to_json_type () for a in local_analyzers ]
205+ for uid , local_analyzers in analyzers .items ()
206+ })
207+
208+ else :
209+ analyzers_json = json .dumps ([a .to_json_type () for a in analyzers ])
210+
211+ return client (
212+ 'post' ,
213+ f'assets/analyze' ,
214+ data = {
215+ 'analyzers' : analyzers_json ,
216+ 'local' : True if local else None ,
217+ 'notification_url' : notification_url ,
218+ 'uids' : uids
219+ }
220+ )
221+
186222 @classmethod
187223 def create (cls , client , file , name = None , expire = None , secure = False ):
188224 """Upload an asset to Hangar51"""
@@ -200,6 +236,25 @@ def create(cls, client, file, name=None, expire=None, secure=False):
200236 )
201237 )
202238
239+ @classmethod
240+ def expire_many (cls , client , uids , seconds ):
241+ """
242+ Find one or more assets matching the given uids and set them to
243+ persist (remove the expires time).
244+ """
245+
246+ if isinstance (seconds , datetime ):
247+ seconds = datetime .timestamp () - time .time ()
248+
249+ return client (
250+ 'post' ,
251+ 'assets/expire' ,
252+ data = {
253+ 'seconds' : seconds ,
254+ 'uids' : uids
255+ }
256+ )
257+
203258 @classmethod
204259 def many (
205260 cls ,
@@ -237,6 +292,17 @@ def one(cls, client, uid):
237292 """Return an asset matching the given uid"""
238293 return cls (client , client ('get' , f'assets/{ uid } ' ))
239294
295+ @classmethod
296+ def persist_many (cls , client , uids ):
297+ """
298+ Find one or more assets matching the given uids and set them to
299+ persist (remove the expires time).
300+ """
301+ return client (
302+ 'post' ,
303+ 'assets/persist' ,
304+ data = {'uids' : uids }
305+ )
240306
241307class Variation (_BaseResource ):
242308 """
@@ -283,10 +349,12 @@ def create(cls, asset, variations, notification_url=None):
283349 r = asset ._client (
284350 'put' ,
285351 f'assets/{ asset .uid } /variations' ,
286- params = {'notification_url' : notification_url },
287- json_type_body = {
288- name : [t .to_json_type () for t in transforms ]
289- for name , transforms in variations .items ()
352+ data = {
353+ 'notification_url' : notification_url ,
354+ 'variations' : json .dumps ({
355+ name : [t .to_json_type () for t in transforms ]
356+ for name , transforms in variations .items ()
357+ }),
290358 }
291359 )
292360
@@ -295,3 +363,43 @@ def create(cls, asset, variations, notification_url=None):
295363 n : cls (asset ._client , asset , n , v )
296364 for n , v in r ['variations' ].items ()
297365 }
366+
367+ @classmethod
368+ def create_many (
369+ cls ,
370+ client ,
371+ uids ,
372+ variations ,
373+ local = False ,
374+ notification_url = None
375+ ):
376+ """
377+ Find one or more assets matching the given uids and create a set of
378+ variations for them.
379+ """
380+
381+ if local :
382+ variations_json = json .dumps ({
383+ uid : {
384+ name : [t .to_json_type () for t in transforms ]
385+ for name , transforms in local_variations .items ()
386+ }
387+ for uid , local_variations in variations .items ()
388+ })
389+
390+ else :
391+ variations_json = json .dumps ({
392+ name : [t .to_json_type () for t in transforms ]
393+ for name , transforms in variations .items ()
394+ })
395+
396+ return client (
397+ 'put' ,
398+ f'assets/transform' ,
399+ data = {
400+ 'local' : True if local else None ,
401+ 'notification_url' : notification_url ,
402+ 'uids' : uids ,
403+ 'variations' : variations_json
404+ }
405+ )
0 commit comments