Skip to content

Commit 429a495

Browse files
authored
Merge pull request #32 from TransposeData/dev
v3.1.0
2 parents 4ab9491 + 33f6c43 commit 429a495

File tree

5 files changed

+83
-35
lines changed

5 files changed

+83
-35
lines changed

README.md

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,55 @@ 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+
39+
### SDK Classes
40+
The Transpose SDK uses custom classes to represent API responses:
41+
42+
#### Error Classes
43+
<details>
44+
<summary>SDK Error Class Specifications</summary>
45+
The SDK uses the following error classes to represent API errors:
46+
47+
- ``TransposeBadRequest``
48+
- Represents a 400 Bad Request error from the Transpose API.
49+
- ``TransposeRateLimit``
50+
- Represents a 429 Rate Limit error from the Transpose API.
51+
- ``TransposeInvalidAPIKey``
52+
- Represents a 401 Unauthorized error from the Transpose API.
53+
- ``TransposeInternalServerError``
54+
- Represents a 500 Internal Server Error error from the Transpose API.
55+
- ``TransposeResourceNotFound``
56+
- Represents a 404 Not Found error from the Transpose API.
57+
58+
These errors will be raised when the SDK encounters an error from the Transpose API.
59+
</details>
60+
61+
#### Response Classes (**DEPRECATED AS OF v3.1.0**)
62+
<details>
63+
<summary>Response Class Specifications</summary>
64+
65+
The SDK will always return a list of response objects from the Transpose API. For example, calling the ``ens.records_by_date`` endpoint will return a list of ``ENSRecord`` objects.
66+
67+
These response objects can be accessed in the following ways:
68+
- ``ENSRecord[0].ens_name`` will return the first record's ens_name.
69+
- ``ENSRecord[i].ens_name`` retrieves the ens_name from the i-th response
70+
71+
All response objects can also be accessed as a dictionary by calling ``.to_dict()`` on them:
72+
- ``ENSRecord[0].to_dict()`` will return the first record as a dictionary.
73+
- ``ENSRecord[i].to_dict()`` retrieves the i-th record as a dictionary.
74+
</details>
75+
76+
---
77+
78+
## SDK Options
79+
The Transpose SDK can be configured to your liking, allowing you to change the default behavior of the SDK.
80+
3881
### Updating Chain ID
82+
<details>
83+
<summary>
84+
Updating SDK Working Chain ID
85+
</summary>
86+
3987
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:
4088

4189
```python
@@ -66,42 +114,21 @@ api.set_chain(1)
66114
| :------: | :--------: |
67115
| 1 | Ethereum |
68116
| 137 | Polygon |
69-
70-
### SDK Classes
71-
The Transpose SDK uses custom classes to represent API responses:
72-
73-
#### Error Classes
74-
<details>
75-
<summary>SDK Error Class Specifications</summary>
76-
The SDK uses the following error classes to represent API errors:
77-
78-
- ``TransposeBadRequest``
79-
- Represents a 400 Bad Request error from the Transpose API.
80-
- ``TransposeRateLimit``
81-
- Represents a 429 Rate Limit error from the Transpose API.
82-
- ``TransposeInvalidAPIKey``
83-
- Represents a 401 Unauthorized error from the Transpose API.
84-
- ``TransposeInternalServerError``
85-
- Represents a 500 Internal Server Error error from the Transpose API.
86-
- ``TransposeResourceNotFound``
87-
- Represents a 404 Not Found error from the Transpose API.
88-
89-
These errors will be raised when the SDK encounters an error from the Transpose API.
90117
</details>
91118

92-
#### Response Classes
119+
### Raw JSON Responses
93120
<details>
94-
<summary>Response Class Specifications</summary>
121+
<summary>
122+
Opt-in to raw JSON Responses
123+
</summary>
124+
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.
95125

96-
The SDK will always return a list of response objects from the Transpose API. For example, calling the ``ens.records_by_date`` endpoint will return a list of ``ENSRecord`` objects.
126+
**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**
97127

98-
These response objects can be accessed in the following ways:
99-
- ``ENSRecord[0].ens_name`` will return the first record's ens_name.
100-
- ``ENSRecord[i].ens_name`` retrieves the ens_name from the i-th response
101-
102-
All response objects can also be accessed as a dictionary by calling ``.to_dict()`` on them:
103-
- ``ENSRecord[0].to_dict()`` will return the first record as a dictionary.
104-
- ``ENSRecord[i].to_dict()`` retrieves the i-th record as a dictionary.
128+
```python
129+
from transpose_sdk import Transpose
130+
api = Transpose(api_key="YOUR_API_KEY", json=True)
131+
```
105132
</details>
106133

107134
### Pagination
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)