-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
230 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
lib/dl_file_uploader_lib/dl_file_uploader_lib/yadocs_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from typing import Any | ||
|
||
import aiohttp | ||
from aiohttp.client import ( | ||
ClientResponse, | ||
ClientSession, | ||
) | ||
|
||
from dl_file_uploader_lib import exc as file_upl_exc | ||
|
||
|
||
def yadocs_error_to_file_uploader_exception(status_code: int, resp_info: dict): | ||
if status_code == 401: | ||
err_cls = file_upl_exc.PermissionDenied | ||
elif status_code == 404: | ||
err_cls = file_upl_exc.DocumentNotFound | ||
elif status_code == 400: | ||
err_cls = file_upl_exc.UnsupportedDocument | ||
elif status_code >= 500: | ||
err_cls = file_upl_exc.RemoteServerError | ||
else: | ||
err_cls = file_upl_exc.DLFileUploaderBaseError | ||
|
||
return err_cls( | ||
details=resp_info, | ||
) | ||
|
||
|
||
class YaDocsClient: | ||
headers: dict[str, Any] = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
hostname: str = "https://cloud-api.yandex.net/v1/disk" | ||
|
||
def __init__(self, session: ClientSession): | ||
self.session = session | ||
|
||
async def get_spreadsheet_public_ref(self, link: str) -> str: | ||
resp = await self.session.get( | ||
f"{self.hostname}/public/resources/download/?public_key={link}", | ||
headers=self.headers, | ||
) | ||
if resp.status != 200: | ||
raise yadocs_error_to_file_uploader_exception(resp.status, await resp.json()) | ||
return (await resp.json())["href"] | ||
|
||
async def get_spreadsheet_public_meta(self, link: str) -> ClientResponse: | ||
resp = await self.session.get( | ||
f"{self.hostname}/public/resources/?public_key={link}", | ||
headers=self.headers, | ||
) | ||
if resp.status != 200: | ||
raise yadocs_error_to_file_uploader_exception(resp.status, await resp.json()) | ||
return await resp.json() | ||
|
||
async def get_spreadsheet_private_ref(self, path: str, token: str) -> str: | ||
headers_with_token = self._create_headers_with_token(token) | ||
resp = await self.session.get( | ||
f"{self.hostname}/resources/download/?path={path}", | ||
headers=headers_with_token, | ||
) | ||
if resp.status != 200: | ||
raise yadocs_error_to_file_uploader_exception(resp.status, await resp.json()) | ||
return (await resp.json())["href"] | ||
|
||
async def get_spreadsheet_private_meta(self, path: str, token: str) -> ClientResponse: | ||
headers_with_token = self._create_headers_with_token(token) | ||
resp = await self.session.get( | ||
f"{self.hostname}/resources/?path={path}", | ||
headers=headers_with_token, | ||
) | ||
if resp.status != 200: | ||
raise yadocs_error_to_file_uploader_exception(resp.status, await resp.json()) | ||
return await resp.json() | ||
|
||
def _create_headers_with_token(self, token: str) -> dict[str, Any]: | ||
headers_with_token = self.headers.copy() | ||
headers_with_token.update({"Authorization": "OAuth " + token}) | ||
return headers_with_token |
43 changes: 0 additions & 43 deletions
43
lib/dl_file_uploader_lib/dl_file_uploader_lib/yadocuments_client.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.