File tree Expand file tree Collapse file tree 3 files changed +65
-2
lines changed Expand file tree Collapse file tree 3 files changed +65
-2
lines changed Original file line number Diff line number Diff line change
1
+ import json
2
+ from pathlib import Path
3
+
1
4
from pydantic import BaseModel
2
5
6
+ from .utils .config import get_cli_config_path
7
+
3
8
4
9
class Settings (BaseModel ):
5
- base_api_url : str = "http ://localhost:8000 /api/v1"
10
+ base_api_url : str = "https ://api.fastapicloud.club /api/v1"
6
11
client_id : str = "fastapi-cli"
12
+ base_frontend_url : str = "https://dashboard.fastapicloud.work"
13
+
14
+ @classmethod
15
+ def from_user_settings (cls , config_path : Path ) -> "Settings" :
16
+ try :
17
+ content = config_path .read_bytes () if config_path .exists () else b"{}"
18
+
19
+ user_settings = json .loads (content )
20
+ except json .JSONDecodeError :
21
+ user_settings = {}
22
+
23
+ return cls (** user_settings )
7
24
8
25
9
- settings = Settings ( )
26
+ settings = Settings . from_user_settings ( get_cli_config_path () )
Original file line number Diff line number Diff line change @@ -12,3 +12,10 @@ def get_auth_path() -> Path:
12
12
auth_path .parent .mkdir (parents = True , exist_ok = True )
13
13
14
14
return auth_path
15
+
16
+
17
+ def get_cli_config_path () -> Path :
18
+ cli_config_path = get_config_folder () / "cli.json"
19
+ cli_config_path .parent .mkdir (parents = True , exist_ok = True )
20
+
21
+ return cli_config_path
Original file line number Diff line number Diff line change
1
+ from pathlib import Path
2
+
3
+ from fastapi_cli .config import Settings
4
+
5
+
6
+ def test_loads_default_values_when_file_does_not_exist () -> None :
7
+ default_settings = Settings ()
8
+
9
+ settings = Settings .from_user_settings (Path ("non_existent_file.json" ))
10
+
11
+ assert settings .base_api_url == default_settings .base_api_url
12
+ assert settings .client_id == default_settings .client_id
13
+ assert settings .base_frontend_url == default_settings .base_frontend_url
14
+
15
+
16
+ def test_loads_settings_even_when_file_is_broken (tmp_path : Path ) -> None :
17
+ broken_settings_path = tmp_path / "broken_settings.json"
18
+ broken_settings_path .write_text ("this is not json" )
19
+
20
+ default_settings = Settings ()
21
+
22
+ settings = Settings .from_user_settings (broken_settings_path )
23
+
24
+ assert settings .base_api_url == default_settings .base_api_url
25
+ assert settings .client_id == default_settings .client_id
26
+ assert settings .base_frontend_url == default_settings .base_frontend_url
27
+
28
+
29
+ def test_loads_partial_settings (tmp_path : Path ) -> None :
30
+ settings_path = tmp_path / "settings.json"
31
+ settings_path .write_text ('{"base_api_url": "https://example.com"}' )
32
+
33
+ default_settings = Settings ()
34
+
35
+ settings = Settings .from_user_settings (settings_path )
36
+
37
+ assert settings .base_api_url == "https://example.com"
38
+ assert settings .client_id == default_settings .client_id
39
+ assert settings .base_frontend_url == default_settings .base_frontend_url
You can’t perform that action at this time.
0 commit comments