Skip to content

Commit

Permalink
Handle API authenticating response (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismail-Ben authored Dec 19, 2023
1 parent f50d544 commit 0a6fbe5
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sgpt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from typing import Dict, Generator, List

import requests
import typer

from .cache import Cache
from .config import cfg
from .config import SHELL_GPT_CONFIG_PATH, cfg

CACHE_LENGTH = int(cfg.get("CACHE_LENGTH"))
CACHE_PATH = Path(cfg.get("CACHE_PATH"))
Expand Down Expand Up @@ -58,6 +59,12 @@ def _request(
timeout=REQUEST_TIMEOUT,
stream=stream,
)
# Check if OPENAI_API_KEY is valid
if response.status_code == 401 or response.status_code == 403:
typer.secho(
f"Invalid OpenAI API key, update your config file: {SHELL_GPT_CONFIG_PATH}",
fg="red",
)
response.raise_for_status()
# TODO: Optimise.
# https://github.com/openai/openai-python/blob/237448dc072a2c062698da3f9f512fae38300c1c/openai/api_requestor.py#L98
Expand Down

0 comments on commit 0a6fbe5

Please sign in to comment.