first_commit

This commit is contained in:
2024-12-13 19:49:04 +03:00
commit 00cc47785e
23 changed files with 188 additions and 0 deletions
+8
View File
@@ -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
+10
View File
@@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="108" name="Python" />
</Languages>
</inspection_tool>
</profile>
</component>
+6
View File
@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (yandex_disk_rest_api)" project-jdk-type="Python SDK" />
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/yandex_disk_rest_api.iml" filepath="$PROJECT_DIR$/.idea/yandex_disk_rest_api.iml" />
</modules>
</component>
</project>
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.12 (.venv)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
View File
View File
View File
+1
View File
@@ -0,0 +1 @@
from .test import *
View File
View File
+2
View File
@@ -0,0 +1,2 @@
class YandexDiskAPIException(Exception):
pass
View File
View File
+104
View File
@@ -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"
View File
+29
View File
@@ -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)
View File
View File
View File
View File