Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for custom http headers in RpcClient #861

Merged
merged 3 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/solana/lib/src/rpc/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ abstract class RpcClient {
factory RpcClient(
String url, {
Duration timeout = const Duration(seconds: 30),
Map<String, String> customHeaders = const {},
}) =>
_RpcClient(url, JsonRpcClient(url, timeout: timeout));
_RpcClient(
url,
JsonRpcClient(
url,
timeout: timeout,
customHeaders: customHeaders,
),
);

abstract final JsonRpcClient _jsonRpcClient;

Expand Down
9 changes: 6 additions & 3 deletions packages/solana/lib/src/rpc/json_rpc_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class JsonRpcClient {
JsonRpcClient(
this._url, {
required Duration timeout,
}) : _timeout = timeout;
required Map<String, String> customHeaders,
}) : _timeout = timeout,
_headers = {..._defaultHeaders, ...customHeaders};

final String _url;
final Duration _timeout;
final Map<String, String> _headers;
int _lastId = 1;

Future<List<Map<String, dynamic>>> bulkRequest(
Expand Down Expand Up @@ -69,7 +72,7 @@ class JsonRpcClient {
// Perform the POST request
final Response response = await post(
Uri.parse(_url),
headers: _defaultHeaders,
headers: _headers,
body: json.encode(body),
).timeout(
_timeout,
Expand Down Expand Up @@ -143,5 +146,5 @@ class _JsonRpcArrayResponse implements _JsonRpcResponse {
}

const _defaultHeaders = <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Content-Type': 'application/json',
};