Skip to content

Commit 880d83f

Browse files
committed
added changes for tags to pass as an string or array and added test for array
1 parent d1b1f83 commit 880d83f

4 files changed

Lines changed: 125 additions & 1 deletion

File tree

imagekitio/file.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,18 @@ def list(self, options: ListAndSearchFileRequestOptions = None) -> ListFileResul
118118
:return: ListFileResult
119119
"""
120120
if options is not None:
121+
if 'tags' in options.__dict__ and isinstance(options.tags, list):
122+
val = ", ".join(options.tags)
123+
if val:
124+
options.tags = val
121125
formatted_options = request_formatter(options.__dict__)
122126
if not self.is_valid_list_options(formatted_options):
123127
raise ValueError("Invalid option for list_files")
124128
else:
125129
formatted_options = dict()
126130
url = "{}/v1/files".format(URL.API_BASE_URL)
127131
headers = self.request.create_headers()
132+
print("Forma:-->", formatted_options)
128133
resp = self.request.request(
129134
method="GET", url=url, headers=headers, params=dumps(formatted_options)
130135
)

imagekitio/models/ListAndSearchFileRequestOptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import array
2+
3+
14
class ListAndSearchFileRequestOptions:
25
def __init__(
36
self,
@@ -8,7 +11,7 @@ def __init__(
811
file_type: str = None,
912
limit: int = None,
1013
skip: int = None,
11-
tags: str = None,
14+
tags=None,
1215
):
1316
if type is not None:
1417
self.type = type

tests/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ def setUp(self, mock_file, mock_req):
3232
skip=0,
3333
tags="Tag-1, Tag-2, Tag-3",
3434
)
35+
self.opt = ListAndSearchFileRequestOptions(
36+
type="file",
37+
sort="ASC_CREATED",
38+
path="/",
39+
search_query="created_at >= '2d' OR size < '2mb' OR format='png'",
40+
file_type="all",
41+
limit=1,
42+
skip=0,
43+
tags=["Tag-1", "Tag-2", "Tag-3"],
44+
)
3545
self.client = ImageKit(
3646
public_key="fake122",
3747
private_key=ClientTestCase.private_key,

tests/test_files_ops.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,112 @@ def test_list_files_fails_on_unauthenticated_request(self) -> None:
350350
self.assertEqual("Your account cannot be authenticated.", e.message)
351351
self.assertEqual(403, e.response_metadata.http_status_code)
352352

353+
@responses.activate
354+
def test_list_files_succeeds_with_basic_request_tags_with_array(self) -> None:
355+
"""
356+
Tests if list_files work with options which contains type, sort, path, searchQuery, fileType, limit, skip and tags
357+
"""
358+
359+
URL.API_BASE_URL = "http://test.com"
360+
url = "{}/v1/files".format(URL.API_BASE_URL)
361+
362+
headers = create_headers_for_test()
363+
responses.add(
364+
responses.GET,
365+
url,
366+
body="""[{
367+
"type": "file",
368+
"name": "sample-cat-image_gr64HPlJS.jpg",
369+
"createdAt": "2022-06-15T08:19:00.843Z",
370+
"updatedAt": "2022-06-15T08:19:45.169Z",
371+
"fileId": "62a995f4d875ec08dc587b72",
372+
"tags": ["{Tag_1", " Tag_2", " Tag_3}", "tag-to-add-2"],
373+
"AITags": "",
374+
"versionInfo": {
375+
"id": "62a995f4d875ec08dc587b72",
376+
"name": "Version 1"
377+
},
378+
"embeddedMetadata": {
379+
"XResolution": 250,
380+
"YResolution": 250,
381+
"DateCreated": "2022-06-15T08:19:01.523Z",
382+
"DateTimeCreated": "2022-06-15T08:19:01.524Z"
383+
},
384+
"customCoordinates": "10,10,20,20",
385+
"customMetadata": {
386+
"test100": 10
387+
},
388+
"isPrivateFile": false,
389+
"url": "https://ik.imagekit.io/your_imagekit_id/sample-cat-image_gr64HPlJS.jpg",
390+
"thumbnail": "https://ik.imagekit.io/your_imagekit_id/tr:n-ik_ml_thumbnail/sample-cat-image_gr64HPlJS.jpg",
391+
"fileType": "image",
392+
"filePath": "/sample-cat-image_gr64HPlJS.jpg",
393+
"height": 354,
394+
"width": 236,
395+
"size": 23023,
396+
"hasAlpha": false,
397+
"mime": "image/jpeg"
398+
}]""",
399+
headers=headers,
400+
match=[
401+
matchers.query_string_matcher(
402+
"%7B%22type%22:%20%22file%22,%20%22sort%22:%20%22ASC_CREATED%22,%20%22path%22:%20%22/%22,%20%22searchQuery%22:%20%22created_at%20%3E=%20'2d'%20OR%20size%20%3C%20'2mb'%20OR%20format='png'%22,%20%22fileType%22:%20%22all%22,%20%22limit%22:%201,%20%22skip%22:%200,%20%22tags%22:%20%22Tag-1,%20Tag-2,%20Tag-3%22%7D"
403+
)
404+
],
405+
)
406+
407+
resp = self.client.list_files(self.opt)
408+
409+
mock_response_metadata = {
410+
"headers": {
411+
"Content-Type": "text/plain",
412+
"Accept-Encoding": "gzip, deflate",
413+
"Authorization": "Basic ZmFrZTEyMjo=",
414+
},
415+
"httpStatusCode": 200,
416+
"raw": [
417+
{
418+
"AITags": "",
419+
"createdAt": "2022-06-15T08:19:00.843Z",
420+
"customCoordinates": "10,10,20,20",
421+
"customMetadata": {"test100": 10},
422+
"embeddedMetadata": {
423+
"DateCreated": "2022-06-15T08:19:01.523Z",
424+
"DateTimeCreated": "2022-06-15T08:19:01.524Z",
425+
"XResolution": 250,
426+
"YResolution": 250,
427+
},
428+
"fileId": "62a995f4d875ec08dc587b72",
429+
"filePath": "/sample-cat-image_gr64HPlJS.jpg",
430+
"fileType": "image",
431+
"hasAlpha": False,
432+
"height": 354,
433+
"isPrivateFile": False,
434+
"mime": "image/jpeg",
435+
"name": "sample-cat-image_gr64HPlJS.jpg",
436+
"size": 23023,
437+
"tags": ["{Tag_1", " Tag_2", " Tag_3}", "tag-to-add-2"],
438+
"thumbnail": "https://ik.imagekit.io/your_imagekit_id/tr:n-ik_ml_thumbnail/sample-cat-image_gr64HPlJS.jpg",
439+
"type": "file",
440+
"updatedAt": "2022-06-15T08:19:45.169Z",
441+
"url": "https://ik.imagekit.io/your_imagekit_id/sample-cat-image_gr64HPlJS.jpg",
442+
"versionInfo": {
443+
"id": "62a995f4d875ec08dc587b72",
444+
"name": "Version " "1",
445+
},
446+
"width": 236,
447+
}
448+
],
449+
}
450+
self.assertEqual(
451+
"http://test.com/v1/files?%7B%22type%22:%20%22file%22,%20%22sort%22:%20%22ASC_CREATED%22,%20%22path%22:%20%22/%22,%20%22searchQuery%22:%20%22created_at%20%3E=%20'2d'%20OR%20size%20%3C%20'2mb'%20OR%20format='png'%22,%20%22fileType%22:%20%22all%22,%20%22limit%22:%201,%20%22skip%22:%200,%20%22tags%22:%20%22Tag-1,%20Tag-2,%20Tag-3%22%7D",
452+
responses.calls[0].request.url,
453+
)
454+
self.assertEqual(
455+
camel_dict_to_snake_dict(mock_response_metadata),
456+
resp.response_metadata.__dict__,
457+
)
458+
353459
@responses.activate
354460
def test_list_files_succeeds_with_basic_request(self) -> None:
355461
"""

0 commit comments

Comments
 (0)