Skip to content

Commit

Permalink
Merge pull request #35 from blockfrost/fix/api-version-ryo
Browse files Browse the repository at this point in the history
fix: allow to omit api version with custom backend url
  • Loading branch information
sorki authored Nov 8, 2023
2 parents 6403bbf + 5937d64 commit 6b3e054
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 13 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,26 @@ def webhook():
if __name__ == "__main__":
app.run(host='0.0.0.0', port=6666)
```

## Development

Install dependencies

```
pip install -r requirements.txt
pip install -r rest-requirements.txt
```

Install package

```
pip install .
```

Run integration and unit tests:

```
pytest
```

_For integration tests you need to set env variable `BLOCKFROST_PROJECT_ID_MAINNET`_
9 changes: 7 additions & 2 deletions blockfrost/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import requests
from dataclasses import dataclass

from blockfrost.config import DEFAULT_API_VERSION

from ..utils import Api, ApiUrls, request_wrapper


Expand All @@ -10,8 +12,11 @@ class BlockFrostApi(Api):
def __init__(self, project_id: str = None, base_url: str = None, api_version: str = None):
super().__init__(
project_id=project_id,
base_url=base_url if base_url else os.environ.get('BLOCKFROST_API_URL', default=ApiUrls.mainnet.value),
api_version=api_version)
base_url=base_url if base_url else os.environ.get(
'BLOCKFROST_API_URL', default=ApiUrls.mainnet.value),
# if custom base_url is specified then also use specified api_version
api_version=api_version if base_url else os.environ.get('BLOCKFROST_API_VERSION',
default=DEFAULT_API_VERSION))

@request_wrapper
def root(self, **kwargs):
Expand Down
9 changes: 6 additions & 3 deletions blockfrost/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from enum import Enum
import pkg_resources

try:
from importlib.metadata import version
except ImportError: # for Python<3.8
from importlib_metadata import version

class ApiUrls(Enum):
mainnet = 'https://cardano-mainnet.blockfrost.io/api'
Expand All @@ -18,4 +20,5 @@ class ApiUrls(Enum):
ADDRESS_GAP_LIMIT = 20

package_name = 'blockfrost-python'
USER_AGENT = f'{package_name} {pkg_resources.get_distribution(package_name).version}'
version = version(package_name)
USER_AGENT = f'{package_name} {version}'
7 changes: 5 additions & 2 deletions blockfrost/ipfs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

from blockfrost.config import DEFAULT_API_VERSION
from ..utils import Api, ApiUrls


Expand All @@ -8,8 +9,10 @@ class BlockFrostIPFS(Api):
def __init__(self, project_id: str = None, base_url: str = None, api_version: str = None):
super().__init__(
project_id=project_id,
base_url=base_url if base_url else os.environ.get('BLOCKFROST_IPFS_URL', default=ApiUrls.ipfs.value),
api_version=api_version)
base_url=base_url if base_url else os.environ.get(
'BLOCKFROST_IPFS_URL', default=ApiUrls.ipfs.value),
api_version=api_version if base_url else os.environ.get('BLOCKFROST_API_VERSION',
default=DEFAULT_API_VERSION))

from .add import add
from .gateway import gateway
Expand Down
11 changes: 6 additions & 5 deletions blockfrost/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def convert_json_to_pandas(json_response):
import pandas as pd
return pd.json_normalize(json_response)
except ImportError as error:
raise ImportError("To use \"return_type='pandas'\" you must pip install pandas")
raise ImportError(
"To use \"return_type='pandas'\" you must pip install pandas")


def simple_request_wrapper(func):
Expand Down Expand Up @@ -125,14 +126,14 @@ def __init__(
base_url: str = None,
api_version: str = None,
):
self.project_id = project_id if project_id else os.environ.get('BLOCKFROST_PROJECT_ID')
self.api_version = api_version if api_version else os.environ.get('BLOCKFROST_API_VERSION',
default=DEFAULT_API_VERSION)
self.project_id = project_id if project_id else os.environ.get(
'BLOCKFROST_PROJECT_ID')
self.api_version = api_version
self.base_url = base_url

@property
def url(self):
return f"{self.base_url}/{self.api_version}"
return f"{self.base_url}/{self.api_version}" if self.api_version else f"{self.base_url}"

@property
def authentication_header(self):
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
keywords='blockfrost blockchain cardano ipfs',
packages=find_packages(exclude=['tests', 'tests.*']),
python_requires='>=3.7, <4',
requires= [
"importlib_metadata",
],
install_requires=[
"requests",
],
Expand Down
27 changes: 27 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
(pkgs.python3.withPackages (ps: [
ps.requests
# tests
ps.setuptools
ps.pytest
ps.mock
ps.requests-mock
ps.pandas
])
)
];

shellHook = ''
echo
echo '# blockfrost-python development shell'
echo
echo '## to run unit tests, use'
echo 'pytest'
echo
echo '## to run integration tests, use'
echo 'export BLOCKFROST_PROJECT_ID_MAINNET=mainnet..'
echo 'pytest'
'';
}
2 changes: 1 addition & 1 deletion tests/test_cardano_addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_address_utxos(requests_mock):
def test_integration_address_utxos():
if os.getenv('BLOCKFROST_PROJECT_ID_MAINNET'):
api = BlockFrostApi(project_id=os.getenv('BLOCKFROST_PROJECT_ID_MAINNET'))
assert api.address_utxos(address=address) == []
assert api.address_utxos(address=address)


def test_address_utxos_asset(requests_mock):
Expand Down

0 comments on commit 6b3e054

Please sign in to comment.