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

Allow passing custom headers to HfApi #2098

Merged
merged 7 commits into from
Mar 20, 2024

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Mar 8, 2024

cc @rafaelpierrehf @philschmid I'll document + add tests for it later but the current implementation should be good as it is now. Here is a short example on how it can be used to authenticate via cookies:

from pathlib import Path
from huggingface_hub import HfApi

HF_COOKIE = "token=XATmoNot...;"
api = HfApi(headers={"Cookie": HF_COOKIE}, token=False)

# Create repo
repo_url = api.create_repo("test_with_cookie")

# Upload file 
api.upload_file(repo_id=repo_url.repo_id, path_in_repo="file.txt", path_or_fileobj=b"content")

# Download file
path = api.hf_hub_download(repo_id=repo_url.repo_id, filename="file.txt")
print(Path(path).read_text())
# 'content'

Which resulted in https://huggingface.co/Wauplin/test_with_cookie.

EDIT: failing tests are mostly due to warnings.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin Wauplin marked this pull request as ready for review March 19, 2024 17:14
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the changes.

repo_id=repo_id,
repo_type=repo_type,
revision=revision,
endpoint=endpoint,
headers=headers,
token=None, # already passed in 'headers'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the token be generally expected in the headers in that case, rather than put as a kwarg? Or is it a public-facing function and we can't change the API maybe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I removed token whenever possible but this one is not a private method so just in case I did not remove token from the params.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(though it's not a documented / promoted method)

@Wauplin
Copy link
Contributor Author

Wauplin commented Mar 20, 2024

Thanks for the review @LysandreJik! I'll merge it as it is

@Wauplin Wauplin merged commit 969af39 into main Mar 20, 2024
15 of 16 checks passed
@Wauplin Wauplin deleted the allow-passing-custom-headers-to-hf-api branch March 20, 2024 09:24
@albertvillanova
Copy link
Member

albertvillanova commented Apr 16, 2024

Hi @Wauplin, we would like to know if we should update our code to use headers instead of token.

CC: @severo

@Wauplin
Copy link
Contributor Author

Wauplin commented Apr 16, 2024

we would like to know if we should update our code to use headers instead of token.

No, using tokens is the recommended way of handling authentication.
Passing headers can be more flexible but has very limited use cases so not something we want to promote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants