arcade-mcp/toolkits/github/arcade_github/tools/utils.py
Eric Gustin ab889f9f1d
Lint all toolkits (#183)
# PR Description
* Adds/updates the following files to all toolkits:
    - `.pre-commit-config.yaml`
    - `.ruff.toml`
    - `LICENSE`
    - `Makefile`
    - `pyproject.toml`
* Lint all toolkits such that they pass `make check` and `make test` (a
total doozy). This includes adding some unit tests and evals.
* Github workflow for testing toolkits before merge into main (courtesy
of @sdreyer)
* Added a QOL improvement for tool developers for when they need to get
the context's auth token.
* Minor updates to `arcade new` template.
2024-12-20 09:49:45 -08:00

85 lines
2.7 KiB
Python

from typing import Any
import httpx
from arcade.sdk.errors import ToolExecutionError
from arcade_github.tools.constants import ENDPOINTS, GITHUB_API_BASE_URL
def handle_github_response(response: httpx.Response, url: str) -> None:
"""
Handle GitHub API response and raise appropriate exceptions for non-200 status codes.
:param response: The response object from the GitHub API
:param url: The URL of the API endpoint
:raises ToolExecutionError: If the response status code is not 200
"""
if 200 <= response.status_code < 300:
return
error_messages = {
301: "Moved permanently. The repository has moved.",
304: "Not modified. The requested resource hasn't been modified since the last request.",
403: "Forbidden. You do not have access to this resource.",
404: "Resource not found. The requested resource does not exist.",
410: "Gone. The requested resource is no longer available.",
422: "Validation failed or the endpoint has been spammed.",
503: "Service unavailable. The server is temporarily unable to handle the request.",
}
error_message = error_messages.get(
response.status_code, f"Failed to process request. Status code: {response.status_code}"
)
raise ToolExecutionError(f"Error accessing '{url}': {error_message}")
def get_github_json_headers(token: str | None) -> dict:
"""
Generate common headers for GitHub API requests.
:param token: The authorization token
:return: A dictionary of headers
"""
token = token or ""
return {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
}
def get_github_diff_headers(token: str | None) -> dict:
"""
Generate headers for GitHub API requests for diff content.
:param token: The authorization token
:return: A dictionary of headers
"""
token = token or ""
return {
"Accept": "application/vnd.github.diff",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
}
def remove_none_values(params: dict) -> dict:
"""
Remove None values from a dictionary.
:param params: The dictionary to clean
:return: A new dictionary with None values removed
"""
return {k: v for k, v in params.items() if v is not None}
def get_url(endpoint: str, **kwargs: Any) -> str:
"""
Get the full URL for a given endpoint.
:param endpoint: The endpoint key from ENDPOINTS
:param kwargs: The parameters to format the URL with
:return: The full URL
"""
return f"{GITHUB_API_BASE_URL}{ENDPOINTS[endpoint].format(**kwargs)}"