Skip to content

Commit

Permalink
fixed with aiohttp_client_cache neoctobers#2, fixed name
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav.sabadash committed Nov 10, 2022
1 parent c5abccf commit 7b10cd6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 55 deletions.
4 changes: 4 additions & 0 deletions aioetherscan/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# coding:utf-8
from .aioetherscan import *

name = 'aioetherscan'
86 changes: 44 additions & 42 deletions etherscan/etherscan.py → aioetherscan/aioetherscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ def __init__(

@property
def _cache_type_factory(self):
payload = {
data = {
'cache_name': self._cache_name,
'expire_after': self._cache_expire_after
}
if self._cache_backend == 'CacheBackend':
return aiohttp_client_cache.CacheBackend(**payload)
return aiohttp_client_cache.CacheBackend(**data)
elif self._cache_backend == 'DynamoDBBackend':
return aiohttp_client_cache.DynamoDBBackend(**payload)
return aiohttp_client_cache.DynamoDBBackend(**data)
elif self._cache_backend == 'FileBackend':
return aiohttp_client_cache.FileBackend(**payload)
return aiohttp_client_cache.FileBackend(**data)
elif self._cache_backend == 'MongoDBBackend':
return aiohttp_client_cache.MongoDBBackend(**payload)
return aiohttp_client_cache.MongoDBBackend(**data)
elif self._cache_backend == 'RedisBackend':
return aiohttp_client_cache.RedisBackend(**payload)
return aiohttp_client_cache.RedisBackend(**data)
elif self._cache_backend == 'SQLiteBackend':
return aiohttp_client_cache.SQLiteBackend(**payload)
return aiohttp_client_cache.SQLiteBackend(**data)
else:
return aiohttp_client_cache.CacheBackend(self._cache_name or 'demo_cache')

Expand All @@ -70,11 +70,10 @@ def session(self):
self._session = aiohttp_client_cache.CachedSession(
cache=self._cache_type_factory
)

self._session.headers.update(
{
'User-agent': 'etherscan - python wrapper '
'around etherscan.io (github.com/neoctobers/etherscan)'
'User-agent': 'aioetherscan - python wrapper '
'around etherscan.io (github.com/viacheslav-sabadash/aioetherscan)'
}
)

Expand Down Expand Up @@ -137,28 +136,29 @@ async def get_eth_price(self):
'ethusd_timestamp': int(r['ethbtc_timestamp']),
}

def get_eth_supply(self):
async def get_eth_supply(self):
self._params['module'] = 'stats'
self._params['action'] = 'ethsupply'

return int(self.__req())
return int(await self.__req())

def get_eth_balance(self, address: str):
async def get_eth_balance(self, address: str):
"""Get ETH balance by address."""
self._params['module'] = 'account'
self._params['action'] = 'balance'
self._params['address'] = address

return int(self.__req())
return int(await self.__req())

def get_eth_balances(self, addresses: list):
async def get_eth_balances(self, addresses: list):
"""Get ETH balances by addresses list."""
self._params['module'] = 'account'
self._params['action'] = 'balancemulti'
self._params['address'] = ','.join(addresses)

balances = {}
for row in self.__req():
rs = await self.__req()
for row in rs:
balances[row['account']] = int(row['balance'])

return balances
Expand Down Expand Up @@ -190,15 +190,16 @@ def __transaction(self, source: dict):
'block_hash': self.__str(source['blockHash']),
}

def get_transactions_by_address(self,
address: str,
type: str = 'normal',
start_block: int = 0,
end_block: int = 999999999,
page: int = 1,
limit: int = 1000,
sort: str = 'asc',
):
async def get_transactions_by_address(
self,
address: str,
type: str = 'normal',
start_block: int = 0,
end_block: int = 999999999,
page: int = 1,
limit: int = 1000,
sort: str = 'asc',
):
"""Get transactions by address."""
self._params['module'] = 'account'

Expand All @@ -216,7 +217,7 @@ def get_transactions_by_address(self,
self._params['offset'] = limit
self._params['sort'] = sort

rs = self.__req()
rs = await self.__req()

transactions = []
for t in rs:
Expand Down Expand Up @@ -252,15 +253,16 @@ def __token_transaction(self, source: dict):
'block_hash': self.__str(source['blockHash']),
}

def get_token_transactions(self,
contract_address: str = None,
address: str = None,
start_block: int = 0,
end_block: int = 999999999,
page: int = 1,
limit: int = 1000,
sort: str = 'asc',
):
async def get_token_transactions(
self,
contract_address: str = None,
address: str = None,
start_block: int = 0,
end_block: int = 999999999,
page: int = 1,
limit: int = 1000,
sort: str = 'asc',
):
"""Get ERC20 token transactions by contract address."""
if contract_address is None and address is None:
raise EtherscanIoException('Param `contract_address` and `address` cannot be None at the same time.')
Expand All @@ -280,30 +282,30 @@ def get_token_transactions(self,
self._params['offset'] = limit
self._params['sort'] = sort

rs = self.__req()
rs = await self.__req()

token_transactions = []
for t in rs:
token_transactions.append(self.__token_transaction(t))

return token_transactions

def get_gas_price(self):
async def get_gas_price(self):
"""Get gas price."""
self._params['action'] = 'eth_gasPrice'

return int(self.__proxy_req(), 16)
return int(await self.__proxy_req(), 16)

def get_block_number(self):
async def get_block_number(self):
"""Get latest block number."""
self._params['action'] = 'eth_blockNumber'

return int(self.__proxy_req(), 16)
return int(await self.__proxy_req(), 16)

def get_block_by_number(self, block_number):
async def get_block_by_number(self, block_number):
"""Get block by number."""
self._params['action'] = 'eth_getBlockByNumber'
self._params['tag'] = hex(block_number)
self._params['boolean'] = True

return self.__proxy_req()
return await self.__proxy_req()
File renamed without changes.
18 changes: 9 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

# -- Project information -----------------------------------------------------

project = 'etherscan'
copyright = '2018, neoctobers'
author = 'neoctobers'
project = 'aioetherscan'
copyright = '2021, neoctobers'
author = 'neoctobers, viacheslav-sabadash'

# The short X.Y version
version = ''
Expand Down Expand Up @@ -106,7 +106,7 @@
# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'etherscandoc'
htmlhelp_basename = 'aioetherscandoc'


# -- Options for LaTeX output ------------------------------------------------
Expand All @@ -133,8 +133,8 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'etherscan.tex', 'etherscan Documentation',
'neoctobers', 'manual'),
(master_doc, 'aioetherscan.tex', 'aioetherscan Documentation',
'viacheslav-sabadash', 'manual'),
]


Expand All @@ -143,7 +143,7 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'etherscan', 'etherscan Documentation',
(master_doc, 'aioetherscan', 'aioetherscan Documentation',
[author], 1)
]

Expand All @@ -154,8 +154,8 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'etherscan', 'etherscan Documentation',
author, 'etherscan', 'One line description of project.',
(master_doc, 'aioetherscan', 'aioetherscan Documentation',
author, 'aioetherscan', 'One line description of project.',
'Miscellaneous'),
]

Expand Down
4 changes: 0 additions & 4 deletions etherscan/__init__.py

This file was deleted.

0 comments on commit 7b10cd6

Please sign in to comment.