microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/qsharp/_http.py
34lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | """ |
| 5 | _http.py |
| 6 | |
| 7 | This module provides HTTP utility functions for interacting with |
| 8 | GitHub repositories. |
| 9 | """ |
| 10 | |
| 11 | |
| 12 | def fetch_github(owner: str, repo: str, ref: str, path: str) -> str: |
| 13 | """ |
| 14 | Fetches the content of a file from a GitHub repository. |
| 15 | |
| 16 | Args: |
| 17 | owner (str): The owner of the GitHub repository. |
| 18 | repo (str): The name of the GitHub repository. |
| 19 | ref (str): The reference (branch, tag, or commit) of the repository. |
| 20 | path (str): The path to the file within the repository. |
| 21 | |
| 22 | Returns: |
| 23 | str: The content of the file as a string. |
| 24 | |
| 25 | Raises: |
| 26 | urllib.error.HTTPError: If there is an error fetching the file from GitHub. |
| 27 | urllib.error.URLError: If there is an error with the URL. |
| 28 | """ |
| 29 | |
| 30 | import urllib |
| 31 | |
| 32 | path_no_leading_slash = path[1:] if path.startswith("/") else path |
| 33 | url = f"https://raw.githubusercontent.com/{owner}/{repo}/{ref}/{path_no_leading_slash}" |
| 34 | return urllib.request.urlopen(url).read().decode("utf-8-sig") |
| 35 | |