Skip to content

Commit

Permalink
feat: allow to provide a custom base URL for Etherscan API (#146)
Browse files Browse the repository at this point in the history
see https://ledgerhq.atlassian.net/issues/MO-10789 => this will allow us to provide use our own Etherscan proxy in CI with:
```shell
export ETHERSCAN_API_HOST=etherscan.api.live.ledger.com
```
  • Loading branch information
jnicoulaud-ledger authored Nov 18, 2024
1 parent 58cee56 commit ed93331
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/erc7730/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def handle_request(self, request: Request) -> Response:
class EtherscanTransport(DelegateTransport):
"""Etherscan specific transport for handling rate limiting, API key parameter injection, response unwrapping."""

ETHERSCAN_API_HOST = "ETHERSCAN_API_HOST"
ETHERSCAN_API_KEY = "ETHERSCAN_API_KEY"

@Limiter(rate=5, capacity=5, consume=1)
Expand All @@ -164,12 +165,16 @@ def handle_request(self, request: Request) -> Response:
if request.url.host != ETHERSCAN:
return super().handle_request(request)

# add API key
if (api_key := os.environ.get(self.ETHERSCAN_API_KEY)) is None and (
# substitute base URL if provided
if (api_host := os.environ.get(self.ETHERSCAN_API_HOST)) is not None:
request.url = URL(str(request.url).replace(ETHERSCAN, api_host))
request.headers.update({"Host": api_host})

# add API key if provided
if (api_key := os.environ.get(self.ETHERSCAN_API_KEY)) is not None or (
api_key := os.environ.get(f"SCAN_{self.ETHERSCAN_API_KEY}")
) is None:
raise ValueError(f"{self.ETHERSCAN_API_KEY} environment variable is required")
request.url = request.url.copy_add_param("apikey", api_key)
) is not None:
request.url = request.url.copy_add_param("apikey", api_key)

# read response
response = super().handle_request(request)
Expand Down

0 comments on commit ed93331

Please sign in to comment.