-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support create(), get_full_list(), download() and delete() * Refactored Client.send() to support binary responses. * Added Client.send_raw() which returns raw bytes from HTTP response.
- Loading branch information
1 parent
040cee4
commit 42db364
Showing
6 changed files
with
105 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from .admin import Admin | ||
from .backups import Backup | ||
from .collection import Collection | ||
from .external_auth import ExternalAuth | ||
from .log_request import LogRequest | ||
from .record import Record | ||
from .file_upload import FileUpload | ||
|
||
__all__ = ["Admin", "Collection", "ExternalAuth", "LogRequest", "Record", "FileUpload"] | ||
__all__ = ["Admin", "Backup", "Collection", "ExternalAuth", "LogRequest", "Record", "FileUpload"] |
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,18 @@ | ||
from __future__ import annotations | ||
|
||
import datetime | ||
|
||
from pocketbase.models.utils import BaseModel | ||
from pocketbase.utils import to_datetime | ||
|
||
|
||
class Backup(BaseModel): | ||
key: str | ||
modified: str | datetime.datetime | ||
size: int | ||
|
||
def load(self, data: dict) -> None: | ||
super().load(data) | ||
self.key = data.get("key", "") | ||
self.modified = to_datetime(data.pop("modified", "")) | ||
self.size = data.get("size", 0) |
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,32 @@ | ||
from __future__ import annotations | ||
|
||
from pocketbase.models import Backup | ||
from pocketbase.services.utils import BaseService | ||
|
||
|
||
class BackupsService(BaseService): | ||
def decode(self, data: dict) -> Backup: | ||
return Backup(data) | ||
|
||
def base_path(self) -> str: | ||
return "/api/backups" | ||
|
||
def create(self, name: str): | ||
# The backups service create method does not return an object. | ||
self.client.send( | ||
self.base_path(), | ||
{"method": "POST", "body": {"name": name}}, | ||
) | ||
|
||
def get_full_list(self, query_params: dict = {}) -> list[Backup]: | ||
response_data = self.client.send(self.base_path(), {"method": "GET", "params": query_params}) | ||
return [self.decode(item) for item in response_data] | ||
|
||
def download(self, key: str, file_token: str = None) -> bytes: | ||
if file_token is None: | ||
file_token = self.client.get_file_token() | ||
return self.client.send_raw("%s/%s" % (self.base_path(), key), | ||
{"method": "GET", "params": {"token": file_token}}) | ||
|
||
def delete(self, key: str): | ||
self.client.send("%s/%s" % (self.base_path(), key), {"method": "DELETE"}) |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import logging | ||
|
||
from pocketbase import PocketBase | ||
from pocketbase.utils import ClientResponseError | ||
import pytest | ||
|
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,36 @@ | ||
import datetime | ||
from uuid import uuid4 | ||
|
||
from pocketbase import PocketBase | ||
|
||
|
||
class TestBackupsService: | ||
def test_create_list_download_and_delete(self, client: PocketBase, state): | ||
state.backup_name = "%s.zip" % (uuid4().hex[:16],) | ||
client.backups.create(state.backup_name) | ||
|
||
try: | ||
# Find new backup in list of all backups | ||
for backup in client.backups.get_full_list(): | ||
if backup.key == state.backup_name: | ||
state.backup = backup | ||
assert isinstance(backup.modified, datetime.datetime) | ||
assert backup.size > 0 | ||
break | ||
else: | ||
self.fail("Backup %s not found in list of all backups" % (state.backup_name,)) | ||
|
||
# Download the backup | ||
data = client.backups.download(state.backup_name) | ||
assert isinstance(data, bytes) | ||
assert len(data) == state.backup.size | ||
finally: | ||
# Cleanup | ||
client.backups.delete(state.backup_name) | ||
|
||
# Check that it was deleted | ||
for backup in client.backups.get_full_list(): | ||
if backup.key == state.backup_name: | ||
self.fail("Backup %s still found in list of all backups" % (state.backup_name,)) | ||
break | ||
|