Skip to content

Commit

Permalink
Merge pull request #168 from bitfag/aio-api-getattr-fix
Browse files Browse the repository at this point in the history
Fix RPC method calling in aio version
  • Loading branch information
xeroc authored Jun 12, 2020
2 parents c26845f + 893e341 commit 7eacfb6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
42 changes: 41 additions & 1 deletion grapheneapi/aio/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import asyncio
import logging
from grapheneapi.exceptions import NumRetriesReached
from grapheneapi.exceptions import NumRetriesReached, RPCError

from grapheneapi.api import Api as SyncApi
from .websocket import Websocket
Expand Down Expand Up @@ -91,3 +91,43 @@ async def find_next(self):
raise NumRetriesReached
url = urls[0]
return url

def __getattr__(self, name):
async def func(*args, **kwargs):
while True:
try:
func = self.connection.__getattr__(name)
r = await func(*args, **kwargs)
self.reset_counter()
break
except KeyboardInterrupt: # pragma: no cover
raise
except RPCError as e: # pragma: no cover
""" When the backend actual returns an error
"""
self.post_process_exception(e)
# the above line should raise. Let's be sure to at least
# break
break # pragma: no cover
except IOError: # pragma: no cover
import traceback

log.debug(traceback.format_exc())
log.warning("Connection was closed remotely.")
log.warning("Reconnecting ...")
self.error_url()
self.next()
except Exception as e: # pragma: no cover
""" When something fails talking to the backend
"""
import traceback

log.debug(traceback.format_exc())
log.warning(str(e))
log.warning("Reconnecting ...")
self.error_url()
self.next()

return r

return func
6 changes: 6 additions & 0 deletions grapheneapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,15 @@ def register_apis(self): # pragma: no cover
pass

def __getattr__(self, name):
""" Proxies RPC calls to actual Websocket or Http instance.
Connection-related errors catched here and handled.
"""

def func(*args, **kwargs):
while True:
try:
# RPC method called on actual Websocket or Http class
func = self.connection.__getattr__(name)
r = func(*args, **kwargs)
self.reset_counter()
Expand Down

0 comments on commit 7eacfb6

Please sign in to comment.