import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:stackwallet/dto/ordinals/ordinals_response.dart'; import 'package:stackwallet/dto/ordinals/feed_response.dart'; import 'package:stackwallet/dto/ordinals/inscription_response.dart'; import 'package:stackwallet/dto/ordinals/sat_response.dart'; import 'package:stackwallet/dto/ordinals/transaction_response.dart'; import 'package:stackwallet/dto/ordinals/output_response.dart'; import 'package:stackwallet/dto/ordinals/address_response.dart'; import 'package:stackwallet/dto/ordinals/block_response.dart'; import 'package:stackwallet/dto/ordinals/content_response.dart'; import 'package:stackwallet/dto/ordinals/preview_response.dart'; class OrdinalsAPI { final String baseUrl; OrdinalsAPI({required this.baseUrl}); Future _getResponse(String endpoint) async { final response = await http.get(Uri.parse('$baseUrl$endpoint')); if (response.statusCode == 200) { return OrdinalsResponse(data: _validateJson(response.body)); } else { throw Exception('Failed to load data'); } } Map _validateJson(String responseBody) { final parsed = jsonDecode(responseBody); if (parsed is Map) { return parsed; } else { throw const FormatException('Invalid JSON format'); } } Future getLatestInscriptions() async { final response = await _getResponse('/feed'); return FeedResponse.fromJson(response); } Future getInscriptionDetails(String inscriptionId) async { final response = await _getResponse('/inscription/$inscriptionId'); return InscriptionResponse.fromJson(response); } Future getSatDetails(int satNumber) async { final response = await _getResponse('/sat/$satNumber'); return SatResponse.fromJson(response); } Future getTransaction(String transactionId) async { final response = await _getResponse('/tx/$transactionId'); return TransactionResponse.fromJson(response); } Future getTransactionOutputs(String transactionId) async { final response = await _getResponse('/output/$transactionId'); return OutputResponse.fromJson(response); } Future getInscriptionsByAddress(String address) async { final response = await _getResponse('/address/$address'); return AddressResponse.fromJson(response); } Future getBlock(int blockNumber) async { final response = await _getResponse('/block/$blockNumber'); return BlockResponse.fromJson(response); } Future getInscriptionContent(String inscriptionId) async { final response = await _getResponse('/content/$inscriptionId'); return ContentResponse.fromJson(response); } Future getInscriptionPreview(String inscriptionId) async { final response = await _getResponse('/preview/$inscriptionId'); return PreviewResponse.fromJson(response); } }