diff --git a/aiogoogle/__version__.py b/aiogoogle/__version__.py index 7c769f9..ba10f2c 100644 --- a/aiogoogle/__version__.py +++ b/aiogoogle/__version__.py @@ -2,7 +2,7 @@ __about__ = "Async Google API client" __description__ = __about__ __url__ = "https://github.com/omarryhan/aiogoogle" -__version_info__ = ("1", "1", "2") +__version_info__ = ("1", "1", "3") __version__ = ".".join(__version_info__) __author__ = "Omar Ryhan" __author_email__ = "omarryhan@gmail.com" diff --git a/aiogoogle/auth/utils.py b/aiogoogle/auth/utils.py index e9c8c98..bed0c05 100644 --- a/aiogoogle/auth/utils.py +++ b/aiogoogle/auth/utils.py @@ -18,6 +18,9 @@ def _get_expires_at(expires_in): def _is_expired(expires_at): + # Refresh in case there's no expires_at present + if expires_at is None: + return True if not isinstance(expires_at, datetime.datetime): expires_at = datetime.datetime.fromisoformat(expires_at) if datetime.datetime.utcnow() >= expires_at: diff --git a/aiogoogle/models.py b/aiogoogle/models.py index 78e7641..99f3374 100644 --- a/aiogoogle/models.py +++ b/aiogoogle/models.py @@ -129,6 +129,8 @@ class Request: callback (callable): Synchronous callback that takes the content of the response as the only argument. Should also return content. _verify_ssl (boolean): Defaults to True. + + upload_file_content_type (str): Optional content-type header string. In case you don't want to use the default application/octet-stream (Or whatever is auto-detected by your transport handler) """ def __init__( @@ -144,6 +146,7 @@ def __init__( timeout=None, callback=None, _verify_ssl=True, + upload_file_content_type=None, ): self.method = method self.url = url @@ -159,6 +162,7 @@ def __init__( self.timeout = timeout self.callback = callback self._verify_ssl = _verify_ssl + self.upload_file_content_type = upload_file_content_type def _add_query_param(self, query: dict): url = self.url diff --git a/aiogoogle/sessions/aiohttp_session.py b/aiogoogle/sessions/aiohttp_session.py index 6880d27..6ca67af 100644 --- a/aiogoogle/sessions/aiohttp_session.py +++ b/aiogoogle/sessions/aiohttp_session.py @@ -101,10 +101,19 @@ async def fire_request(request): _aiter_file( request.media_upload.file_path, request.media_upload.chunk_size - ) + ), + headers={"Content-Type": request.upload_file_content_type} if request.upload_file_content_type else None ) if request.json: mpwriter.append_json(request.json) + + req_content_type = (request.upload_file_content_type or "multipart/related") if not request.json else "multipart/related" + + request.headers.update({"Content-Type": f"{req_content_type}; boundary={mpwriter.boundary}"}) + + # Aiohttp already handles this for us. Also the line below doesn't work. dk why. + # request.headers.update({"Content-Length": str(size)}) + return await self.request( method=request.method, url=request.media_upload.upload_path, @@ -119,6 +128,8 @@ async def fire_request(request): request.media_upload.file_path, "rb" ) as file: read_file = await file.read() + if request.upload_file_content_type: + request.headers.update({"Content-Type": request.upload_file_content_type}) return await self.request( method=request.method, url=request.media_upload.upload_path, diff --git a/examples/upload_drive_file.py b/examples/upload_drive_file.py index 3354609..b0c9493 100755 --- a/examples/upload_drive_file.py +++ b/examples/upload_drive_file.py @@ -11,14 +11,14 @@ async def upload_file(full_path, new_name): # Create API drive_v3 = await aiogoogle.discover("drive", "v3") - # Upload file - upload_res = await aiogoogle.as_user( - drive_v3.files.create( - upload_file=full_path, - fields="id", - json={"name": new_name} - ) + req = drive_v3.files.create( + upload_file=full_path, + fields="id", + json={"name": new_name} ) + + # Upload file + upload_res = await aiogoogle.as_user(req) print("Uploaded {} successfully.\nFile ID: {}".format(full_path, upload_res['id'])) # file_id = upload_res["id"] # # Rename uploaded file