Skip to content

Commit

Permalink
Add price rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
holgern committed Mar 21, 2024
1 parent 7ce59a6 commit 4e91d58
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ default_language_version:
python: python3
repos:
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 24.3.0
hooks:
- id: black
exclude: ^(dev/)
args: [--skip-string-normalization]
- repo: https://github.com/myint/autoflake
rev: v2.0.0
rev: v2.3.1
hooks:
- id: autoflake
exclude: ^(dev/)
Expand All @@ -18,7 +18,7 @@ repos:
- --remove-all-unused-imports
- --remove-duplicate-keys
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-toml
- id: check-yaml
Expand All @@ -27,7 +27,7 @@ repos:
- id: trailing-whitespace
exclude: ^(dev/)
- repo: https://github.com/PyCQA/flake8
rev: "6.0.0"
rev: "7.0.0"
hooks:
- id: flake8
name: flake8 except __init__.py
Expand All @@ -40,20 +40,20 @@ repos:
args: [--config, dev/.flake8, "--extend-ignore=F401"] # ignore unused imports in __init__.py
files: /__init__\.py$
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black"]
name: isort except __init__.py
exclude: (^(dev/)|/__init__\.py$)
- repo: https://github.com/myint/docformatter
rev: v1.4
rev: v1.7.5
hooks:
- id: docformatter
exclude: ^(dev/)
args: ["--in-place", "--wrap-summaries=88"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
rev: v3.15.1
hooks:
- id: pyupgrade
exclude: ^(dev/)
Expand Down
39 changes: 30 additions & 9 deletions pymempool/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ def __send(self, url, data):

raise

def get_price(self):
"""Returns bitcoin latest price denominated in main currencies."""
api_url = 'v1/prices'
return self._request(api_url)

def get_historical_price(self, currency=None, timestamp=None):
"""Returns bitcoin historical price denominated in main currencies.
Available query parameters: currency, timestamp.
If no parameter is provided, the full price history
for all currencies is returned.
"""
api_url = 'v1/historical-price'
connector = "?"
if currency is not None:
currency = str(currency).upper()
api_url += f'{connector}currency={currency}'
connector = "&"
if timestamp is not None:
timestamp = int(timestamp)
api_url += f'{connector}timestamp={timestamp}'
return self._request(api_url)

def get_difficulty_adjustment(self):
"""Returns details about difficulty adjustment."""
api_url = 'v1/difficulty-adjustment'
Expand Down Expand Up @@ -230,9 +253,7 @@ def get_block_transactions(self, hash_value, start_index=None):
hash_value = hash_value.replace(' ', '')
if start_index is not None:
start_index = int(start_index)
api_url = '{}block/{}/txs/{}'.format(
self.get_api_base_url(), hash_value, start_index
)
api_url = f'block/{hash_value}/txs/{start_index}'
else:
api_url = f'block/{hash_value}/txs'
return self._request(api_url)
Expand Down Expand Up @@ -322,8 +343,8 @@ def get_block_fees(self, time_period):
"""Returns average total fees for blocks in the specified :timePeriod, ordered
oldest to newest.
:timePeriod can be any of the following:
24h, 3d, 1w, 1m, 3m, 6m, 1y, 2y, 3y.
:timePeriod can be any of the following: 24h, 3d, 1w, 1m, 3m,
6m, 1y, 2y, 3y.
"""
api_url = f'v1/mining/blocks/fees/{time_period}'
return self._request(api_url)
Expand All @@ -332,8 +353,8 @@ def get_block_rewards(self, time_period):
"""Returns average block rewards for blocks in the specified :timePeriod,
ordered oldest to newest.
:timePeriod can be any of the following:
24h, 3d, 1w, 1m, 3m, 6m, 1y, 2y, 3y.
:timePeriod can be any of the following: 24h, 3d, 1w, 1m, 3m,
6m, 1y, 2y, 3y.
"""
api_url = f'v1/mining/blocks/rewards/{time_period}'
return self._request(api_url)
Expand All @@ -351,8 +372,8 @@ def get_block_sizes_and_weights(self, time_period):
"""Returns average size (bytes) and average weight (weight units) for blocks in
the specified :timePeriod, ordered oldest to newest.
:timePeriod can be any of the following:
24h, 3d, 1w, 1m, 3m, 6m, 1y, 2y, 3y.
:timePeriod can be any of the following: 24h, 3d, 1w, 1m, 3m,
6m, 1y, 2y, 3y.
"""
api_url = f'v1/mining/blocks/sizes-weights/{time_period}'
return self._request(api_url)
Expand Down

0 comments on commit 4e91d58

Please sign in to comment.