Skip to content

Commit 1272740

Browse files
committed
feat: implement get_remote_url_metadata
1 parent cafc026 commit 1272740

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,14 @@ Accepts the file ID and fetches the metadata as per the [API documentation here]
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**
460468
Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/imagekit-docs).
461469
The first argument to the `update_field_details` method is the file ID and the second argument is an object with the

imagekitio/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def get_metadata(self, file_id: str = None) -> Dict[str, Any]:
7272
"""
7373
return self.file.get_metadata(str(file_id))
7474

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

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_PARAM = "python-1.0.0"

imagekitio/constants/url.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ class URL(Enum):
66
PURGE_CACHE = "/purge"
77
UPLOAD_URL = "https://upload.imagekit.io/api/v1/files/upload"
88
BULK_FILE_DELETE = "/batch/deleteByFileIds"
9+
REMOTE_METADATA_FULL_URL = "https://api.imagekit.io/v1/metadata"

imagekitio/file.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ def batch_delete(self, file_ids: list = None):
150150
raise ValueError("Need to pass ids in list")
151151
url = URL.BASE_URL.value + URL.BULK_FILE_DELETE.value
152152
resp = self.request.request(
153-
method="POST", url=url, headers=self.request.create_headers(), data={"fileIds": file_ids}
153+
method="POST",
154+
url=url,
155+
headers=self.request.create_headers(),
156+
data={"fileIds": file_ids},
154157
)
155158

156159
if resp.status_code > 204:
@@ -168,7 +171,7 @@ def purge_cache(self, file_url: str = None) -> Dict[str, Any]:
168171
"""
169172
if not file_url:
170173
raise TypeError(ERRORS.MISSING_FILE_URL.value)
171-
url = URL.BASE_URL.value + "/purge"
174+
url = URL.BASE_URL.value + URL.PURGE_CACHE.value
172175
headers = {"Content-Type": "application/json"}
173176
headers.update(self.request.get_auth_headers())
174177
body = {"url": file_url}
@@ -216,11 +219,29 @@ def get_metadata(self, file_id: str = None):
216219
resp = self.request.request("GET", url, headers=self.request.create_headers())
217220
formatted_resp = camel_dict_to_snake_dict(resp.json())
218221
if resp.status_code > 200:
219-
error = formatted_resp
222+
error = resp.json()
220223
response = None
221224
else:
222225
error = None
223-
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()
224245
response = {"error": error, "response": response}
225246
return response
226247

sample/sample.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@
9090
print("Signed url-", image_url, end="\n\n")
9191

9292
print("-------------------------------------")
93-
list_files = imagekit.list_files({"skip": 0, "limit": 2})
93+
list_files = imagekit.list_files({"skip": 0, "limit": 5})
9494
bulk_ids = [
95-
list_files["response"][0]["fileId"],
96-
list_files["response"][1]["fileId"],
95+
list_files["response"][3]["fileId"],
96+
list_files["response"][4]["fileId"],
9797
]
9898
print("List files-", "\n", list_files)
9999

@@ -185,5 +185,9 @@
185185

186186
print("-------------------------------------")
187187

188-
189188
print("Bulk File delete-", imagekit.bulk_delete(bulk_ids))
189+
190+
print("-----------------------------")
191+
192+
remote_file_url = upload["response"]["url"]
193+
print("Get metadata-", imagekit.get_remote_url_metadata(remote_file_url))

0 commit comments

Comments
 (0)