stack_wallet/lib/services/ordinals_api.dart

90 lines
3.1 KiB
Dart
Raw Normal View History

2023-07-19 15:37:42 +00:00
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:stackwallet/dto/ordinals/ordinals_response.dart';
2023-07-19 15:37:42 +00:00
import 'package:stackwallet/dto/ordinals/feed_response.dart';
import 'package:stackwallet/dto/ordinals/inscription_response.dart';
2023-07-19 15:41:23 +00:00
import 'package:stackwallet/dto/ordinals/sat_response.dart';
2023-07-19 15:45:18 +00:00
import 'package:stackwallet/dto/ordinals/transaction_response.dart';
import 'package:stackwallet/dto/ordinals/output_response.dart';
2023-07-19 15:48:54 +00:00
import 'package:stackwallet/dto/ordinals/address_response.dart';
2023-07-19 15:50:56 +00:00
import 'package:stackwallet/dto/ordinals/block_response.dart';
2023-07-19 15:51:46 +00:00
import 'package:stackwallet/dto/ordinals/content_response.dart';
2023-07-19 15:55:46 +00:00
import 'package:stackwallet/dto/ordinals/preview_response.dart';
2023-07-18 16:15:05 +00:00
class OrdinalsAPI {
static final OrdinalsAPI _instance = OrdinalsAPI._internal();
factory OrdinalsAPI({required String baseUrl}) {
_instance.baseUrl = baseUrl;
return _instance;
}
OrdinalsAPI._internal();
2023-07-19 15:37:42 +00:00
late String baseUrl;
2023-07-19 15:37:42 +00:00
Future<OrdinalsResponse> _getResponse(String endpoint) async {
2023-07-19 15:37:42 +00:00
final response = await http.get(Uri.parse('$baseUrl$endpoint'));
if (response.statusCode == 200) {
return OrdinalsResponse(data: _validateJson(response.body));
2023-07-19 15:37:42 +00:00
} else {
throw Exception('Failed to load data');
}
}
Map<String, dynamic> _validateJson(String responseBody) {
final parsed = jsonDecode(responseBody);
if (parsed is Map<String, dynamic>) {
return parsed;
} else {
throw const FormatException('Invalid JSON format');
}
}
Future<FeedResponse> getLatestInscriptions() async {
final response = await _getResponse('/feed');
return FeedResponse.fromJson(response);
}
2023-07-18 16:15:05 +00:00
2023-07-19 15:37:42 +00:00
Future<InscriptionResponse> getInscriptionDetails(String inscriptionId) async {
final response = await _getResponse('/inscription/$inscriptionId');
return InscriptionResponse.fromJson(response);
2023-07-18 16:15:05 +00:00
}
2023-07-19 15:41:23 +00:00
Future<SatResponse> getSatDetails(int satNumber) async {
final response = await _getResponse('/sat/$satNumber');
return SatResponse.fromJson(response);
}
2023-07-19 15:45:18 +00:00
Future<TransactionResponse> getTransaction(String transactionId) async {
final response = await _getResponse('/tx/$transactionId');
return TransactionResponse.fromJson(response);
}
Future<OutputResponse> getTransactionOutputs(String transactionId) async {
final response = await _getResponse('/output/$transactionId');
return OutputResponse.fromJson(response);
}
2023-07-19 15:48:54 +00:00
Future<AddressResponse> getInscriptionsByAddress(String address) async {
final response = await _getResponse('/address/$address');
return AddressResponse.fromJson(response);
}
2023-07-19 15:50:56 +00:00
Future<BlockResponse> getBlock(int blockNumber) async {
final response = await _getResponse('/block/$blockNumber');
return BlockResponse.fromJson(response);
}
2023-07-19 15:51:46 +00:00
Future<ContentResponse> getInscriptionContent(String inscriptionId) async {
final response = await _getResponse('/content/$inscriptionId');
return ContentResponse.fromJson(response);
}
2023-07-19 15:55:46 +00:00
Future<PreviewResponse> getInscriptionPreview(String inscriptionId) async {
final response = await _getResponse('/preview/$inscriptionId');
return PreviewResponse.fromJson(response);
}
2023-07-18 16:15:05 +00:00
}