Skip to content

Commit 7587ffd

Browse files
authored
Merge pull request #4 from imagekit-developer/improvement
Improvement
2 parents 2c1734a + 0658b37 commit 7587ffd

File tree

11 files changed

+242
-101
lines changed

11 files changed

+242
-101
lines changed

README.md

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ The usage of the SDK has been explained below
5151

5252
This method allows you to create a URL using the path where the image exists and the URL
5353
endpoint(url_endpoint) you want to use to access the image. You can refer to the documentation
54-
[here](https://docs.imagekit.io/imagekit-docs/url-endpoints) to read more about URL endpoints
55-
in ImageKit and the section about [image origins](https://docs.imagekit.io/imagekit-docs/configure-origin) to understand
54+
[here](https://docs.imagekit.io/integration/url-endpoints) to read more about URL endpoints
55+
in ImageKit and the section about [image origins](https://docs.imagekit.io/integration/configure-origin) to understand
5656
about paths with different kinds of origins.
5757

5858

@@ -100,7 +100,7 @@ The ```.url()``` method accepts the following parameters
100100
| url_endpoint | Optional. The base URL to be appended before the path of the image. If not specified, the URL Endpoint specified at the time of SDK initialization is used. For example, https://ik.imagekit.io/your_imagekit_id/endpoint/ |
101101
| path | Conditional. This is the path at which the image exists. For example, `/path/to/image.jpg`. Either the `path` or `src` parameter need to be specified for URL generation. |
102102
| src | Conditional. This is the complete URL of an image already mapped to ImageKit. For example, `https://ik.imagekit.io/your_imagekit_id/endpoint/path/to/image.jpg`. Either the `path` or `src` parameter need to be specified for URL generation. |
103-
| transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a [chained transformation](https://docs.imagekit.io/imagekit-docs/chained-transformations) can be specified as different objects of the array. The complete list of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
103+
| transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a [chained transformation](https://docs.imagekit.io/features/image-transformations/chained-transformations) can be specified as different objects of the array. The complete list of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
104104
| transformation_position | Optional. Default value is `path` that places the transformation string as a path parameter in the URL. Can also be specified as `query` which adds the transformation string as the query parameter `tr` in the URL. If you use `src` parameter to create the URL, then the transformation string is always added as a query parameter. |
105105
| query_parameters | Optional. These are the other query parameters that you want to add to the final URL. These can be any query parameters and not necessarily related to ImageKit. Especially useful, if you want to add some versioning parameter to your URLs. |
106106
| signed | Optional. Boolean. Default is `false`. If set to `true`, the SDK generates a signed image URL adding the image signature to the image URL. This can only be used if you are creating the URL with the `url_endpoint` and `path` parameters, and not with the `src` parameter. |
@@ -133,7 +133,7 @@ https://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg?tr=h-300%2Cw-
133133

134134
**2. Sharpening and contrast transforms and a progressive JPG image**
135135

136-
There are some transforms like [Sharpening](https://docs.imagekit.io/imagekit-docs/image-enhancement---color-manipulation)
136+
There are some transforms like [Sharpening](https://docs.imagekit.io/features/image-transformations/image-enhancement-and-color-manipulation)
137137
that can be added to the URL with or without any other value. To use such transforms without specifying a value, specify
138138
the value as "-" in the transformation object, otherwise, specify the value that you want to be
139139
added to this transformation.
@@ -179,7 +179,7 @@ https://ik.imagekit.io/your_imagekit_id/tr:h-300,w-400/default-image.jpg?v=123&i
179179

180180
**List of transformations**
181181

182-
The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/imagekit-docs/image-transformations).
182+
The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/features/image-transformations/resize-crop-and-other-transformations).
183183
The SDK gives a name to each transformation parameter, making the code simpler, making the code simpler and readable.
184184
If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, then use the
185185
transformation code from ImageKit docs as the name when using in the ```url``` function.
@@ -232,13 +232,13 @@ transformation code from ImageKit docs as the name when using in the ```url``` f
232232
## File Upload
233233

234234
The SDK provides a simple interface using the `.upload()` method to upload files to the ImageKit Media library. It
235-
accepts all the parameters supported by the [ImageKit Upload API](https://docs.imagekit.io/imagekit-docs/server-side-file-upload).
235+
accepts all the parameters supported by the [ImageKit Upload API](https://docs.imagekit.io/api-reference/upload-file-api/server-side-file-upload).
236236

237237
The `upload()` method requires at least the `file` and the `file_name` parameter to upload a file and returns
238238
a callback with the `error` and `result` as arguments. You can pass other parameters supported by the
239239
ImageKit upload API using the same parameter name as specified in the upload API documentation. For example, to
240240
specify tags for a file at the time of upload use the tags parameter as specified in the
241-
[documentation here](https://docs.imagekit.io/imagekit-docs/server-side-file-upload)
241+
[documentation here](https://docs.imagekit.io/media-library/overview/image-tags)
242242

243243

244244
Simple usage
@@ -427,13 +427,13 @@ be `None`
427427

428428
## File Management
429429

430-
The SDK provides a simple interface for all the [media APIs mentioned here](https://docs.imagekit.io/imagekit-docs/media-api)
430+
The SDK provides a simple interface for all the [media APIs mentioned here](https://docs.imagekit.io/api-reference/media-api)
431431
to manage your files. This also returns `error` and `result`, error will be `None` if API succeeds.
432432

433433
**1. List & Search Files**
434434

435435
Accepts an object specifying the parameters to be used to list and search files. All parameters specified
436-
in the [documentation here](https://docs.imagekit.io/imagekit-docs/list-and-search-files-api) can be passed as is with the
436+
in the [documentation here](https://docs.imagekit.io/api-reference/media-api/list-and-search-files#list-and-search-file-api) can be passed as is with the
437437
correct values to get the results.
438438

439439
```python
@@ -444,20 +444,28 @@ correct values to get the results.
444444
)
445445
```
446446
**2. Get File Details**
447-
Accepts the file ID and fetches the details as per the [API documentation here](https://docs.imagekit.io/imagekit-docs/get-file-details-api)
447+
Accepts the file ID and fetches the details as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/get-file-details)
448448

449449
```python
450450
imagekit.get_file_details(file_id)
451451
```
452452

453453
**3. Get File Metadata**
454-
Accepts the file ID and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/imagekit-docs)
454+
Accepts the file ID and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/api-reference/metadata-api/get-image-metadata-for-uploaded-media-files)
455455
```python
456456
imagekit.get_metadata(file_id)
457457
```
458458

459+
460+
**3. Get File Metadata from remote url**
461+
Accepts the remote file url and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/api-reference/metadata-api/get-image-metadata-from-remote-url)
462+
463+
```python
464+
imagekit.get_remote_url_metadata(remote_file_url)
465+
```
466+
459467
**4. Update File Details**
460-
Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/imagekit-docs).
468+
Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/update-file-details).
461469
The first argument to the `update_field_details` method is the file ID and the second argument is an object with the
462470
parameters to be updated.
463471

@@ -469,23 +477,31 @@ imagekit.update_file_details("file_id", {
469477
```
470478

471479
**6. Delete File**
472-
Delete a file as per the [API documentation here](https://docs.imagekit.io/imagekit-docs/delete-file-api). The method accepts the file ID of the file that has to be
480+
Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-file). The method accepts the file ID of the file that has to be
473481
deleted.
474482

475483
```python
476484
imagekit.delete_file(file_id)
477485
```
478486

487+
**6. Bulk File Delete by IDs**
488+
Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-files-bulk). The method accepts list of file IDs of files that has to be
489+
deleted.
490+
491+
```python
492+
imagekit.bulk_delete(["file_id1", "file_id2"])
493+
```
494+
479495
**6. Purge Cache**
480-
Programmatically issue a cache clear request as clear request as pet the [API documentation here](https://docs.imagekit.io/imagekit-docs/purge-cache-api).
496+
Programmatically issue a cache clear request as clear request as pet the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache).
481497
Accepts the full URL of the file for which the cache has to be cleared.
482498
```python
483499
imagekit.purge_cache(full_url)
484500
```
485501
**7. Purge Cache Status**
486502

487503
Get the purge cache request status using the request ID returned when a purge cache request gets submitted as pet the
488-
[API documentation here](https://docs.imagekit.io/imagekit-docs/purge-cache-status-api)
504+
[API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache-status)
489505

490506
```python
491507
imagekit.get_purge_cache_status(cache_request_id)
@@ -525,7 +541,7 @@ signature are always returned in the response, no matter if they are provided as
525541
**Distance calculation between two pHash values**
526542

527543
Perceptual hashing allows you to constructing a has value that uniquely identifies an input image based on the contents
528-
of an image. [imagekit.io metadata API](https://docs.imagekit.io/imagekit-docs/metadata-api) returns the pHash
544+
of an image. [imagekit.io metadata API](https://docs.imagekit.io/api-reference/metadata-api) returns the pHash
529545
value of an image in the response. You can use this value to find duplicate, near duplicate(similar) image by calculating
530546
distance between the two images.
531547

@@ -569,7 +585,7 @@ For any feedback or to report any issues or general implementation support pleas
569585

570586
## Links
571587

572-
* [Documentation](https://docs.imagekit.io/imagekit-docs)
588+
* [Documentation](https://docs.imagekit.io/)
573589

574590
* [Main Website](https://imagekit.io/)
575591

imagekitio/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ImageKit(object):
1212
Main Class What user will use by creating
1313
instance
1414
"""
15+
1516
def __init__(
1617
self,
1718
public_key=None,
@@ -51,6 +52,11 @@ def delete_file(self, file_id: str = None) -> Dict[str, Any]:
5152
"""
5253
return self.file.delete(file_id)
5354

55+
def bulk_delete(self, file_ids: list = None):
56+
"""Delete files in bulk by provided list of ids
57+
"""
58+
return self.file.batch_delete(file_ids)
59+
5460
def purge_cache(self, file_url: str = None) -> Dict[str, Any]:
5561
"""Purge Cache from server by file url
5662
"""
@@ -66,6 +72,9 @@ def get_metadata(self, file_id: str = None) -> Dict[str, Any]:
6672
"""
6773
return self.file.get_metadata(str(file_id))
6874

75+
def get_remote_url_metadata(self, remote_file_url: str = ""):
76+
return self.file.get_metadata_from_remote_url(remote_file_url)
77+
6978
def url(self, options: Dict[str, Any]) -> str:
7079
"""Get generated Url from options parameter
7180
"""

imagekitio/constants/defaults.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ class Default(enum.Enum):
99
QUERY_TRANSFORMATION_POSITION,
1010
]
1111
DEFAULT_TIMESTAMP = "9999999999"
12+
SDK_VERSION = "python-1.0.0"

imagekitio/constants/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
"is_private_file",
2020
"custom_coordinates",
2121
"response_fields",
22-
"metadata"
22+
"metadata",
2323
]

imagekitio/constants/url.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
class URL(Enum):
55
BASE_URL = "https://api.imagekit.io/v1/files"
66
PURGE_CACHE = "/purge"
7-
UPLOAD = "/upload"
7+
UPLOAD_URL = "https://upload.imagekit.io/api/v1/files/upload"
8+
BULK_FILE_DELETE = "/batch/deleteByFileIds"
9+
REMOTE_METADATA_FULL_URL = "https://api.imagekit.io/v1/metadata"

imagekitio/file.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def upload(self, file, file_name, options) -> Dict:
2626
raise TypeError(ERRORS.MISSING_UPLOAD_FILE_PARAMETER.value)
2727
if not file_name:
2828
raise TypeError(ERRORS.MISSING_UPLOAD_FILENAME_PARAMETER.value)
29-
url = URL.BASE_URL.value + URL.UPLOAD.value
29+
url = URL.UPLOAD_URL.value
3030
headers = self.request.create_headers()
3131

3232
files = {
@@ -142,12 +142,36 @@ def delete(self, file_id: str = None) -> Dict:
142142
response = {"error": error, "response": response}
143143
return response
144144

145+
def batch_delete(self, file_ids: list = None):
146+
"""Delete bulk files
147+
Delete files by batch ids
148+
"""
149+
if not file_ids:
150+
raise ValueError("Need to pass ids in list")
151+
url = URL.BASE_URL.value + URL.BULK_FILE_DELETE.value
152+
resp = self.request.request(
153+
method="POST",
154+
url=url,
155+
headers=self.request.create_headers(),
156+
data={"fileIds": file_ids},
157+
)
158+
159+
if resp.status_code > 204:
160+
error = resp.text
161+
response = None
162+
else:
163+
error = None
164+
response = None
165+
166+
response = {"error": error, "response": response}
167+
return response
168+
145169
def purge_cache(self, file_url: str = None) -> Dict[str, Any]:
146170
"""Use from child class to purge cache
147171
"""
148172
if not file_url:
149173
raise TypeError(ERRORS.MISSING_FILE_URL.value)
150-
url = URL.BASE_URL.value + "/purge"
174+
url = URL.BASE_URL.value + URL.PURGE_CACHE.value
151175
headers = {"Content-Type": "application/json"}
152176
headers.update(self.request.get_auth_headers())
153177
body = {"url": file_url}
@@ -195,11 +219,29 @@ def get_metadata(self, file_id: str = None):
195219
resp = self.request.request("GET", url, headers=self.request.create_headers())
196220
formatted_resp = camel_dict_to_snake_dict(resp.json())
197221
if resp.status_code > 200:
198-
error = formatted_resp
222+
error = resp.json()
199223
response = None
200224
else:
201225
error = None
202-
response = formatted_resp
226+
response = resp.json()
227+
response = {"error": error, "response": response}
228+
return response
229+
230+
def get_metadata_from_remote_url(self, remote_file_url: str):
231+
if not remote_file_url:
232+
raise ValueError("You must provide remote url")
233+
url = URL.REMOTE_METADATA_FULL_URL.value
234+
param = {"url": remote_file_url}
235+
resp = self.request.request(
236+
"GET", url, headers=self.request.create_headers(), params=param
237+
)
238+
239+
if resp.status_code > 204:
240+
error = resp.json()
241+
response = None
242+
else:
243+
error = None
244+
response = resp.json()
203245
response = {"error": error, "response": response}
204246
return response
205247

imagekitio/url.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def build_url(self, options: dict) -> str:
128128
generated_url = urlunparse(
129129
result_url_dict.get(f, "") for f in ParseResult._fields
130130
)
131+
if result_url_dict["query"]:
132+
generated_url = generated_url + "&sdk-version=" + Default.SDK_VERSION.value
133+
else:
134+
generated_url = generated_url + "?sdk-version=" + Default.SDK_VERSION.value
131135
return generated_url
132136

133137
@staticmethod

0 commit comments

Comments
 (0)