From 1d3d5e516e5ba547956dd3a8022abf93ccc75f2d Mon Sep 17 00:00:00 2001 From: Hugo Herter Date: Wed, 19 Jun 2024 12:12:50 +0200 Subject: [PATCH] Fix: aiohttp issues a DeprecationWarning (#129) For passing bytes to a file upload instead of a BytesIO fix(authenticated_http/storage_push_file): fix and add type annotation --------- Co-authored-by: Laurent Peuch --- src/aleph/sdk/client/authenticated_http.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/aleph/sdk/client/authenticated_http.py b/src/aleph/sdk/client/authenticated_http.py index 755a4557..60d42b2b 100644 --- a/src/aleph/sdk/client/authenticated_http.py +++ b/src/aleph/sdk/client/authenticated_http.py @@ -3,6 +3,7 @@ import logging import ssl import time +from io import BytesIO from pathlib import Path from typing import Any, Dict, List, Mapping, NoReturn, Optional, Tuple, Union @@ -114,14 +115,14 @@ async def storage_push(self, content: Mapping) -> str: resp.raise_for_status() return (await resp.json()).get("hash") - async def ipfs_push_file(self, file_content: Union[str, bytes]) -> str: + async def ipfs_push_file(self, file_content: bytes) -> str: """ Push a file to the IPFS service. :param file_content: The file content to upload """ data = aiohttp.FormData() - data.add_field("file", file_content) + data.add_field("file", BytesIO(file_content)) url = "/api/v0/ipfs/add_file" logger.debug(f"Pushing file to IPFS on {url}") @@ -130,12 +131,12 @@ async def ipfs_push_file(self, file_content: Union[str, bytes]) -> str: resp.raise_for_status() return (await resp.json()).get("hash") - async def storage_push_file(self, file_content) -> str: + async def storage_push_file(self, file_content: bytes) -> Optional[str]: """ Push a file to the storage service. """ data = aiohttp.FormData() - data.add_field("file", file_content) + data.add_field("file", BytesIO(file_content)) url = "/api/v0/storage/add_file" logger.debug(f"Posting file on {url}") @@ -669,7 +670,7 @@ async def _storage_push_file_with_message( content_type="application/json", ) # Add the file - data.add_field("file", file_content) + data.add_field("file", BytesIO(file_content)) url = "/api/v0/storage/add_file" logger.debug(f"Posting file on {url}")