Skip to content

Commit 220c4e9

Browse files
committed
✨ feat: add json option to base class, deprecate response classes
1 parent 4ab9491 commit 220c4e9

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ You can find specific documentation on a per-product basis below.
3535
## SDK Documentation
3636
You can learn more about the Transpose SDK and how it works below.
3737

38+
### Response Classes
39+
If you wish to recieve responses in JSON format, you can set the `json` parameter to `True` when initializing the SDK. This will return all responses as JSON objects.
40+
41+
**Response classes are considered deprecated as of v3.1.0 and will be removed in v4.0.0. JSON responses will become standard in v4.0.0**
42+
43+
```python
44+
from transpose_sdk import Transpose
45+
api = Transpose(api_key="YOUR_API_KEY", json=True)
46+
```
47+
3848
### Updating Chain ID
3949
If you want to change the chain ID of your query, you can do so by setting the `chain_id` or `chain` properties of the `Transpose` object. For example, if you want to query the Ethereum mainnet, you can do so by running the following code:
4050

217 KB
Loading

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# version compliant with PEP440
1010
# https://peps.python.org/pep-0440/
11-
version='3.0.1',
11+
version='3.1.0',
1212

1313
# project meta
1414
long_description = long_description,

tests/test_base_connection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ def test_invalid_api_key():
1515
except TransposeInvalidAPIKey:
1616
assert True
1717
except Exception:
18-
assert False
18+
assert False
19+
20+
def test_json_response():
21+
api = Transpose(api_key, json=True)
22+
assert api.json == True
23+
24+
block = api.Block.blocks_by_number(1)
25+
26+
assert isinstance(block, (dict,))

transpose/src/base.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,21 @@
1212

1313
# base class for the Transpose python SDK
1414
class Transpose:
15-
def __init__(self, api_key: str, debug: bool=False, host: str=None, chain_id: int=0, chain: str="ethereum") -> None:
15+
def __init__(
16+
self,
17+
api_key: str,
18+
debug: bool=False,
19+
host: str=None,
20+
chain_id: int=0,
21+
chain: str="ethereum",
22+
json: bool=False,
23+
) -> None:
24+
1625
self._next = None
1726
self._next_class_name = None
1827
self.host = host if host else 'https://api.transpose.io'
1928
self.verbose = debug
29+
self.json = json
2030

2131
if chain.lower() == "ethereum":
2232
self.chain_id = 1
@@ -55,7 +65,6 @@ def set_chain(self, chain_id: int=0, chain: str="ethereum") -> None:
5565

5666
if chain_id != 0:
5767
self.chain_id = chain_id
58-
5968

6069
# this can be renamed later. Pagination helper function to get many
6170
def bulk_request(self, endpoint_response: List, requests_per_second: int=None, results_to_fetch: int=999999999999) -> List:
@@ -103,6 +112,10 @@ def perform_authorized_request(self, model: type, endpoint: str, api_key: str=No
103112
self._next = response['next']
104113
self._next_class_name = model
105114

115+
# if we are in json mode, return the raw json
116+
if self.json:
117+
return response
118+
106119
return list(model(dict(each)) for each in response['results'])
107120
else:
108121
raise_custom_error(request.status_code, request.json()['message'])

0 commit comments

Comments
 (0)