Skip to content

Commit d679fb8

Browse files
mksong76goldworm
andauthored
Fix default keep alive interval (#79)
* Fix to use default keep_alive interval * Add some type hints --------- Co-authored-by: Chiwon Cho <superdoli@gmail.com>
1 parent 2c71bc1 commit d679fb8

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

iconsdk/icon_service.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def get_btp_source_information(self) -> dict:
521521
"""
522522
return self.__provider.make_request('btp_getSourceInformation')
523523

524-
def monitor(self, spec: MonitorSpec, keep_alive: Optional[float]) -> Monitor:
524+
def monitor(self, spec: MonitorSpec, keep_alive: Optional[float] = None) -> Monitor:
525525
"""
526526
Monitor events specified in the spec
527527

iconsdk/monitor.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# limitations under the License.
2626
from __future__ import annotations
2727

28-
from typing import List
28+
from typing import List, Dict, Any
2929

3030
from iconsdk.providers.provider import MonitorSpec
3131
from iconsdk.utils.typing.conversion import object_to_str
@@ -38,7 +38,7 @@ def __init__(self, addr: str, event: str, indexed: int, *args):
3838
self.__indexed = list(args[0:indexed])
3939
self.__data = list(args[indexed:])
4040

41-
def apply_to(self, obj: dict):
41+
def apply_to(self, obj: Dict[str, Any]):
4242
obj.update({
4343
"event": self.__event,
4444
"indexed": self.__indexed,
@@ -49,7 +49,7 @@ def apply_to(self, obj: dict):
4949
"addr": self.__addr
5050
})
5151

52-
def as_dict(self) -> dict:
52+
def as_dict(self) -> Dict[str, Any]:
5353
obj = {}
5454
self.apply_to(obj)
5555
return obj
@@ -107,7 +107,7 @@ def __init__(self, height: int | None,
107107
def get_path(self) -> str:
108108
return 'block'
109109

110-
def get_request(self) -> any:
110+
def get_request(self) -> Dict[str, Any]:
111111
params = {}
112112
if self.__height is not None:
113113
params["height"] = self.__height
@@ -142,7 +142,7 @@ def __init__(self, height: int | None,
142142
def get_path(self) -> str:
143143
return 'btp'
144144

145-
def get_request(self) -> any:
145+
def get_request(self) -> Dict[str, Any]:
146146
params = {
147147
"networkID": self.__network_id,
148148
}

iconsdk/providers/http_provider.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def _return_custom_response(response: requests.Response, full_response: bool = F
155155
return content['result']
156156
raise JSONRPCException(content["error"])
157157

158-
def make_monitor(self, spec: MonitorSpec, keep_alive: float = 30.0) -> Monitor:
158+
def make_monitor(self, spec: MonitorSpec, keep_alive: Optional[float] = None) -> Monitor:
159159
if self._WS_MAP is None:
160160
raise Exception(f'Channel must be set for socket')
161161
path = spec.get_path()
@@ -166,10 +166,9 @@ def make_monitor(self, spec: MonitorSpec, keep_alive: float = 30.0) -> Monitor:
166166

167167

168168
class WebSocketMonitor(Monitor):
169-
def __init__(self, url: str, params: dict, keep_alive: float):
169+
def __init__(self, url: str, params: dict, keep_alive: Optional[float] = None):
170170
self.__client = WebSocket()
171-
self.__keep_alive = keep_alive
172-
self.__client.timeout = keep_alive
171+
self.__keep_alive = keep_alive or 30
173172
self.__client.connect(url)
174173
self.__client.send(json.dumps(params))
175174
result = self.__read_json(None)
@@ -181,7 +180,7 @@ def __init__(self, url: str, params: dict, keep_alive: float):
181180
def close(self):
182181
self.__client.close()
183182

184-
def __read_json(self, timeout: Optional[float]) -> any:
183+
def __read_json(self, timeout: Optional[float] = None) -> any:
185184
now = monotonic()
186185
limit = None
187186
if timeout is not None:
@@ -202,5 +201,5 @@ def __read_json(self, timeout: Optional[float]) -> any:
202201
else:
203202
raise MonitorTimeoutException()
204203

205-
def read(self, timeout: Optional[float]) -> any:
204+
def read(self, timeout: Optional[float] = None) -> any:
206205
return self.__read_json(timeout=timeout)

iconsdk/providers/provider.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515

1616
from abc import ABCMeta, abstractmethod
17-
from typing import Optional
17+
from typing import Optional, Dict, Any
1818

1919

2020
class MonitorSpec(metaclass=ABCMeta):
@@ -48,7 +48,7 @@ def close(self):
4848
pass
4949

5050
@abstractmethod
51-
def read(self, timeout: Optional[float]) -> any:
51+
def read(self, timeout: Optional[float] = None) -> any:
5252
"""
5353
Read the notification
5454
@@ -62,11 +62,11 @@ class Provider(metaclass=ABCMeta):
6262
"""The provider defines how the IconService connects to RPC server."""
6363

6464
@abstractmethod
65-
def make_request(self, method: str, params=None, full_response: bool = False):
65+
def make_request(self, method: str, params: Optional[Dict[str, Any]] = None, full_response: bool = False):
6666
raise NotImplementedError("Providers must implement this method")
6767

6868
@abstractmethod
69-
def make_monitor(self, spec: MonitorSpec, keep_alive: float = 30.0) -> Monitor:
69+
def make_monitor(self, spec: MonitorSpec, keep_alive: Optional[float] = None) -> Monitor:
7070
"""
7171
Make monitor for the spec
7272
:param spec: Monitoring spec

0 commit comments

Comments
 (0)