Skip to content

Commit

Permalink
Merge pull request #30 from amanforindia/api-implementation
Browse files Browse the repository at this point in the history
Api implementation
  • Loading branch information
imagekitio authored Sep 30, 2022
2 parents d0cd96b + 0904e9d commit 38e50cc
Show file tree
Hide file tree
Showing 85 changed files with 7,629 additions and 1,431 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ on:
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.6, 3.7, 3.8, 3.9, '3.10']
steps:
- uses: actions/checkout@v1
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: "3.x"
python-version: ${{ matrix.python }}
- name: Install Tox and any other packages
run: |
python -m pip install --upgrade pip
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.7, 3.8]

python: [3.6, 3.7, 3.8, 3.9, '3.10']
steps:
- uses: actions/checkout@v1
- name: Setup Python
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parts/
sdist/
var/
wheels/
sample/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
Expand Down
1,334 changes: 1,105 additions & 229 deletions README.md

Large diffs are not rendered by default.

234 changes: 182 additions & 52 deletions imagekitio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@

from .constants.errors import ERRORS
from .file import File
from .models.CopyFileRequestOptions import CopyFileRequestOptions
from .models.CopyFolderRequestOptions import CopyFolderRequestOptions
from .models.CreateCustomMetadataFieldsRequestOptions import (
CreateCustomMetadataFieldsRequestOptions,
)
from .models.CreateFolderRequestOptions import CreateFolderRequestOptions
from .models.DeleteFolderRequestOptions import DeleteFolderRequestOptions
from .models.ListAndSearchFileRequestOptions import ListAndSearchFileRequestOptions
from .models.MoveFileRequestOptions import MoveFileRequestOptions
from .models.MoveFolderRequestOptions import MoveFolderRequestOptions
from .models.RenameFileRequestOptions import RenameFileRequestOptions
from .models.UpdateCustomMetadataFieldsRequestOptions import (
UpdateCustomMetadataFieldsRequestOptions,
)
from .models.UpdateFileRequestOptions import UpdateFileRequestOptions
from .models.UploadFileRequestOptions import UploadFileRequestOptions
from .models.results.BulkDeleteFileResult import BulkDeleteFileResult
from .models.results.CustomMetadataFieldsResultWithResponseMetadata import (
CustomMetadataFieldsResultWithResponseMetadata,
)
from .models.results.FileResultWithResponseMetadata import (
FileResultWithResponseMetadata,
)
from .models.results.FolderResult import FolderResult
from .models.results.GetBulkJobStatusResult import GetBulkJobStatusResult
from .models.results.GetMetadataResult import GetMetadataResult
from .models.results.ListCustomMetadataFieldsResult import (
ListCustomMetadataFieldsResult,
)
from .models.results.ListFileResult import ListFileResult
from .models.results.PurgeCacheResult import PurgeCacheResult
from .models.results.PurgeCacheStatusResult import PurgeCacheStatusResult
from .models.results.RenameFileResult import RenameFileResult
from .models.results.ResponseMetadataResult import ResponseMetadataResult
from .models.results.TagsResult import TagsResult
from .models.results.UploadFileResult import UploadFileResult
from .resource import ImageKitRequest
from .url import Url
from .utils.calculation import get_authenticated_params, hamming_distance
Expand All @@ -27,96 +63,190 @@ def __init__(
self.file = File(self.ik_request)
self.url_obj = Url(self.ik_request)

def upload(self, file=None, file_name=None, options=None) -> Dict[str, Any]:
"""Provides upload functionality
"""
def upload(self, file=None, file_name=None, options=None) -> UploadFileResult:
"""Provides upload functionality"""
return self.file.upload(file, file_name, options)

def upload_file(self, file=None, file_name=None, options=None) -> Dict[str, Any]:
"""Provides upload functionality
"""
return self.file.upload(file, file_name, options)
def upload_file(
self, file=None, file_name=None, options: UploadFileRequestOptions = None
) -> UploadFileResult:
"""Provides upload functionality"""
return self.file.upload(
file, file_name, options if options is not None else None
)

def list_files(self, options: Dict) -> Dict:
"""Get list(filtered if given param) of images of client
"""
def list_files(
self, options: ListAndSearchFileRequestOptions = None
) -> ListFileResult:
"""Get list(filtered if given param) of images of client"""
return self.file.list(options)

def get_file_details(self, file_identifier: str = None) -> Dict:
"""Get file_detail by file_id or file_url
"""
return self.file.details(file_identifier)
def get_file_details(self, file_id: str = None) -> FileResultWithResponseMetadata:
"""Get file_detail by file_id or file_url"""
return self.file.details(file_id)

def get_file_versions(self, file_id: str = None) -> ListFileResult:
"""Get file_version by file_id or file_url"""
return self.file.get_file_versions(file_id)

def update_file_details(self, file_id: str, options: dict = None) -> Dict:
"""Update file detail by file id and options
"""
def get_file_version_details(
self, file_id: str = None, version_id: str = None
) -> FileResultWithResponseMetadata:
"""Get file_version details by file_id and version_id"""
return self.file.get_file_version_details(file_id, version_id)

def update_file_details(
self, file_id: str, options: UpdateFileRequestOptions = None
) -> FileResultWithResponseMetadata:
"""Update file details by file id and options"""
return self.file.update_file_details(file_id, options)

def delete_file(self, file_id: str = None) -> Dict[str, Any]:
"""Delete file by file_id
"""
def add_tags(self, file_ids, tags) -> TagsResult:
"""Add tags by file ids and tags"""
return self.file.manage_tags(file_ids, tags, "addTags")

def remove_tags(self, file_ids, tags) -> TagsResult:
"""Remove tags by file ids and tags"""
return self.file.manage_tags(file_ids, tags, "removeTags")

def remove_ai_tags(self, file_ids, ai_tags) -> TagsResult:
"""Remove AI tags by file ids and AI tags"""
return self.file.remove_ai_tags(file_ids, ai_tags)

def delete_file(self, file_id: str = None) -> ResponseMetadataResult:
"""Delete file by file_id"""
return self.file.delete(file_id)

def bulk_delete(self, file_ids: list = None):
"""Delete files in bulk by provided list of ids
"""
def delete_file_version(self, file_id, version_id) -> ResponseMetadataResult:
"""Delete file version by provided file id and version id"""
return self.file.delete_file_version(file_id, version_id)

def bulk_delete(self, file_ids: list = None) -> BulkDeleteFileResult:
"""Delete files in bulk by provided list of file ids"""
return self.file.batch_delete(file_ids)

def bulk_file_delete(self, file_ids: list = None):
"""Delete files in bulk by provided list of ids
"""
def bulk_file_delete(self, file_ids: list = None) -> BulkDeleteFileResult:
"""Delete files in bulk by provided list of file ids"""
return self.file.batch_delete(file_ids)

def purge_cache(self, file_url: str = None) -> Dict[str, Any]:
"""Purge Cache from server by file url
"""
def copy_file(
self, options: CopyFileRequestOptions = None
) -> ResponseMetadataResult:
"""Copy file by provided sourceFilePath, destinationPath and includeFileVersions as an options"""
return self.file.copy_file(options)

def move_file(
self, options: MoveFileRequestOptions = None
) -> ResponseMetadataResult:
"""Move file by provided sourceFilePath and destinationPath as an options"""
return self.file.move_file(options)

def rename_file(self, options: RenameFileRequestOptions = None) -> RenameFileResult:
"""Rename file by provided filePath, newFileName and purgeCache as an options"""
return self.file.rename_file(options)

def restore_file_version(
self, file_id, version_id
) -> FileResultWithResponseMetadata:
"""Restore file version by provided file id and version id"""
return self.file.restore_file_version(file_id, version_id)

def create_folder(
self, options: CreateFolderRequestOptions = None
) -> ResponseMetadataResult:
"""Create folder by provided folderName and parentFolderPath as an options"""
return self.file.create_folder(options)

def delete_folder(
self, options: DeleteFolderRequestOptions = None
) -> ResponseMetadataResult:
"""Delete folder by provided folderPath as an options"""
return self.file.delete_folder(options)

def copy_folder(self, options: CopyFolderRequestOptions = None) -> FolderResult:
"""Copy folder by provided sourceFolderPath, destinationPath and includeFileVersions as an options"""
return self.file.copy_folder(options)

def move_folder(self, options: MoveFolderRequestOptions = None) -> FolderResult:
"""Move folder by provided sourceFolderPath and destinationPath as an options"""
return self.file.move_folder(options)

def get_bulk_job_status(self, job_id) -> GetBulkJobStatusResult:
"""Get bulk job status by provided only jobId"""
return self.file.get_bulk_job_status(job_id)

def purge_cache(self, file_url: str = None) -> PurgeCacheResult:
"""Purge Cache from server by file url"""
return self.file.purge_cache(file_url)

def purge_file_cache(self, file_url: str = None) -> Dict[str, Any]:
"""Purge Cache from server by file url
"""
def purge_file_cache(self, file_url: str = None) -> PurgeCacheResult:
"""Purge Cache from server by file url"""
return self.file.purge_cache(file_url)

def get_purge_cache_status(self, purge_cache_id: str = "") -> Dict[str, Any]:
"""Get Purge Cache status by purge cache request_id
"""
def get_purge_cache_status(
self, purge_cache_id: str = ""
) -> PurgeCacheStatusResult:
"""Get Purge Cache status by purge cache request_id"""
return self.file.get_purge_cache_status(str(purge_cache_id))

def get_purge_file_cache_status(self, purge_cache_id: str = "") -> Dict[str, Any]:
"""Get Purge Cache status by purge cache request_id
"""
def get_purge_file_cache_status(
self, purge_cache_id: str = ""
) -> PurgeCacheStatusResult:
"""Get Purge Cache status by purge cache request_id"""
return self.file.get_purge_cache_status(str(purge_cache_id))

def get_metadata(self, file_id: str = None) -> Dict[str, Any]:
"""Get Meta Data of a file by file id
"""
def get_metadata(self, file_id: str = None) -> GetMetadataResult:
"""Get Meta Data of a file by file id"""
return self.file.get_metadata(str(file_id))

def get_file_metadata(self, file_id: str = None) -> Dict[str, Any]:
"""Get Meta Data of a file by file id
"""
def get_file_metadata(self, file_id: str = None) -> GetMetadataResult:
"""Get Meta Data of a file by file id"""
return self.file.get_metadata(str(file_id))

def get_remote_url_metadata(self, remote_file_url: str = ""):
def get_remote_url_metadata(self, remote_file_url: str = "") -> GetMetadataResult:
return self.file.get_metadata_from_remote_url(remote_file_url)

def get_remote_file_url_metadata(self, remote_file_url: str = ""):
def get_remote_file_url_metadata(
self, remote_file_url: str = ""
) -> GetMetadataResult:
"""Get remote metadata by provided remote_file_url"""
return self.file.get_metadata_from_remote_url(remote_file_url)

def create_custom_metadata_fields(
self, options: CreateCustomMetadataFieldsRequestOptions = None
) -> CustomMetadataFieldsResultWithResponseMetadata:
"""creates custom metadata fields by passing name, label and schema as an options"""
return self.file.create_custom_metadata_fields(options)

def get_custom_metadata_fields(
self, include_deleted: bool = False
) -> ListCustomMetadataFieldsResult:
"""get custom metadata fields"""
return self.file.get_custom_metadata_fields(include_deleted)

def update_custom_metadata_fields(
self, field_id, options: UpdateCustomMetadataFieldsRequestOptions = None
) -> CustomMetadataFieldsResultWithResponseMetadata:
"""updates custom metadata fields by passing id of custom metadata field and params as an options"""
return self.file.update_custom_metadata_fields(field_id, options)

def delete_custom_metadata_field(
self, field_id: str = ""
) -> ResponseMetadataResult:
"""Deletes custom metadata fields by passing field_id"""
return self.file.delete_custom_metadata_field(field_id)

def url(self, options: Dict[str, Any]) -> str:
"""Get generated Url from options parameter
"""
"""Get generated Url from options parameter"""
return self.url_obj.generate_url(options)

@staticmethod
def phash_distance(first, second):
"""Get hamming distance between two phash(to check similarity)
"""
"""Get hamming distance between two phash(to check similarity)"""
if not (first and second):
raise TypeError(ERRORS.MISSING_PHASH_VALUE.value)
return hamming_distance(first, second)

def get_authentication_parameters(self, token="", expire=0):
"""Get Authentication parameters
"""
"""Get Authentication parameters"""
return get_authenticated_params(token, expire, self.ik_request.private_key)
1 change: 0 additions & 1 deletion imagekitio/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .errors import ERRORS
3 changes: 1 addition & 2 deletions imagekitio/constants/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Default(enum.Enum):
QUERY_TRANSFORMATION_POSITION,
]
DEFAULT_TIMESTAMP = 9999999999
SDK_VERSION_PARAMETER = "ik-sdk-version"
SDK_VERSION = "python-2.2.8"
SDK_VERSION = "python-3.0.0"
TRANSFORMATION_PARAMETER = "tr"
CHAIN_TRANSFORM_DELIMITER = ":"
TRANSFORM_DELIMITER = ","
Expand Down
15 changes: 11 additions & 4 deletions imagekitio/constants/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ERRORS(enum.Enum):
}
FILE_ID_MISSING = {
"message": "Missing File ID parameter for this request",
help: "",
"help": "",
}
UPDATE_DATA_MISSING = {
"message": "Missing file update data for this request",
Expand All @@ -53,13 +53,12 @@ class ERRORS(enum.Enum):
MISSING_UPLOAD_DATA = {"message": "Missing data for upload", help: ""}
MISSING_UPLOAD_FILE_PARAMETER = {
"message": "Missing file parameter for upload",
help: "",
"help": "",
}
MISSING_UPLOAD_FILENAME_PARAMETER = {
"message": "Missing fileName parameter for upload",
help: "",
"help": "",
}

INVALID_PHASH_VALUE = (
{
"message": "Invalid pHash value",
Expand All @@ -74,3 +73,11 @@ class ERRORS(enum.Enum):
"message": "Unequal pHash string length",
help: "For distance calculation, the two pHash strings must have equal length",
}
VERSION_ID_MISSING = {
"message": "Missing Version ID parameter for this request",
"help": "",
}
MISSING_CUSTOM_METADATA_FIELD_ID = {
"message": "Missing field_id for update_custom_metadata_fields",
"help": "",
}
Loading

0 comments on commit 38e50cc

Please sign in to comment.