forked from platypush/platypush
[#398] Replaced GoogleDriveFile
response with GoogleDriveFileSchema
.
This commit is contained in:
parent
6003b205c8
commit
15b1c1f3c0
6 changed files with 95 additions and 88 deletions
|
@ -1,5 +0,0 @@
|
||||||
``google.drive``
|
|
||||||
===========================================
|
|
||||||
|
|
||||||
.. automodule:: platypush.message.response.google.drive
|
|
||||||
:members:
|
|
|
@ -6,6 +6,5 @@ Responses
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
:caption: Responses:
|
:caption: Responses:
|
||||||
|
|
||||||
platypush/responses/google.drive.rst
|
|
||||||
platypush/responses/printer.cups.rst
|
platypush/responses/printer.cups.rst
|
||||||
platypush/responses/tensorflow.rst
|
platypush/responses/tensorflow.rst
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from platypush.message import Mapping
|
|
||||||
from platypush.message.response import Response
|
|
||||||
|
|
||||||
|
|
||||||
class GoogleDriveResponse(Response):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class GoogleDriveFile(Mapping):
|
|
||||||
# noinspection PyShadowingBuiltins
|
|
||||||
def __init__(self,
|
|
||||||
type: str,
|
|
||||||
id: str,
|
|
||||||
name: str,
|
|
||||||
mime_type: Optional[str] = None,
|
|
||||||
*args, **kwargs):
|
|
||||||
super().__init__(id=id, name=name, type=type,
|
|
||||||
mime_type=mime_type, *args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
|
|
@ -4,7 +4,7 @@ from typing import Optional, List, Union
|
||||||
|
|
||||||
from platypush.plugins import action
|
from platypush.plugins import action
|
||||||
from platypush.plugins.google import GooglePlugin
|
from platypush.plugins.google import GooglePlugin
|
||||||
from platypush.message.response.google.drive import GoogleDriveFile
|
from platypush.schemas.google.drive import GoogleDriveFileSchema
|
||||||
|
|
||||||
|
|
||||||
class GoogleDrivePlugin(GooglePlugin):
|
class GoogleDrivePlugin(GooglePlugin):
|
||||||
|
@ -63,16 +63,15 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
scopes = [
|
scopes = [
|
||||||
'https://www.googleapis.com/auth/drive',
|
'https://www.googleapis.com/auth/drive',
|
||||||
'https://www.googleapis.com/auth/drive.appfolder',
|
'https://www.googleapis.com/auth/drive.appfolder',
|
||||||
'https://www.googleapis.com/auth/drive.photos.readonly',
|
'https://www.googleapis.com/auth/drive.file',
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(scopes=self.scopes, *args, **kwargs)
|
super().__init__(scopes=self.scopes, **kwargs)
|
||||||
|
|
||||||
def get_service(self, **_):
|
def get_service(self, *_, **__):
|
||||||
return super().get_service(service='drive', version='v3')
|
return super().get_service(service='drive', version='v3')
|
||||||
|
|
||||||
# noinspection PyShadowingBuiltins
|
|
||||||
@action
|
@action
|
||||||
def files(
|
def files(
|
||||||
self,
|
self,
|
||||||
|
@ -82,7 +81,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
drive_id: Optional[str] = None,
|
drive_id: Optional[str] = None,
|
||||||
spaces: Optional[Union[str, List[str]]] = None,
|
spaces: Optional[Union[str, List[str]]] = None,
|
||||||
order_by: Optional[Union[str, List[str]]] = None,
|
order_by: Optional[Union[str, List[str]]] = None,
|
||||||
) -> Union[GoogleDriveFile, List[GoogleDriveFile]]:
|
) -> List[dict]:
|
||||||
"""
|
"""
|
||||||
Get the list of files.
|
Get the list of files.
|
||||||
|
|
||||||
|
@ -117,8 +116,9 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
Attributes will be sorted in ascending order by default. You can change that by
|
Attributes will be sorted in ascending order by default. You can change that by
|
||||||
by appending "``desc``" separated by a space to the attribute you want in descending
|
by appending "``desc``" separated by a space to the attribute you want in descending
|
||||||
order - e.g. ``["folder", "createdTime desc", "modifiedTime desc"]``.
|
order - e.g. ``["folder", "createdTime desc", "modifiedTime desc"]``.
|
||||||
"""
|
|
||||||
|
|
||||||
|
:return: .. schema:: google.drive.GoogleDriveFileSchema(many=True)
|
||||||
|
"""
|
||||||
service = self.get_service()
|
service = self.get_service()
|
||||||
page_token = None
|
page_token = None
|
||||||
files = []
|
files = []
|
||||||
|
@ -152,15 +152,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
|
|
||||||
page_token = results.get('nextPageToken')
|
page_token = results.get('nextPageToken')
|
||||||
files.extend(
|
files.extend(
|
||||||
[
|
GoogleDriveFileSchema().dump(results.get('files', []), many=True)
|
||||||
GoogleDriveFile(
|
|
||||||
id=f.get('id'),
|
|
||||||
name=f.get('name'),
|
|
||||||
type=f.get('kind').split('#')[1],
|
|
||||||
mime_type=f.get('mimeType'),
|
|
||||||
)
|
|
||||||
for f in results.get('files', [])
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if not page_token or (limit and len(files) >= limit):
|
if not page_token or (limit and len(files) >= limit):
|
||||||
|
@ -172,16 +164,13 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
def get(self, file_id: str):
|
def get(self, file_id: str):
|
||||||
"""
|
"""
|
||||||
Get the information of a file on the Drive by file ID.
|
Get the information of a file on the Drive by file ID.
|
||||||
|
|
||||||
:param file_id: File ID.
|
:param file_id: File ID.
|
||||||
|
:return: .. schema:: google.drive.GoogleDriveFileSchema
|
||||||
"""
|
"""
|
||||||
service = self.get_service()
|
service = self.get_service()
|
||||||
file = service.files().get(fileId=file_id).execute()
|
file = service.files().get(fileId=file_id).execute()
|
||||||
return GoogleDriveFile(
|
return GoogleDriveFileSchema().dump(file)
|
||||||
type=file.get('kind').split('#')[1],
|
|
||||||
id=file.get('id'),
|
|
||||||
name=file.get('name'),
|
|
||||||
mime_type=file.get('mimeType'),
|
|
||||||
)
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def upload(
|
def upload(
|
||||||
|
@ -193,7 +182,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
parents: Optional[List[str]] = None,
|
parents: Optional[List[str]] = None,
|
||||||
starred: bool = False,
|
starred: bool = False,
|
||||||
target_mime_type: Optional[str] = None,
|
target_mime_type: Optional[str] = None,
|
||||||
) -> GoogleDriveFile:
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Upload a file to Google Drive.
|
Upload a file to Google Drive.
|
||||||
|
|
||||||
|
@ -208,8 +197,11 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
(use "``application/vnd.google-apps.document``). See
|
(use "``application/vnd.google-apps.document``). See
|
||||||
`the official documentation <https://developers.google.com/drive/api/v3/mime-types>`_ for a complete list
|
`the official documentation <https://developers.google.com/drive/api/v3/mime-types>`_ for a complete list
|
||||||
of supported types.
|
of supported types.
|
||||||
|
:return: The uploaded file metadata.
|
||||||
|
|
||||||
|
.. schema:: google.drive.GoogleDriveFileSchema
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# noinspection PyPackageRequirements
|
|
||||||
from googleapiclient.http import MediaFileUpload
|
from googleapiclient.http import MediaFileUpload
|
||||||
|
|
||||||
path = os.path.abspath(os.path.expanduser(path))
|
path = os.path.abspath(os.path.expanduser(path))
|
||||||
|
@ -232,12 +224,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
.execute()
|
.execute()
|
||||||
)
|
)
|
||||||
|
|
||||||
return GoogleDriveFile(
|
return dict(GoogleDriveFileSchema().dump(file))
|
||||||
type=file.get('kind').split('#')[1],
|
|
||||||
id=file.get('id'),
|
|
||||||
name=file.get('name'),
|
|
||||||
mime_type=file.get('mimeType'),
|
|
||||||
)
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def download(self, file_id: str, path: str) -> str:
|
def download(self, file_id: str, path: str) -> str:
|
||||||
|
@ -248,7 +235,6 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
:param path: Path of the file to upload.
|
:param path: Path of the file to upload.
|
||||||
:return: The local file path.
|
:return: The local file path.
|
||||||
"""
|
"""
|
||||||
# noinspection PyPackageRequirements
|
|
||||||
from googleapiclient.http import MediaIoBaseDownload
|
from googleapiclient.http import MediaIoBaseDownload
|
||||||
|
|
||||||
service = self.get_service()
|
service = self.get_service()
|
||||||
|
@ -268,6 +254,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
|
|
||||||
with open(path, 'wb') as f:
|
with open(path, 'wb') as f:
|
||||||
f.write(fh.getbuffer().tobytes())
|
f.write(fh.getbuffer().tobytes())
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
@ -278,7 +265,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
mime_type: Optional[str] = None,
|
mime_type: Optional[str] = None,
|
||||||
parents: Optional[List[str]] = None,
|
parents: Optional[List[str]] = None,
|
||||||
starred: bool = False,
|
starred: bool = False,
|
||||||
) -> GoogleDriveFile:
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Create a file.
|
Create a file.
|
||||||
|
|
||||||
|
@ -287,6 +274,10 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
:param mime_type: File MIME type.
|
:param mime_type: File MIME type.
|
||||||
:param parents: List of folder IDs that will contain the file (default: drive root).
|
:param parents: List of folder IDs that will contain the file (default: drive root).
|
||||||
:param starred: If True then the file will be marked as starred.
|
:param starred: If True then the file will be marked as starred.
|
||||||
|
:return: The created file metadata.
|
||||||
|
|
||||||
|
.. schema:: google.drive.GoogleDriveFileSchema
|
||||||
|
|
||||||
"""
|
"""
|
||||||
metadata = {
|
metadata = {
|
||||||
'name': name,
|
'name': name,
|
||||||
|
@ -300,13 +291,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
|
|
||||||
service = self.get_service()
|
service = self.get_service()
|
||||||
file = service.files().create(body=metadata, fields='*').execute()
|
file = service.files().create(body=metadata, fields='*').execute()
|
||||||
|
return dict(GoogleDriveFileSchema().dump(file))
|
||||||
return GoogleDriveFile(
|
|
||||||
type=file.get('kind').split('#')[1],
|
|
||||||
id=file.get('id'),
|
|
||||||
name=file.get('name'),
|
|
||||||
mime_type=file.get('mimeType'),
|
|
||||||
)
|
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def update(
|
def update(
|
||||||
|
@ -319,7 +304,7 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
mime_type: Optional[str] = None,
|
mime_type: Optional[str] = None,
|
||||||
starred: Optional[bool] = None,
|
starred: Optional[bool] = None,
|
||||||
trashed: Optional[bool] = None,
|
trashed: Optional[bool] = None,
|
||||||
) -> GoogleDriveFile:
|
) -> dict:
|
||||||
"""
|
"""
|
||||||
Update the metadata or the content of a file.
|
Update the metadata or the content of a file.
|
||||||
|
|
||||||
|
@ -331,6 +316,10 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
:param mime_type: Set the file MIME type.
|
:param mime_type: Set the file MIME type.
|
||||||
:param starred: Change the starred flag.
|
:param starred: Change the starred flag.
|
||||||
:param trashed: Move/remove from trash.
|
:param trashed: Move/remove from trash.
|
||||||
|
:return: The updated file metadata.
|
||||||
|
|
||||||
|
.. schema:: google.drive.GoogleDriveFileSchema
|
||||||
|
|
||||||
"""
|
"""
|
||||||
metadata = {}
|
metadata = {}
|
||||||
if name:
|
if name:
|
||||||
|
@ -348,40 +337,40 @@ class GoogleDrivePlugin(GooglePlugin):
|
||||||
if trashed is not None:
|
if trashed is not None:
|
||||||
metadata['trashed'] = trashed
|
metadata['trashed'] = trashed
|
||||||
|
|
||||||
service = self.get_service()
|
return dict(
|
||||||
file = (
|
GoogleDriveFileSchema().dump(
|
||||||
service.files().update(fileId=file_id, body=metadata, fields='*').execute()
|
self.get_service()
|
||||||
)
|
.files()
|
||||||
|
.update(fileId=file_id, body=metadata, fields='*')
|
||||||
return GoogleDriveFile(
|
.execute()
|
||||||
type=file.get('kind').split('#')[1],
|
)
|
||||||
id=file.get('id'),
|
|
||||||
name=file.get('name'),
|
|
||||||
mime_type=file.get('mimeType'),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def delete(self, file_id: str):
|
def delete(self, file_id: str):
|
||||||
"""
|
"""
|
||||||
Delete a file from Google Drive.
|
Delete a file from Google Drive.
|
||||||
|
|
||||||
:param file_id: File ID.
|
:param file_id: File ID.
|
||||||
"""
|
"""
|
||||||
service = self.get_service()
|
service = self.get_service()
|
||||||
service.files().delete(fileId=file_id).execute()
|
service.files().delete(fileId=file_id).execute()
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def copy(self, file_id: str) -> GoogleDriveFile:
|
def copy(self, file_id: str) -> dict:
|
||||||
"""
|
"""
|
||||||
Create a copy of a file.
|
Create a copy of a file.
|
||||||
|
|
||||||
:param file_id: File ID.
|
:param file_id: File ID.
|
||||||
|
:return: The copied file metadata.
|
||||||
|
|
||||||
|
.. schema:: google.drive.GoogleDriveFileSchema
|
||||||
|
|
||||||
"""
|
"""
|
||||||
service = self.get_service()
|
return dict(
|
||||||
file = service.files().copy(fileId=file_id).execute()
|
GoogleDriveFileSchema().dump(
|
||||||
return GoogleDriveFile(
|
self.get_service().files().copy(fileId=file_id).execute()
|
||||||
type=file.get('kind').split('#')[1],
|
)
|
||||||
id=file.get('id'),
|
|
||||||
name=file.get('name'),
|
|
||||||
mime_type=file.get('mimeType'),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
|
47
platypush/schemas/google/drive.py
Normal file
47
platypush/schemas/google/drive.py
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
from marshmallow import INCLUDE, fields, pre_dump
|
||||||
|
from marshmallow.schema import Schema
|
||||||
|
|
||||||
|
|
||||||
|
class GoogleDriveFileSchema(Schema):
|
||||||
|
"""
|
||||||
|
Schema for Google Drive files.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Meta: # type: ignore
|
||||||
|
"""
|
||||||
|
Include unknown fields in the deserialized output.
|
||||||
|
"""
|
||||||
|
|
||||||
|
unknown = INCLUDE
|
||||||
|
|
||||||
|
id = fields.String(
|
||||||
|
required=True,
|
||||||
|
metadata={'description': 'File ID'},
|
||||||
|
)
|
||||||
|
|
||||||
|
type = fields.String(
|
||||||
|
required=True,
|
||||||
|
metadata={
|
||||||
|
'description': 'File type',
|
||||||
|
'example': 'file',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
name = fields.String(
|
||||||
|
required=True,
|
||||||
|
metadata={'description': 'File name'},
|
||||||
|
)
|
||||||
|
|
||||||
|
mime_type = fields.String(
|
||||||
|
required=True,
|
||||||
|
metadata={
|
||||||
|
'description': 'File MIME type',
|
||||||
|
'example': 'plain/text',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
@pre_dump
|
||||||
|
def parse_type(self, data, **_):
|
||||||
|
data['type'] = data.pop('kind').split('#')[-1]
|
||||||
|
data['mime_type'] = data.pop('mimeType')
|
||||||
|
return data
|
Loading…
Reference in a new issue