runtime.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. from darabonba.core import DaraModel
  2. from typing import Dict, Any
  3. from darabonba.policy.retry import RetryOptions
  4. class ExtendsParameters(DaraModel):
  5. def __init__(
  6. self,
  7. headers: Dict[str, str] = None,
  8. queries: Dict[str, str] = None,
  9. ):
  10. self.headers = headers
  11. self.queries = queries
  12. def validate(self):
  13. pass
  14. def to_map(self):
  15. _map = super().to_map()
  16. if _map is not None:
  17. return _map
  18. result = dict()
  19. if self.headers is not None:
  20. result['headers'] = self.headers
  21. if self.queries is not None:
  22. result['queries'] = self.queries
  23. return result
  24. def from_map(self, m: dict = None):
  25. m = m or dict()
  26. if m.get('headers') is not None:
  27. self.headers = m.get('headers')
  28. if m.get('queries') is not None:
  29. self.queries = m.get('queries')
  30. return self
  31. class RuntimeOptions(DaraModel):
  32. """
  33. The common runtime options model
  34. """
  35. def __init__(
  36. self,
  37. retry_options: RetryOptions = None,
  38. autoretry: bool = None,
  39. ignore_ssl: bool = None,
  40. max_attempts: int = None,
  41. backoff_policy: str = None,
  42. backoff_period: int = None,
  43. read_timeout: int = None,
  44. connect_timeout: int = None,
  45. http_proxy: str = None,
  46. https_proxy: str = None,
  47. no_proxy: str = None,
  48. max_idle_conns: int = None,
  49. local_addr: str = None,
  50. socks_5proxy: str = None,
  51. socks_5net_work: str = None,
  52. keep_alive: bool = None,
  53. key: str = None,
  54. cert: str = None,
  55. ca: str = None,
  56. extends_parameters: ExtendsParameters = None,
  57. web_socket_ping_interval: int = None,
  58. web_socket_pong_timeout: int = None,
  59. web_socket_enable_reconnect: bool = None,
  60. web_socket_reconnect_interval: int = None,
  61. web_socket_max_reconnect_times: int = None,
  62. web_socket_write_timeout: int = None,
  63. web_socket_handshake_timeout: int = None,
  64. web_socket_handler: Any = None,
  65. ):
  66. # retry options
  67. self.retry_options = retry_options
  68. # whether to try again
  69. self.autoretry = autoretry
  70. # ignore SSL validation
  71. self.ignore_ssl = ignore_ssl
  72. # privite key for client certificate
  73. self.key = key
  74. # client certificate
  75. self.cert = cert
  76. # server certificate
  77. self.ca = ca
  78. # maximum number of retries
  79. self.max_attempts = max_attempts
  80. # backoff policy
  81. self.backoff_policy = backoff_policy
  82. # backoff period
  83. self.backoff_period = backoff_period
  84. # read timeout
  85. self.read_timeout = read_timeout
  86. # connect timeout
  87. self.connect_timeout = connect_timeout
  88. # http proxy url
  89. self.http_proxy = http_proxy
  90. # https Proxy url
  91. self.https_proxy = https_proxy
  92. # agent blacklist
  93. self.no_proxy = no_proxy
  94. # maximum number of connections
  95. self.max_idle_conns = max_idle_conns
  96. # local addr
  97. self.local_addr = local_addr
  98. # SOCKS5 proxy
  99. self.socks_5proxy = socks_5proxy
  100. # SOCKS5 netWork
  101. self.socks_5net_work = socks_5net_work
  102. # whether to enable keep-alive
  103. self.keep_alive = keep_alive
  104. # Extends Parameters
  105. self.extends_parameters = extends_parameters
  106. # WebSocket options
  107. self.web_socket_ping_interval = web_socket_ping_interval
  108. self.web_socket_pong_timeout = web_socket_pong_timeout
  109. self.web_socket_enable_reconnect = web_socket_enable_reconnect
  110. self.web_socket_reconnect_interval = web_socket_reconnect_interval
  111. self.web_socket_max_reconnect_times = web_socket_max_reconnect_times
  112. self.web_socket_write_timeout = web_socket_write_timeout
  113. self.web_socket_handshake_timeout = web_socket_handshake_timeout
  114. self.web_socket_handler = web_socket_handler
  115. def validate(self):
  116. if self.retry_options:
  117. self.retry_options.validate()
  118. if self.extends_parameters:
  119. self.extends_parameters.validate()
  120. def to_map(self):
  121. _map = super().to_map()
  122. if _map is not None:
  123. return _map
  124. result = dict()
  125. if self.retry_options is not None:
  126. result['retryOptions'] = self.retry_options.to_map()
  127. if self.autoretry is not None:
  128. result['autoretry'] = self.autoretry
  129. if self.ignore_ssl is not None:
  130. result['ignoreSSL'] = self.ignore_ssl
  131. if self.key is not None:
  132. result['key'] = self.key
  133. if self.cert is not None:
  134. result['cert'] = self.cert
  135. if self.ca is not None:
  136. result['ca'] = self.ca
  137. if self.max_attempts is not None:
  138. result['max_attempts'] = self.max_attempts
  139. if self.backoff_policy is not None:
  140. result['backoff_policy'] = self.backoff_policy
  141. if self.backoff_period is not None:
  142. result['backoff_period'] = self.backoff_period
  143. if self.read_timeout is not None:
  144. result['readTimeout'] = self.read_timeout
  145. if self.connect_timeout is not None:
  146. result['connectTimeout'] = self.connect_timeout
  147. if self.http_proxy is not None:
  148. result['httpProxy'] = self.http_proxy
  149. if self.https_proxy is not None:
  150. result['httpsProxy'] = self.https_proxy
  151. if self.no_proxy is not None:
  152. result['noProxy'] = self.no_proxy
  153. if self.max_idle_conns is not None:
  154. result['maxIdleConns'] = self.max_idle_conns
  155. if self.local_addr is not None:
  156. result['localAddr'] = self.local_addr
  157. if self.socks_5proxy is not None:
  158. result['socks5Proxy'] = self.socks_5proxy
  159. if self.socks_5net_work is not None:
  160. result['socks5NetWork'] = self.socks_5net_work
  161. if self.keep_alive is not None:
  162. result['keepAlive'] = self.keep_alive
  163. if self.extends_parameters is not None:
  164. result['extendsParameters'] = self.extends_parameters.to_map()
  165. if self.web_socket_ping_interval is not None:
  166. result['webSocketPingInterval'] = self.web_socket_ping_interval
  167. if self.web_socket_pong_timeout is not None:
  168. result['webSocketPongTimeout'] = self.web_socket_pong_timeout
  169. if self.web_socket_enable_reconnect is not None:
  170. result['webSocketEnableReconnect'] = self.web_socket_enable_reconnect
  171. if self.web_socket_reconnect_interval is not None:
  172. result['webSocketReconnectInterval'] = self.web_socket_reconnect_interval
  173. if self.web_socket_max_reconnect_times is not None:
  174. result['webSocketMaxReconnectTimes'] = self.web_socket_max_reconnect_times
  175. if self.web_socket_write_timeout is not None:
  176. result['webSocketWriteTimeout'] = self.web_socket_write_timeout
  177. if self.web_socket_handshake_timeout is not None:
  178. result['webSocketHandshakeTimeout'] = self.web_socket_handshake_timeout
  179. return result
  180. def from_map(self, m: dict = None):
  181. m = m or dict()
  182. if m.get('retryOptions') is not None:
  183. self.retry_options = RetryOptions.from_map(m.get('retryOptions'))
  184. if m.get('autoretry') is not None:
  185. self.autoretry = m.get('autoretry')
  186. if m.get('ignoreSSL') is not None:
  187. self.ignore_ssl = m.get('ignoreSSL')
  188. if m.get('key') is not None:
  189. self.key = m.get('key')
  190. if m.get('cert') is not None:
  191. self.cert = m.get('cert')
  192. if m.get('ca') is not None:
  193. self.ca = m.get('ca')
  194. if m.get('max_attempts') is not None:
  195. self.max_attempts = m.get('max_attempts')
  196. if m.get('backoff_policy') is not None:
  197. self.backoff_policy = m.get('backoff_policy')
  198. if m.get('backoff_period') is not None:
  199. self.backoff_period = m.get('backoff_period')
  200. if m.get('readTimeout') is not None:
  201. self.read_timeout = m.get('readTimeout')
  202. if m.get('connectTimeout') is not None:
  203. self.connect_timeout = m.get('connectTimeout')
  204. if m.get('httpProxy') is not None:
  205. self.http_proxy = m.get('httpProxy')
  206. if m.get('httpsProxy') is not None:
  207. self.https_proxy = m.get('httpsProxy')
  208. if m.get('noProxy') is not None:
  209. self.no_proxy = m.get('noProxy')
  210. if m.get('maxIdleConns') is not None:
  211. self.max_idle_conns = m.get('maxIdleConns')
  212. if m.get('localAddr') is not None:
  213. self.local_addr = m.get('localAddr')
  214. if m.get('socks5Proxy') is not None:
  215. self.socks_5proxy = m.get('socks5Proxy')
  216. if m.get('socks5NetWork') is not None:
  217. self.socks_5net_work = m.get('socks5NetWork')
  218. if m.get('keepAlive') is not None:
  219. self.keep_alive = m.get('keepAlive')
  220. if m.get('extendsParameters') is not None:
  221. temp_model = ExtendsParameters()
  222. self.extends_parameters = temp_model.from_map(m['extendsParameters'])
  223. if m.get('webSocketPingInterval') is not None:
  224. self.web_socket_ping_interval = m.get('webSocketPingInterval')
  225. if m.get('webSocketPongTimeout') is not None:
  226. self.web_socket_pong_timeout = m.get('webSocketPongTimeout')
  227. if m.get('webSocketEnableReconnect') is not None:
  228. self.web_socket_enable_reconnect = m.get('webSocketEnableReconnect')
  229. if m.get('webSocketReconnectInterval') is not None:
  230. self.web_socket_reconnect_interval = m.get('webSocketReconnectInterval')
  231. if m.get('webSocketMaxReconnectTimes') is not None:
  232. self.web_socket_max_reconnect_times = m.get('webSocketMaxReconnectTimes')
  233. if m.get('webSocketWriteTimeout') is not None:
  234. self.web_socket_write_timeout = m.get('webSocketWriteTimeout')
  235. if m.get('webSocketHandshakeTimeout') is not None:
  236. self.web_socket_handshake_timeout = m.get('webSocketHandshakeTimeout')
  237. return self