stack_wallet/lib/wallets/api/cardano/blockfrost_http_provider.dart

54 lines
1.9 KiB
Dart
Raw Normal View History

2024-08-26 17:38:58 +00:00
import 'dart:convert';
2024-09-01 13:19:46 +00:00
import 'dart:io';
2024-09-14 18:38:57 +00:00
import 'package:cbor/simple.dart';
2024-08-26 17:38:58 +00:00
import 'package:on_chain/ada/src/provider/blockfrost/core/core.dart';
import 'package:on_chain/ada/src/provider/service/service.dart';
2024-09-14 18:38:57 +00:00
import '../../../utilities/logger.dart';
2024-08-26 17:38:58 +00:00
class BlockfrostHttpProvider implements BlockfrostServiceProvider {
2024-09-01 13:19:46 +00:00
BlockfrostHttpProvider({
required this.url,
this.version = "v0",
this.projectId,
HttpClient? client,
this.defaultRequestTimeout = const Duration(seconds: 30),
}) : client = client ?? HttpClient();
2024-08-26 17:38:58 +00:00
@override
final String url;
final String version;
final String? projectId;
2024-09-01 13:19:46 +00:00
final HttpClient client;
2024-08-26 17:38:58 +00:00
final Duration defaultRequestTimeout;
@override
Future<dynamic> get(BlockforestRequestDetails params,
2024-09-01 13:19:46 +00:00
[Duration? timeout,]) async {
final response = await client.getUrl(Uri.parse(params.url(url, "api/$version"))).timeout(timeout ?? defaultRequestTimeout);
response.headers.add("Content-Type", "application/json");
response.headers.add("Accept", "application/json");
if (projectId != null) {
response.headers.add("project_id", projectId!);
}
final responseStream = await response.close();
final data = json.decode(await responseStream.transform(utf8.decoder).join());
2024-08-26 17:38:58 +00:00
return data;
}
@override
Future<dynamic> post(BlockforestRequestDetails params,
2024-09-01 13:19:46 +00:00
[Duration? timeout,]) async {
final request = await client.postUrl(Uri.parse(params.url(url, "api/$version"))).timeout(timeout ?? defaultRequestTimeout);
2024-09-14 18:38:57 +00:00
// Need to change this for other operations than submitting transactions
request.headers.add("Content-Type", "application/cbor");
2024-09-01 13:19:46 +00:00
request.headers.add("Accept", "application/json");
if (projectId != null) {
request.headers.add("project_id", projectId!);
}
2024-09-14 18:38:57 +00:00
request.add(params.body as List<int>);
2024-09-01 13:19:46 +00:00
final response = await request.close();
final data = json.decode(await response.transform(utf8.decoder).join());
2024-08-26 17:38:58 +00:00
return data;
}
2024-09-01 13:19:46 +00:00
}