simolus3 / web3dart

Ethereum library, written in Dart.
MIT License
442 stars 273 forks source link

Add optional additional headers #174

Closed ed0xe closed 3 years ago

ed0xe commented 3 years ago

Hello, I am using getblock.io which recommends passing the API key in the requests headers.

'x-api-key: <YOUR-API-KEY>'

I added an additional option to pass extra headers on the instantiation of Web3Client Let me know if you think there is a better way.

simolus3 commented 3 years ago

Thank you for your contribution! Unfortunately, I think there's a better way that doesn't require changes to web3dart. Instead, I would just do this:

class ClientWithDefaultHeaders extends BaseClient {
  final Map<String, String> additionalHeaders;
  final Client inner;

  ClientWithDefaultHeaders(this.additionalHeaders, this.inner);

  @override
  Future<StreamedResponse> send(BaseRequest request) {
    additionalHeaders.forEach((key, value) {
      if (!request.headers.containsKey(key)) request.headers[key] = value;
    });
    return inner.send(request):
  }

  @override
  void close() => inner.close();
}

Then, you can just use a ClientWithDefaultHeaders({'x-api-key': '<YOUR-API-KEY>'}, Client()) for web3dart which sets the right headers by default.