Skip to content

Commit

Permalink
Auth managers now will refresh if expires_at is None
Browse files Browse the repository at this point in the history
Added support for adding custom upload_file_content_type for file uploads
  • Loading branch information
omarryhan committed Jan 15, 2021
1 parent 5136567 commit 06e30ae
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion aiogoogle/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions aiogoogle/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions aiogoogle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -144,6 +146,7 @@ def __init__(
timeout=None,
callback=None,
_verify_ssl=True,
upload_file_content_type=None,
):
self.method = method
self.url = url
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion aiogoogle/sessions/aiohttp_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions examples/upload_drive_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 06e30ae

Please sign in to comment.