Skip to content

Commit f6589b6

Browse files
✨ added "download" method to Backup class
1 parent 645ac54 commit f6589b6

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

squarecloud/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
from .file import File
2222
from .http.endpoints import Endpoint
2323

24-
__version__ = '3.7.0'
24+
__version__ = '3.7.2'

squarecloud/data.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from __future__ import annotations
22

3+
import os
4+
import zipfile
35
from datetime import datetime
46
from typing import Any, Dict, Literal, Optional
57

68
from ._internal.constants import USING_PYDANTIC
9+
from .http import HTTPClient
710

811
if USING_PYDANTIC:
912
from pydantic.dataclasses import dataclass
@@ -213,7 +216,6 @@ class BackupInfo:
213216
key: str
214217

215218

216-
@dataclass(frozen=True)
217219
class Backup:
218220
"""
219221
Backup data class
@@ -225,11 +227,21 @@ class Backup:
225227
:type key: str
226228
"""
227229

228-
url: str
229-
key: str
230+
__slots__ = ('url', 'key')
231+
232+
def __init__(self, url: str, key: str):
233+
self.url = url
234+
self.key = key
230235

231236
def to_dict(self):
232-
return self.__dict__.copy()
237+
return {'url': self.url, 'key': self.key}
238+
239+
async def download(self, path: str = './') -> zipfile.ZipFile:
240+
file_name = os.path.basename(self.url.split('?')[0])
241+
content = await HTTPClient.fetch_backup_content(self.url)
242+
with zipfile.ZipFile(f'{path}/{file_name}', 'w') as zip_file:
243+
zip_file.writestr(f'{path}/{file_name}', content)
244+
return zip_file
233245

234246

235247
@dataclass(frozen=True)
@@ -279,15 +291,15 @@ class FileInfo:
279291
280292
:type type: Literal['file', 'directory']
281293
:type name: str
282-
:type size: confloat(ge=0)
283-
:type lastModified: conint(ge=0) | float
294+
:type size: int
295+
:type lastModified: int | float | None
284296
:type path: str
285297
"""
286298

287299
app_id: str
288300
type: Literal['file', 'directory']
289301
name: str
290-
lastModified: int | float
302+
lastModified: int | float | None
291303
path: str
292304
size: int = 0
293305

squarecloud/http/http_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ async def request(self, route: Router, **kwargs) -> Response | bytes:
232232
)
233233
return response
234234

235+
@classmethod
236+
async def fetch_backup_content(cls, url: str) -> bytes:
237+
async with aiohttp.ClientSession() as session:
238+
async with session.get(url) as response:
239+
return await response.read()
240+
235241
async def fetch_user_info(self) -> Response:
236242
"""
237243
Fetches user information and returns the response object

0 commit comments

Comments
 (0)