stack_wallet/lib/services/litescribe_api.dart

68 lines
2.7 KiB
Dart
Raw Normal View History

2023-07-20 19:38:56 +00:00
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:stackwallet/dto/ordinals/inscription_data.dart';
2023-07-20 19:38:56 +00:00
import 'package:stackwallet/dto/ordinals/litescribe_response.dart';
class LitescribeAPI {
static final LitescribeAPI _instance = LitescribeAPI._internal();
factory LitescribeAPI({required String baseUrl}) {
_instance.baseUrl = baseUrl;
return _instance;
}
LitescribeAPI._internal();
late String baseUrl;
Future<LitescribeResponse> _getResponse(String endpoint) async {
final response = await http.get(Uri.parse('$baseUrl$endpoint'));
if (response.statusCode == 200) {
return LitescribeResponse(data: _validateJson(response.body));
} else {
throw Exception('LitescribeAPI _getResponse 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('LitescribeAPI _validateJson exception: Invalid JSON format');
}
}
Future<List<InscriptionData>> getInscriptionsByAddress(String address, {int cursor = 0, int size = 1000}) async {
2023-07-20 19:46:15 +00:00
// size param determines how many inscriptions are returned per response
// default of 1000 is used to cover most addresses (I assume)
// if the total number of inscriptions at the address exceeds the length of the list of inscriptions returned, another call with a higher size is made
final int defaultLimit = 1000;
2023-07-20 19:38:56 +00:00
final response = await _getResponse('/address/inscriptions?address=$address&cursor=$cursor&size=$size');
2023-07-20 19:46:15 +00:00
// Check if the number of returned inscriptions equals the limit
2023-07-20 21:49:26 +00:00
final list = response.data['result']['list'];
2023-07-20 19:46:15 +00:00
final int total = response.data['result']['total'] as int;
final int currentSize = list.length as int;
2023-07-20 19:46:15 +00:00
if (currentSize == size && currentSize < total) {
// If the number of returned inscriptions equals the limit and there are more inscriptions available,
2023-07-20 19:48:12 +00:00
// increment the cursor and make the next API call to fetch the remaining inscriptions.
final int newCursor = cursor + size;
return getInscriptionsByAddress(address, cursor: newCursor, size: size);
2023-07-20 21:49:26 +00:00
2023-07-20 19:46:15 +00:00
} else {
try {
2023-07-20 21:49:26 +00:00
// Iterate through the list and create InscriptionData objects from each element
final List<InscriptionData> inscriptions = (list as List<dynamic>)
.map((json) => InscriptionData.fromJson(json as Map<String, dynamic>))
.toList();
return inscriptions;
2023-07-20 19:46:15 +00:00
} catch (e) {
throw const FormatException('LitescribeAPI getInscriptionsByAddress exception: AddressInscriptionResponse.fromJson failure');
}
2023-07-20 19:38:56 +00:00
}
}
}