Skip to content

Commit efafee2

Browse files
authored
Merge pull request fastapi#29 from fastapilabs/09-12-_load_configuration_from_json_file_if_any
✨ Load configuration from json file if any
2 parents ec58db3 + c1fb885 commit efafee2

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/fastapi_cli/config.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
import json
2+
from pathlib import Path
3+
14
from pydantic import BaseModel
25

6+
from .utils.config import get_cli_config_path
7+
38

49
class Settings(BaseModel):
5-
base_api_url: str = "http://localhost:8000/api/v1"
10+
base_api_url: str = "https://api.fastapicloud.club/api/v1"
611
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)
724

825

9-
settings = Settings()
26+
settings = Settings.from_user_settings(get_cli_config_path())

src/fastapi_cli/utils/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ def get_auth_path() -> Path:
1212
auth_path.parent.mkdir(parents=True, exist_ok=True)
1313

1414
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

tests/test_config.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

0 commit comments

Comments
 (0)