Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/doc: allow adding custom header to object/adding docs on this operation #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ Example Usage:
fh.seek(0)
bucket.upload(fh, "test/foobar2.txt")

# it seems newly uplaoded file will **NOT** be available immediately. Sleep for x seconds?
# Advanced: specify headers to optimise the stored objects
import gzip
from io import BytesIO
fh = BytesIO(gzip.compress(b"foo bar"))
fh.seek(0)
# Most HTTP libraries can handle Content-Encoding header
bucket.upload(fh, "test/foobar2_gzipped.txt", headers={"Content-Encoding": "gzip"})

# it seems newly uploaded file will **NOT** be available immediately. Sleep for x seconds?
from time import sleep
sleep(1)

Expand Down
3 changes: 3 additions & 0 deletions ebrains_drive/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import base64
import json
import time
from copy import deepcopy
from ebrains_drive.utils import urljoin, on_401_raise_unauthorized
from ebrains_drive.exceptions import ClientHttpError, TokenExpired
from ebrains_drive.repos import Repos
Expand Down Expand Up @@ -74,6 +75,8 @@ def send_request(self, method: str, url: str, *args, **kwargs):
# - accounts for if url was provided with leading slashes
url = self.server.rstrip('/') + '/' + url.lstrip('/')

# deepcopy the kwargs so do not mutate the original kwargs
kwargs = deepcopy(kwargs)
headers = kwargs.get('headers', {})
headers.setdefault('Authorization', 'Bearer ' + self._token)
kwargs['headers'] = headers
Expand Down