Skip to content

Commit 6b3e054

Browse files
authored
Merge pull request #35 from blockfrost/fix/api-version-ryo
fix: allow to omit api version with custom backend url
2 parents 6403bbf + 5937d64 commit 6b3e054

8 files changed

Lines changed: 78 additions & 13 deletions

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,26 @@ def webhook():
168168
if __name__ == "__main__":
169169
app.run(host='0.0.0.0', port=6666)
170170
```
171+
172+
## Development
173+
174+
Install dependencies
175+
176+
```
177+
pip install -r requirements.txt
178+
pip install -r rest-requirements.txt
179+
```
180+
181+
Install package
182+
183+
```
184+
pip install .
185+
```
186+
187+
Run integration and unit tests:
188+
189+
```
190+
pytest
191+
```
192+
193+
_For integration tests you need to set env variable `BLOCKFROST_PROJECT_ID_MAINNET`_

blockfrost/api/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import requests
33
from dataclasses import dataclass
44

5+
from blockfrost.config import DEFAULT_API_VERSION
6+
57
from ..utils import Api, ApiUrls, request_wrapper
68

79

@@ -10,8 +12,11 @@ class BlockFrostApi(Api):
1012
def __init__(self, project_id: str = None, base_url: str = None, api_version: str = None):
1113
super().__init__(
1214
project_id=project_id,
13-
base_url=base_url if base_url else os.environ.get('BLOCKFROST_API_URL', default=ApiUrls.mainnet.value),
14-
api_version=api_version)
15+
base_url=base_url if base_url else os.environ.get(
16+
'BLOCKFROST_API_URL', default=ApiUrls.mainnet.value),
17+
# if custom base_url is specified then also use specified api_version
18+
api_version=api_version if base_url else os.environ.get('BLOCKFROST_API_VERSION',
19+
default=DEFAULT_API_VERSION))
1520

1621
@request_wrapper
1722
def root(self, **kwargs):

blockfrost/config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from enum import Enum
2-
import pkg_resources
3-
2+
try:
3+
from importlib.metadata import version
4+
except ImportError: # for Python<3.8
5+
from importlib_metadata import version
46

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

2022
package_name = 'blockfrost-python'
21-
USER_AGENT = f'{package_name} {pkg_resources.get_distribution(package_name).version}'
23+
version = version(package_name)
24+
USER_AGENT = f'{package_name} {version}'

blockfrost/ipfs/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22

3+
from blockfrost.config import DEFAULT_API_VERSION
34
from ..utils import Api, ApiUrls
45

56

@@ -8,8 +9,10 @@ class BlockFrostIPFS(Api):
89
def __init__(self, project_id: str = None, base_url: str = None, api_version: str = None):
910
super().__init__(
1011
project_id=project_id,
11-
base_url=base_url if base_url else os.environ.get('BLOCKFROST_IPFS_URL', default=ApiUrls.ipfs.value),
12-
api_version=api_version)
12+
base_url=base_url if base_url else os.environ.get(
13+
'BLOCKFROST_IPFS_URL', default=ApiUrls.ipfs.value),
14+
api_version=api_version if base_url else os.environ.get('BLOCKFROST_API_VERSION',
15+
default=DEFAULT_API_VERSION))
1316

1417
from .add import add
1518
from .gateway import gateway

blockfrost/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def convert_json_to_pandas(json_response):
4242
import pandas as pd
4343
return pd.json_normalize(json_response)
4444
except ImportError as error:
45-
raise ImportError("To use \"return_type='pandas'\" you must pip install pandas")
45+
raise ImportError(
46+
"To use \"return_type='pandas'\" you must pip install pandas")
4647

4748

4849
def simple_request_wrapper(func):
@@ -125,14 +126,14 @@ def __init__(
125126
base_url: str = None,
126127
api_version: str = None,
127128
):
128-
self.project_id = project_id if project_id else os.environ.get('BLOCKFROST_PROJECT_ID')
129-
self.api_version = api_version if api_version else os.environ.get('BLOCKFROST_API_VERSION',
130-
default=DEFAULT_API_VERSION)
129+
self.project_id = project_id if project_id else os.environ.get(
130+
'BLOCKFROST_PROJECT_ID')
131+
self.api_version = api_version
131132
self.base_url = base_url
132133

133134
@property
134135
def url(self):
135-
return f"{self.base_url}/{self.api_version}"
136+
return f"{self.base_url}/{self.api_version}" if self.api_version else f"{self.base_url}"
136137

137138
@property
138139
def authentication_header(self):

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
keywords='blockfrost blockchain cardano ipfs',
2222
packages=find_packages(exclude=['tests', 'tests.*']),
2323
python_requires='>=3.7, <4',
24+
requires= [
25+
"importlib_metadata",
26+
],
2427
install_requires=[
2528
"requests",
2629
],

shell.nix

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{ pkgs ? import <nixpkgs> {} }:
2+
pkgs.mkShell {
3+
buildInputs = [
4+
(pkgs.python3.withPackages (ps: [
5+
ps.requests
6+
# tests
7+
ps.setuptools
8+
ps.pytest
9+
ps.mock
10+
ps.requests-mock
11+
ps.pandas
12+
])
13+
)
14+
];
15+
16+
shellHook = ''
17+
echo
18+
echo '# blockfrost-python development shell'
19+
echo
20+
echo '## to run unit tests, use'
21+
echo 'pytest'
22+
echo
23+
echo '## to run integration tests, use'
24+
echo 'export BLOCKFROST_PROJECT_ID_MAINNET=mainnet..'
25+
echo 'pytest'
26+
'';
27+
}

tests/test_cardano_addresses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test_address_utxos(requests_mock):
158158
def test_integration_address_utxos():
159159
if os.getenv('BLOCKFROST_PROJECT_ID_MAINNET'):
160160
api = BlockFrostApi(project_id=os.getenv('BLOCKFROST_PROJECT_ID_MAINNET'))
161-
assert api.address_utxos(address=address) == []
161+
assert api.address_utxos(address=address)
162162

163163

164164
def test_address_utxos_asset(requests_mock):

0 commit comments

Comments
 (0)