88from contextlib import contextmanager
99from pathlib import Path
1010from typing import Callable , Dict , Iterator , List , Optional , Tuple , Union
11+ from urllib .parse import urlparse
1112
1213from tqdm .auto import tqdm
1314
1415from huggingface_hub .constants import REPO_TYPES_URL_PREFIXES , REPOCARD_NAME
1516from huggingface_hub .repocard import metadata_load , metadata_save
1617
17- from .hf_api import ENDPOINT , HfApi , HfFolder , repo_type_and_id_from_hf_id
18+ from .hf_api import HfApi , HfFolder , repo_type_and_id_from_hf_id
1819from .lfs import LFS_MULTIPART_UPLOAD_COMMAND
1920from .utils import logging
2021
@@ -441,6 +442,7 @@ def __init__(
441442 revision : Optional [str ] = None ,
442443 private : bool = False ,
443444 skip_lfs_files : bool = False ,
445+ client : Optional [HfApi ] = None ,
444446 ):
445447 """
446448 Instantiate a local clone of a git repo.
@@ -482,6 +484,9 @@ def __init__(
482484 whether the repository is private or not.
483485 skip_lfs_files (`bool`, *optional*, defaults to `False`):
484486 whether to skip git-LFS files or not.
487+ client (`HfApi`, *optional*):
488+ Instance of HfApi to use when calling the HF Hub API.
489+ A new instance will be created if this is left to `None`.
485490 """
486491
487492 os .makedirs (local_dir , exist_ok = True )
@@ -490,6 +495,7 @@ def __init__(
490495 self .command_queue = []
491496 self .private = private
492497 self .skip_lfs_files = skip_lfs_files
498+ self .client = client if client is not None else HfApi ()
493499
494500 self .check_git_versions ()
495501
@@ -513,7 +519,7 @@ def __init__(
513519 if self .huggingface_token is not None and (
514520 git_email is None or git_user is None
515521 ):
516- user = HfApi () .whoami (self .huggingface_token )
522+ user = self . client .whoami (self .huggingface_token )
517523
518524 if git_email is None :
519525 git_email = user ["email" ]
@@ -631,34 +637,36 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
631637 "Couldn't load Hugging Face Authorization Token. Credentials are required to work with private repositories."
632638 " Please login in using `huggingface-cli login` or provide your token manually with the `use_auth_token` key."
633639 )
634- api = HfApi ()
635-
636- if "huggingface.co" in repo_url or (
640+ hub_url = self .client .endpoint
641+ if hub_url in repo_url or (
637642 "http" not in repo_url and len (repo_url .split ("/" )) <= 2
638643 ):
639- repo_type , namespace , repo_id = repo_type_and_id_from_hf_id (repo_url )
644+ repo_type , namespace , repo_id = repo_type_and_id_from_hf_id (
645+ repo_url , hub_url = hub_url
646+ )
640647
641648 if repo_type is not None :
642649 self .repo_type = repo_type
643650
644- repo_url = ENDPOINT + "/"
651+ repo_url = hub_url + "/"
645652
646653 if self .repo_type in REPO_TYPES_URL_PREFIXES :
647654 repo_url += REPO_TYPES_URL_PREFIXES [self .repo_type ]
648655
649656 if token is not None :
650- whoami_info = api .whoami (token )
657+ whoami_info = self . client .whoami (token )
651658 user = whoami_info ["name" ]
652659 valid_organisations = [org ["name" ] for org in whoami_info ["orgs" ]]
653660
654661 if namespace is not None :
655662 repo_id = f"{ namespace } /{ repo_id } "
656663 repo_url += repo_id
657664
658- repo_url = repo_url .replace ("https://" , f"https://user:{ token } @" )
665+ scheme = urlparse (repo_url ).scheme
666+ repo_url = repo_url .replace (f"{ scheme } ://" , f"{ scheme } ://user:{ token } @" )
659667
660668 if namespace == user or namespace in valid_organisations :
661- api .create_repo (
669+ self . client .create_repo (
662670 repo_id = repo_id ,
663671 token = token ,
664672 repo_type = self .repo_type ,
@@ -671,7 +679,7 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
671679 repo_url += repo_id
672680
673681 # For error messages, it's cleaner to show the repo url without the token.
674- clean_repo_url = re .sub (r"https://.*@" , "https ://" , repo_url )
682+ clean_repo_url = re .sub (r"( https?) ://.*@" , r"\1 ://" , repo_url )
675683 try :
676684 subprocess .run (
677685 "git lfs install" .split (),
0 commit comments