66from pathlib import Path
77from typing import Optional
88
9- import requests
10-
119from cycode .cli .consts import CYCODE_CONFIGURATION_DIRECTORY
1210from cycode .cli .user_settings .credentials_manager import CredentialsManager
1311from cycode .cyclient import config as cyclient_config
@@ -61,7 +59,7 @@ def _load_cached_spec() -> Optional[dict]:
6159 return None
6260
6361
64- def _resolve_credentials (
62+ def resolve_credentials (
6563 client_id : Optional [str ] = None , client_secret : Optional [str ] = None
6664) -> tuple [str , str ]:
6765 """Resolve credentials from args or the CLI's standard credential chain."""
@@ -81,28 +79,28 @@ def _resolve_credentials(
8179
8280
8381def _fetch_and_cache_spec (client_id : Optional [str ] = None , client_secret : Optional [str ] = None ) -> dict :
84- """Fetch OpenAPI spec from API and cache to disk."""
82+ """Fetch OpenAPI spec from API and cache to disk.
83+
84+ Uses CycodeTokenBasedClient for auth and retries. The spec is served from the app URL,
85+ so we create a client with app_url as base instead of the default api_url.
86+ """
8587 from cycode .cyclient .cycode_token_based_client import CycodeTokenBasedClient
8688
87- cid , csecret = _resolve_credentials (client_id , client_secret )
88- token = CycodeTokenBasedClient (cid , csecret ).get_access_token ()
89+ cid , csecret = resolve_credentials (client_id , client_secret )
8990
90- # Fetch spec from app URL (spec is served from app, not api)
91- app_url = cyclient_config .cycode_app_url
92- spec_url = f'{ app_url } { _OPENAPI_SPEC_PATH } '
93- logger .info ('Fetching OpenAPI spec from %s' , spec_url )
91+ # Create a token-based client pointed at app URL (spec is served from app, not api)
92+ client = CycodeTokenBasedClient (cid , csecret )
93+ client .api_url = cyclient_config .cycode_app_url
94+
95+ spec_path = _OPENAPI_SPEC_PATH .lstrip ('/' )
96+ logger .info ('Fetching OpenAPI spec from %s/%s' , cyclient_config .cycode_app_url , spec_path )
9497
9598 try :
96- response = requests .get (
97- spec_url ,
98- headers = {'Authorization' : f'Bearer { token } ' },
99- timeout = 60 ,
100- )
101- response .raise_for_status ()
99+ response = client .get (spec_path )
102100 spec = response .json ()
103- except requests . RequestException as e :
101+ except Exception as e :
104102 raise OpenAPISpecError (
105- f'Failed to fetch OpenAPI spec from { spec_url } . Check your authentication and network connectivity.'
103+ f'Failed to fetch OpenAPI spec. Check your authentication and network connectivity. Error: { e } '
106104 ) from e
107105
108106 # Override server URL with API URL (supports on-premise installations)
0 commit comments