Skip to content

Commit d3c0414

Browse files
committed
chore: BaseModel 필드로 TAGOClient를 받도록 수정
1 parent 4df7c68 commit d3c0414

12 files changed

Lines changed: 42 additions & 27 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ caches/cache.pkl
77
.vscodeoutput.json
88

99

10+
tagoapi/caches/station.pkl

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tagoapi/caches/station.pkl

0 Bytes
Binary file not shown.

tagoapi/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,13 @@ def _fetch_and_convert(
156156
response = parse_metadata(self._get(endpoint, params))
157157
if not response:
158158
return None
159+
160+
## Convert to List
159161
if isinstance(response, list):
162+
160163
if not is_cache:
161-
return convert(response, model.from_list)
164+
return convert(response, model.from_list, self)
165+
162166
result = []
163167
for v in response:
164168
key = cache_key.generate_key(v)
@@ -171,8 +175,10 @@ def _fetch_and_convert(
171175
cache.save(key, parsed_obj, self.CACHE_TTL)
172176

173177
return result
174-
178+
179+
## Covert to Dict
175180
else:
181+
176182
if is_cache:
177183
key = cache_key.generate_key(response)
178184
cached = cache.get(key)

tagoapi/exceptions.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,3 @@ class CacheNotFoundError(TAGOAPIError):
3333
"""캐시파일을 불러오지 못할 때"""
3434
pass
3535

36-
class TAGORequestError(TAGOAPIError):
37-
"""요청 실패 (http 에러, 타임아웃 등)"""
38-
def __init__(self, message: str, status_code: int | None ):
39-
super().__init__(message)
40-
self.status_code = status_code
41-
42-
class TAGOResponseError(TAGOAPIError):
43-
""" 요청 오류 """
44-
def __init__(self, message: str, status_code: int | None ):
45-
super().__init__(message)
46-
self.status_code = status_code

tagoapi/models/BaseModel.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
1+
from typing import TYPE_CHECKING
2+
if TYPE_CHECKING:
3+
from tagoapi import TAGOClient
24

35
class BaseModel:
46
cache_key = "BaseModel:<id>"
5-
def __init__(self): ...
6-
7+
def __init__(self, client: TAGOClient):
8+
self._client = client
9+
710
def to_dict(self) -> dict: ...
811

912
def to_dict(self):
1013
return vars(self)
1114

1215
@classmethod
13-
def from_dict(cls, data:dict) -> "BaseModel": ...
16+
def from_dict(cls, data: dict, client: TAGOClient) -> "BaseModel": ...
1417

1518
@classmethod
16-
def from_list(cls, data:list) -> list["BaseModel"]:
17-
return [cls.from_dict(v) for v in data]
19+
def from_list(cls, data: list, client: TAGOClient) -> list["BaseModel"]:
20+
return [cls.from_dict(v, client) for v in data]
1821

1922

tagoapi/models/Route.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ def __init__(
1818
startNodeNm: str = None,
1919
endvehicletime: int = None,
2020
startvehicletime: int = None,
21+
client = None
2122
#TODO: 정류장 리스트도 넣으면 좋을 듯 합니당
2223
):
23-
24+
super().__init__(client)
25+
2426
self.routeId = routeId
2527
self.routeNo = routeNo
2628
self.routeTp = routeTp

tagoapi/models/Vehicle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ def from_dict(cls, data: dict) -> "Vehicle":
4545
)
4646

4747
@classmethod
48-
def from_list(cls, data:list[dict]) -> list["Vehicle"]:
48+
def from_list(cls, data: list[dict]) -> list["Vehicle"]:
4949
return [ cls.from_dict(vehicle) for vehicle in data]

tagoapi/utils/cache.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ def __init__(self, path: str = DEFAULT_CACHE_PATH):
1414

1515
def _load(self) -> dict:
1616
if os.path.exists(self.path):
17-
with open(self.path, 'rb') as f:
18-
return pickle.load(f)
17+
try:
18+
19+
with open(self.path, 'rb') as f:
20+
return pickle.load(f)
21+
22+
except Exception:
23+
return {}
1924
return {}
2025

2126
def save(self, key: str, value: dict, ttl: int = 86400) -> bool:

tagoapi/utils/convertor.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import Union, Callable, TypeVar, Any
1+
from typing import TYPE_CHECKING, Union, Callable, TypeVar, Any
2+
if TYPE_CHECKING:
3+
from tagoapi import TAGOClient
24
T = TypeVar('T', dict, list) # list, dict으로 매개변수 받을 때
35
U = TypeVar('U', dict[str], list) # list, dict으로 반환할 때
46

5-
def convert(res: T, converter: Callable[[T], U]) -> U:
7+
def convert(res: T, converter: Callable[[T, TAGOClient], U], _client: TAGOClient) -> U:
68
if not res:
79
return None
8-
return converter(res)
10+
return converter(res, _client)

0 commit comments

Comments
 (0)