• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ValeriyMenshikov / restcodegen / 15833827515

23 Jun 2025 07:57PM UTC coverage: 64.627%. First build
15833827515

Pull #1

github

web-flow
Merge 3dd73feae into 92ba04ef7
Pull Request #1: Manual refactor

260 of 293 new or added lines in 12 files covered. (88.74%)

433 of 670 relevant lines covered (64.63%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
/restcodegen/generator/spec_loader.py
1
import json
1✔
2
from pathlib import Path
1✔
3

4
import httpx
1✔
5

6
from restcodegen.generator.log import LOGGER
1✔
7
from restcodegen.generator.spec_patcher import SpecPatcher
1✔
8
from restcodegen.generator.utils import name_to_snake
1✔
9

10

11
class SpecLoader:
1✔
12
    BASE_PATH = Path.cwd() / "clients" / "http"
1✔
13

14
    def __init__(self, spec: str, service_name: str) -> None:
1✔
15
        self.spec_path = spec
1✔
16
        self.service_name = service_name
1✔
17
        self.cache_spec_dir = self.BASE_PATH / "schemas"
1✔
18

19
        if not self.cache_spec_dir.exists():
1✔
20
            self.cache_spec_dir.mkdir(parents=True, exist_ok=True)
1✔
21

22
        self.cache_spec_path = (
1✔
23
            self.cache_spec_dir / f"{name_to_snake(self.service_name)}.json"
24
        )
25
        self._patcher = SpecPatcher()
1✔
26

27
    def _get_spec_by_url(self) -> dict | None:
1✔
28
        try:
1✔
29
            response = httpx.get(self.spec_path, timeout=5)
1✔
30
            response.raise_for_status()
1✔
31
        except httpx.HTTPError:
1✔
32
            spec = {}
1✔
33
            LOGGER.warning(f"OpenAPI spec not available by url: {self.spec_path} ")
1✔
34
            file_path = (
1✔
35
                self.spec_path
36
                if Path(self.spec_path).is_file()
37
                else str(self.cache_spec_path)
38
            )
39
            if Path(file_path).is_file():
1✔
40
                LOGGER.warning(f"Try open OpenAPI spec by path: {file_path}")
1✔
41
                with open(file_path) as f:
1✔
42
                    spec = self._patcher.patch(json.loads(f.read()))
1✔
43
            return spec
1✔
44
        else:
45
            spec = self._patcher.patch(response.json())
1✔
46
            with open(self.cache_spec_path, "w") as f:
1✔
47
                f.write(json.dumps(spec, indent=4, ensure_ascii=False))
1✔
48
            return spec
1✔
49

50
    def _get_spec_from_cache(self) -> dict:
1✔
51
        try:
1✔
52
            with open(self.cache_spec_path) as f:
1✔
NEW
53
                spec = self._patcher.patch(json.loads(f.read()))
×
NEW
54
                self.spec_path = self.cache_spec_path  # type: ignore
×
NEW
55
                LOGGER.warning(f"OpenAPI spec got from cash: {self.spec_path}")
×
NEW
56
                return spec
×
57
        except FileNotFoundError as e:
1✔
58
            raise FileNotFoundError(
1✔
59
                f"OpenAPI spec not available from url: {self.spec_path}, and not found in cash"
60
            ) from e
61

62
    def _get_spec_by_path(self) -> dict | None:
1✔
63
        try:
1✔
64
            with open(self.spec_path) as f:
1✔
NEW
65
                spec = json.loads(f.read())
×
66
        except FileNotFoundError:
1✔
67
            LOGGER.warning(f"OpenAPI spec not found from local path: {self.spec_path}")
1✔
68
            return None
1✔
69
        else:
NEW
70
            return spec
×
71

72
    def open(self) -> dict:
1✔
73
        spec = self._get_spec_by_url()
1✔
74
        if not spec:
1✔
75
            spec = self._get_spec_by_path()
1✔
76
        if not spec:
1✔
77
            spec = self._get_spec_from_cache()
1✔
78
        return spec
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc