From 00cc47785eac1d3870870102122cf9bc6b212d8f Mon Sep 17 00:00:00 2001 From: Alexander Tarasov Date: Fri, 13 Dec 2024 19:49:04 +0300 Subject: [PATCH] first_commit --- .idea/.gitignore | 8 ++ .idea/inspectionProfiles/Project_Default.xml | 10 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + .idea/yndx_disk.iml | 10 ++ README.md | 0 requirements.txt | 0 setup.py | 0 yndx_disk/__init__.py | 1 + yndx_disk/api/__init__.py | 0 yndx_disk/api/disk.py | 0 yndx_disk/api/exceptions.py | 2 + yndx_disk/api/operations.py | 0 yndx_disk/api/public_resources.py | 0 yndx_disk/api/resources.py | 104 ++++++++++++++++++ yndx_disk/api/trash_resources.py | 0 yndx_disk/api/utils.py | 29 +++++ yndx_disk/classes.py | 0 yndx_disk/clients/__init__.py | 0 yndx_disk/clients/async_client.py | 0 yndx_disk/clients/sync_client.py | 0 23 files changed, 188 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/yndx_disk.iml create mode 100644 README.md create mode 100644 requirements.txt create mode 100644 setup.py create mode 100644 yndx_disk/__init__.py create mode 100644 yndx_disk/api/__init__.py create mode 100644 yndx_disk/api/disk.py create mode 100644 yndx_disk/api/exceptions.py create mode 100644 yndx_disk/api/operations.py create mode 100644 yndx_disk/api/public_resources.py create mode 100644 yndx_disk/api/resources.py create mode 100644 yndx_disk/api/trash_resources.py create mode 100644 yndx_disk/api/utils.py create mode 100644 yndx_disk/classes.py create mode 100644 yndx_disk/clients/__init__.py create mode 100644 yndx_disk/clients/async_client.py create mode 100644 yndx_disk/clients/sync_client.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..9ea93e2 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cbe0c36 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8ead493 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/yndx_disk.iml b/.idea/yndx_disk.iml new file mode 100644 index 0000000..94eb249 --- /dev/null +++ b/.idea/yndx_disk.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/__init__.py b/yndx_disk/__init__.py new file mode 100644 index 0000000..273ad34 --- /dev/null +++ b/yndx_disk/__init__.py @@ -0,0 +1 @@ +from .test import * \ No newline at end of file diff --git a/yndx_disk/api/__init__.py b/yndx_disk/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/api/disk.py b/yndx_disk/api/disk.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/api/exceptions.py b/yndx_disk/api/exceptions.py new file mode 100644 index 0000000..c7ef5d9 --- /dev/null +++ b/yndx_disk/api/exceptions.py @@ -0,0 +1,2 @@ +class YandexDiskAPIException(Exception): + pass \ No newline at end of file diff --git a/yndx_disk/api/operations.py b/yndx_disk/api/operations.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/api/public_resources.py b/yndx_disk/api/public_resources.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/api/resources.py b/yndx_disk/api/resources.py new file mode 100644 index 0000000..ac668c2 --- /dev/null +++ b/yndx_disk/api/resources.py @@ -0,0 +1,104 @@ +import asyncio +import httpx +import yndx_disk.utils as utils +import yndx_disk.exceptions as exceptions + +BASE_URL = "https://cloud-api.yandex.net/v1/disk/resources" + + +async def delete(token: str, path: str, fields: str = "", md5: str = "", force_async: bool = False, + permanently: bool = False, timeout: int = 30): + url = BASE_URL + + async with httpx.AsyncClient() as client: + response = await client.delete( + url=url, + headers=utils.generate_headers(token=token), + params={ + "path": path, + "fields": fields, + "md5": md5, + "force_async": force_async, + "permanently": permanently, + }, + timeout=timeout + ) + + response_json = response.json() + + match response.status_code: + case 200: + return response_json + case _: + raise exceptions.YandexDiskAPIException(response.status_code, response_json.get("description", "")) + + +async def get_info(token: str, path: str, fields: str = "", preview_size: str = "", sort: str = "", + preview_crop: bool = False, limit: int = 100, offset: int = 0, timeout: int = 30): + url = BASE_URL + + async with httpx.AsyncClient() as client: + response = await client.get( + url=url, + headers=utils.generate_headers(token=token), + params={ + "path": path, + "fields": fields, + "preview_size": preview_size, + "sort": sort, + "preview_crop": preview_crop, + "limit": limit, + "offset": offset, + }, + timeout=timeout + ) + + response_json = response.json() + + match response.status_code: + case 200: + return response_json + case _: + raise exceptions.YandexDiskAPIException(response.status_code, response_json.get("description", "")) + +async def update_info(token: str, path: str, body: dict, fields: str = "", timeout: int = 30): + url = BASE_URL + +async def mkdir(token: str, path: str, fields: str = "", timeout: int = 30): + url = BASE_URL + +async def copy(token: str, from_path: str, to_path: str, fields: str = "", force_async: bool = False, + overwrite: bool = False, timeout: int = 30): + url = BASE_URL + "/copy" + +async def get_url(token: str, path: str, fields: str = "", timeout: int = 30): + url = BASE_URL + "/download" + +async def get_all_files(token: str, fields: str = "", media_type: str = "", preview_size: str = "", sort: str = "", + preview_crop: bool = False, limit: int = 100, offset: int = 0, timeout: int = 30): + url = BASE_URL + "/files" + +async def get_last_uploads(token: str, fields: str = "", media_type: str = "", preview_size: str = "", + preview_crop: bool = False, limit: int = 100, timeout: int = 30): + url = BASE_URL + "/last-uploaded" + +async def move(token: str, from_path: str, to_path: str, fields: str = "", force_async: bool = False, + overwrite: bool = False, timeout: int = 30): + url = BASE_URL + "/move" + +async def get_all_public(token: str, fields: str = "", preview_size: str = "", type_filter: str = "", + preview_crop: bool = False, limit: int = 100, offset: int = 0, timeout: int = 30): + url = BASE_URL + "/public" + +async def publish(token: str, path: str, body: dict, fields: str = "", allow_address_access: bool = False, + timeout: int = 30): + url = BASE_URL + "/publish" + +async def unpublish(token: str, path: str, fields: str = "", timeout: int = 30): + url = BASE_URL + "/unpublish" + +async def get_upload_url(token: str, path: str, fields: str = "", overwrite: bool = False, timeout: int = 30): + url = BASE_URL + "/upload" + +async def upload(token: str, upload_url: str, fields: str = "", disable_redirects: bool = False, timeout: int = 30): + url = BASE_URL + "/upload" \ No newline at end of file diff --git a/yndx_disk/api/trash_resources.py b/yndx_disk/api/trash_resources.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/api/utils.py b/yndx_disk/api/utils.py new file mode 100644 index 0000000..027056f --- /dev/null +++ b/yndx_disk/api/utils.py @@ -0,0 +1,29 @@ +from pathlib import Path + + +DEFAULT_HEADERS = { + "Accept": "application/json", + "Authorization": "OAuth {token}", +} + + + +def generate_headers(token: str) -> dict: + headers = DEFAULT_HEADERS.copy() + + headers["Authorization"] = f"OAuth {token}" + + return headers + + +def parse_path(path: str) -> str: + if path.startswith("/"): + path = "disk:/" + path[1:] + elif not path.startswith("disk:/"): + path = "disk:/" + path + + path = Path(path) # Some kind of check is path valid or not =P + + return str(path) + + diff --git a/yndx_disk/classes.py b/yndx_disk/classes.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/clients/__init__.py b/yndx_disk/clients/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/clients/async_client.py b/yndx_disk/clients/async_client.py new file mode 100644 index 0000000..e69de29 diff --git a/yndx_disk/clients/sync_client.py b/yndx_disk/clients/sync_client.py new file mode 100644 index 0000000..e69de29