@@ -59,18 +59,22 @@ public List<Nft> queryNftsByAccount(@NonNull final AccountId accountId) throws H
5959 public List <Nft > queryNftsByAccountAndTokenId (@ NonNull final AccountId accountId , @ NonNull final TokenId tokenId ) throws HederaException {
6060 Objects .requireNonNull (accountId , "accountId must not be null" );
6161 Objects .requireNonNull (tokenId , "tokenId must not be null" );
62+ final String host = mirrorNodeEndpoint .substring (8 ).split ("\\ :" )[0 ];
63+ final String port = mirrorNodeEndpoint .substring (8 ).split ("\\ :" )[1 ];
6264 final String body = restClient .get ()
63- .uri (uriBuilder -> uriBuilder .path (mirrorNodeEndpoint + "/api/v1/tokens/" + tokenId + "/nfts" )
65+ .uri (uriBuilder -> uriBuilder
66+ .scheme ("https" )
67+ .host (host )
68+ .port (port )
69+ .path ("/api/v1/tokens/" + tokenId + "/nfts" )
6470 .queryParam ("account.id" , accountId )
6571 .build ())
6672 .header ("accept" , "application/json" )
6773 .retrieve ()
6874 .onStatus (HttpStatusCode ::is4xxClientError , (request , response ) -> {
6975 throw new RuntimeException ("Client error: " + response .getStatusText ());
7076 }).body (String .class );
71- System .out .println (body );
72- //TODO: JSON PARSING
73- return List .of ();
77+ return fromJson (body );
7478 }
7579
7680 @ Override
@@ -83,26 +87,27 @@ public List<Nft> queryNftsByTokenId(@NonNull TokenId tokenId) throws HederaExcep
8387 .onStatus (HttpStatusCode ::is4xxClientError , (request , response ) -> {
8488 throw new RuntimeException ("Client error: " + response .getStatusText ());
8589 }).body (String .class );
90+ return fromJson (body );
91+ }
92+
93+ private List <Nft > fromJson (final String json ) {
8694 try {
87- final JsonNode rootNode = objectMapper .readTree (body );
95+ final JsonNode rootNode = objectMapper .readTree (json );
8896 return StreamSupport .stream (
8997 Spliterators .spliteratorUnknownSize (rootNode .get ("nfts" ).iterator (), Spliterator .ORDERED ),
9098 false ).map (nftNode -> {
91- try {
92- final TokenId parsedTokenId = TokenId .fromString (nftNode .get ("token_id" ).asText ());
93- if (!tokenId .equals (parsedTokenId )) {
94- throw new RuntimeException ("Token ID mismatch: " + tokenId + " != " + parsedTokenId );
95- }
96- final AccountId account = AccountId .fromString (nftNode .get ("account_id" ).asText ());
97- final long serial = nftNode .get ("serial_number" ).asLong ();
98- final byte [] metadata = nftNode .get ("metadata" ).binaryValue ();
99- return new Nft (tokenId , serial , account , metadata );
100- } catch (final Exception e ) {
101- throw new RuntimeException ("Error parsing NFT" , e );
102- }
99+ try {
100+ final TokenId parsedTokenId = TokenId .fromString (nftNode .get ("token_id" ).asText ());
101+ final AccountId account = AccountId .fromString (nftNode .get ("account_id" ).asText ());
102+ final long serial = nftNode .get ("serial_number" ).asLong ();
103+ final byte [] metadata = nftNode .get ("metadata" ).binaryValue ();
104+ return new Nft (parsedTokenId , serial , account , metadata );
105+ } catch (final Exception e ) {
106+ throw new RuntimeException ("Error parsing NFT" , e );
107+ }
103108 }).toList ();
104109 } catch (final Exception e ) {
105- throw new HederaException ("Error parsing body as JSON: " + body , e );
110+ throw new IllegalArgumentException ("Error parsing body as JSON: " + json , e );
106111 }
107112 }
108113
0 commit comments