Skip to content

Commit 171c19e

Browse files
committed
Updated to reflect support for new endpoints
1 parent 1915304 commit 171c19e

File tree

5 files changed

+170
-20
lines changed

5 files changed

+170
-20
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ with open('image.bmp') as f:
2929

3030
# Analyze the image asset to find its dominant colours and focal point
3131
asset.analyze([
32-
h51.transforms.images.DominantColours(),
32+
h51.analyzers.images.DominantColors(),
3333
h51.analyzers.images.FocalPoint()
3434
])
3535

@@ -41,15 +41,15 @@ h51.resources.Variation.create(
4141
h51.transforms.images.AutoOrient(),
4242
h51.transforms.images.FocalPointCrop(aspect_ratio=0.5),
4343
h51.transforms.images.Fit(640, 640),
44-
h51.transforms.images.Output('WEBP')
44+
h51.transforms.images.Output('WebP')
4545
],
4646
'x2': [
4747
h51.transforms.images.AutoOrient(),
4848
h51.transforms.images.FocalPointCrop(aspect_ratio=0.5),
4949
h51.transforms.images.Fit(1280, 1280),
50-
h51.transforms.images.Output('WEBP')
50+
h51.transforms.images.Output('WebP')
5151
]
5252
}
53-
])
53+
)
5454

5555
```

h51/client.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def __call__(self,
6161
path,
6262
params=None,
6363
data=None,
64-
json_type_body=None,
6564
files=None,
6665
download=False
6766
):
@@ -70,9 +69,6 @@ def __call__(self,
7069
# Build headers
7170
headers = {'X-H51-APIKey': self._api_key}
7271

73-
if json_type_body:
74-
headers['Content-Type'] = 'application/json'
75-
7672
if not download:
7773
headers['Accept'] = 'application/json'
7874

@@ -90,7 +86,6 @@ def __call__(self,
9086
headers=headers,
9187
params=params,
9288
data=data,
93-
json=json_type_body,
9489
files=files,
9590
timeout=self._timeout
9691
)
@@ -120,10 +115,7 @@ def __call__(self,
120115
try:
121116
error = r.json()
122117

123-
except json.decoder.JSONDecodeError:
124-
error = {}
125-
126-
if not isinstance(error, dict):
118+
except ValueError:
127119
error = {}
128120

129121
error_cls = exceptions.H51Exception.get_class_by_status_code(

h51/resources.py

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
from datetime import datetime
3+
import json
34

45
from . 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

241307
class 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+
)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Versions should comply with PEP440. For a discussion on single-sourcing
2222
# the version across setup.py and the project code, see
2323
# https://packaging.python.org/en/latest/single_source_version.html
24-
version='0.0.4',
24+
version='0.0.5',
2525
description=\
2626
'The H51 Python library provides a pythonic interface to the H51 API.',
2727
long_description=long_description,

test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import h51
2+
3+
4+
client = h51.Client(
5+
'gY4uNgniLM6gps60dyYKDKklBVlb7zh8xUqI9ZBp2aLd4KOsEeQrrfNViPiQ_0Hbcu_frZ8BbwMOaWPfnpTaxGAaD93_BAr6nBA6uw6Id0Cgm4OCcG5MDi93aXMjsBkC0oYd9_sED_WaJLrg3S83A0RXd2sDqQobmKwZIGu5AHM',
6+
api_base_url='http://api.h51.local'
7+
)
8+
9+
asset = h51.resources.Asset.one(client, '3owuun')
10+
11+
# r = h51.resources.Variation.create_many(
12+
# client,
13+
# ['3owuun', 'xvr8dj'],
14+
# {
15+
# '3owuun': {
16+
# 'x1': [
17+
# h51.transforms.images.AutoOrient(),
18+
# h51.transforms.images.FocalPointCrop(aspect_ratio=0.5),
19+
# h51.transforms.images.Fit(640, 640),
20+
# h51.transforms.images.Output('WebP')
21+
# ]
22+
# },
23+
# 'xvr8dj': {
24+
# 'x1': [
25+
# h51.transforms.images.AutoOrient(),
26+
# h51.transforms.images.FocalPointCrop(aspect_ratio=0.5),
27+
# h51.transforms.images.Fit(640, 640),
28+
# h51.transforms.images.Output('JPEG')
29+
# ]
30+
# }
31+
# },
32+
# local=True
33+
# )
34+
35+
r = h51.resources.Asset.analyze_many(
36+
client,
37+
['3owuun', 'ayj6wc'],
38+
{
39+
'3owuun': [
40+
h51.analyzers.images.DominantColors(),
41+
h51.analyzers.images.FocalPoint()
42+
],
43+
'ayj6wc': [
44+
h51.analyzers.images.Animation()
45+
]
46+
},
47+
local=True
48+
)
49+
50+
print(r)

0 commit comments

Comments
 (0)