From 51155372d30ca33ccccd1b66c9370da9bb6e012c Mon Sep 17 00:00:00 2001 From: sneurlax Date: Wed, 19 Jul 2023 10:37:42 -0500 Subject: [PATCH] add inscription endpoint --- lib/dto/ordinals/feed_response.dart | 24 +++- lib/dto/ordinals/inscription_response.dart | 123 ++++++++++----------- lib/services/ordinals_api.dart | 38 ++++++- 3 files changed, 112 insertions(+), 73 deletions(-) diff --git a/lib/dto/ordinals/feed_response.dart b/lib/dto/ordinals/feed_response.dart index b6af1c1ca..d958fbe4c 100644 --- a/lib/dto/ordinals/feed_response.dart +++ b/lib/dto/ordinals/feed_response.dart @@ -1,14 +1,28 @@ class FeedResponse { final List inscriptions; - FeedResponse(this.inscriptions); + FeedResponse({required this.inscriptions}); factory FeedResponse.fromJson(Map json) { - final inscriptionsJson = json['_links']['inscriptions'] as List; - final inscriptions = inscriptionsJson - .map((inscriptionJson) => InscriptionLink.fromJson(inscriptionJson)) + final List inscriptionsJson = json['_links']['inscriptions'] as List; + final List inscriptions = inscriptionsJson + .map((json) => InscriptionLink.fromJson(json as Map)) .toList(); - return FeedResponse(inscriptions); + return FeedResponse(inscriptions: inscriptions); + } +} + +class InscriptionLink { + final String href; + final String title; + + InscriptionLink({required this.href, required this.title}); + + factory InscriptionLink.fromJson(Map json) { + return InscriptionLink( + href: json['href'] as String ?? '', + title: json['title'] as String ?? '', + ); } } diff --git a/lib/dto/ordinals/inscription_response.dart b/lib/dto/ordinals/inscription_response.dart index e06ddca6d..ab093b750 100644 --- a/lib/dto/ordinals/inscription_response.dart +++ b/lib/dto/ordinals/inscription_response.dart @@ -1,17 +1,17 @@ class InscriptionResponse { - final InscriptionLinks links; - final String address; - final int contentLength; - final String contentType; - final int genesisFee; - final int genesisHeight; - final String genesisTransaction; - final String location; - final int number; - final int offset; - final String output; - final dynamic sat; // Change to appropriate type if available - final String timestamp; + late final Links links; + late final String address; + late final int contentLength; + late final String contentType; + late final int genesisFee; + late final int genesisHeight; + late final String genesisTransaction; + late final String location; + late final int number; + late final int offset; + late final String output; + late final String? sat; // Make sure to update the type to allow null + late final String timestamp; InscriptionResponse({ required this.links, @@ -29,67 +29,62 @@ class InscriptionResponse { required this.timestamp, }); - factory InscriptionResponse.fromJson(Map json) { - return InscriptionResponse( - links: InscriptionLinks.fromJson(json['_links']), - address: json['address'], - contentLength: json['content_length'], - contentType: json['content_type'], - genesisFee: json['genesis_fee'], - genesisHeight: json['genesis_height'], - genesisTransaction: json['genesis_transaction'], - location: json['location'], - number: json['number'], - offset: json['offset'], - output: json['output'], - sat: json['sat'], - timestamp: json['timestamp'], - ); + InscriptionResponse.fromJson(Map json) { + links = Links.fromJson(json['_links'] as Map); + address = json['address'] as String; + contentLength = json['content_length'] as int; + contentType = json['content_type'] as String; + genesisFee = json['genesis_fee'] as int; + genesisHeight = json['genesis_height'] as int; + genesisTransaction = json['genesis_transaction'] as String; + location = json['location'] as String; + number = json['number'] as int; + offset = json['offset'] as int; + output = json['output'] as String; + sat = json['sat'] as String?; + timestamp = json['timestamp'] as String; } } -class InscriptionLinks { - final InscriptionLink? content; - final InscriptionLink? genesisTransaction; - final InscriptionLink? next; - final InscriptionLink? output; - final InscriptionLink? prev; - final InscriptionLink? preview; - final InscriptionLink? sat; - final InscriptionLink? self; +class Links { + late final Link content; + late final Link genesisTransaction; + late final Link next; + late final Link output; + late final Link prev; + late final Link preview; + late final Link? sat; // Make sure to update the type to allow null + late final Link self; - InscriptionLinks({ - this.content, - this.genesisTransaction, - this.next, - this.output, - this.prev, - this.preview, + Links({ + required this.content, + required this.genesisTransaction, + required this.next, + required this.output, + required this.prev, + required this.preview, this.sat, - this.self, + required this.self, }); - factory InscriptionLinks.fromJson(Map json) { - return InscriptionLinks( - content: InscriptionLink.fromJson(json['content']), - genesisTransaction: InscriptionLink.fromJson(json['genesis_transaction']), - next: InscriptionLink.fromJson(json['next']), - output: InscriptionLink.fromJson(json['output']), - prev: InscriptionLink.fromJson(json['prev']), - preview: InscriptionLink.fromJson(json['preview']), - sat: InscriptionLink.fromJson(json['sat']), - self: InscriptionLink.fromJson(json['self']), - ); + Links.fromJson(Map json) { + content = Link.fromJson(json['content'] as Map); + genesisTransaction = Link.fromJson(json['genesis_transaction'] as Map); + next = Link.fromJson(json['next'] as Map); + output = Link.fromJson(json['output'] as Map); + prev = Link.fromJson(json['prev'] as Map); + preview = Link.fromJson(json['preview'] as Map); + sat = json['sat'] != null ? Link.fromJson(json['sat'] as Map) : null; + self = Link.fromJson(json['self'] as Map); } } -class InscriptionLink { - final String href; - final String title; +class Link { + late final String href; - InscriptionLink({required this.href, required this.title}); + Link({required this.href}); - factory InscriptionLink.fromJson(Map json) { - return InscriptionLink(href: json['href'], title: json['title']); + Link.fromJson(Map json) { + href = json['href'] as String; } -} +} \ No newline at end of file diff --git a/lib/services/ordinals_api.dart b/lib/services/ordinals_api.dart index 97cc45834..a966988d7 100644 --- a/lib/services/ordinals_api.dart +++ b/lib/services/ordinals_api.dart @@ -1,9 +1,39 @@ -import 'package:stackwallet/models/ordinal.dart'; +import 'dart:convert'; +import 'package:http/http.dart' as http; + +import 'package:stackwallet/dto/ordinals/feed_response.dart'; +import 'package:stackwallet/dto/ordinals/inscription_response.dart'; class OrdinalsAPI { - // dummy class with sample functions to be changed / filled out + final String baseUrl; - static Future> fetch() async { - return []; + OrdinalsAPI({required this.baseUrl}); + + Future> _getResponse(String endpoint) async { + final response = await http.get(Uri.parse('$baseUrl$endpoint')); + if (response.statusCode == 200) { + return _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); } }