Skip to content

Commit 43a4e20

Browse files
committed
Adding pagination support for NftRepository
1 parent 986ad26 commit 43a4e20

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

hedera-base/src/main/java/com/openelements/hedera/base/NftRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface NftRepository {
2020
* @throws HederaException if the search fails
2121
*/
2222
@NonNull
23-
List<Nft> findByOwner(@NonNull AccountId ownerId) throws HederaException;
23+
Page<Nft> findByOwner(@NonNull AccountId ownerId) throws HederaException;
2424

2525
/**
2626
* Return all NFTs of a given type.
@@ -52,7 +52,7 @@ public interface NftRepository {
5252
* @throws HederaException if the search fails
5353
*/
5454
@NonNull
55-
List<Nft> findByOwnerAndType(@NonNull AccountId ownerId, @NonNull TokenId tokenId) throws HederaException;
55+
Page<Nft> findByOwnerAndType(@NonNull AccountId ownerId, @NonNull TokenId tokenId) throws HederaException;
5656

5757
/**
5858
* Return the NFT of a given type and serial owned by a specific account.

hedera-base/src/main/java/com/openelements/hedera/base/implementation/NftRepositoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public NftRepositoryImpl(@NonNull final MirrorNodeClient mirrorNodeClient) {
2222

2323
@NonNull
2424
@Override
25-
public List<Nft> findByOwner(@NonNull final AccountId owner) throws HederaException {
25+
public Page<Nft> findByOwner(@NonNull final AccountId owner) throws HederaException {
2626
return mirrorNodeClient.queryNftsByAccount(owner);
2727
}
2828

@@ -41,7 +41,7 @@ public Optional<Nft> findByTypeAndSerial(@NonNull final TokenId tokenId, final l
4141

4242
@NonNull
4343
@Override
44-
public List<Nft> findByOwnerAndType(@NonNull final AccountId owner, @NonNull final TokenId tokenId)
44+
public Page<Nft> findByOwnerAndType(@NonNull final AccountId owner, @NonNull final TokenId tokenId)
4545
throws HederaException {
4646
return mirrorNodeClient.queryNftsByAccountAndTokenId(owner, tokenId);
4747
}

hedera-base/src/main/java/com/openelements/hedera/base/mirrornode/MirrorNodeClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
public interface MirrorNodeClient {
1515

1616
@NonNull
17-
List<Nft> queryNftsByAccount(@NonNull AccountId accountId) throws HederaException;
17+
Page<Nft> queryNftsByAccount(@NonNull AccountId accountId) throws HederaException;
1818

1919
@NonNull
20-
List<Nft> queryNftsByAccountAndTokenId(@NonNull AccountId accountId, @NonNull TokenId tokenId)
20+
Page<Nft> queryNftsByAccountAndTokenId(@NonNull AccountId accountId, @NonNull TokenId tokenId)
2121
throws HederaException;
2222

2323
@NonNull

hedera-spring/src/main/java/com/openelements/hedera/spring/implementation/MirrorNodeClientImpl.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,38 @@ public MirrorNodeClientImpl(@NonNull final String mirrorNodeEndpoint) {
6565
}
6666

6767
@Override
68-
public List<Nft> queryNftsByAccount(@NonNull final AccountId accountId) throws HederaException {
68+
public Page<Nft> queryNftsByAccount(@NonNull final AccountId accountId) throws HederaException {
6969
Objects.requireNonNull(accountId, "newAccountId must not be null");
70-
final JsonNode jsonNode = doGetCall("/api/v1/accounts/" + accountId + "/nfts");
71-
return jsonNodeToNftList(jsonNode);
70+
final URI uri = URI.create(
71+
getUriPrefix()
72+
+ "/api/v1/accounts/" + accountId + "/nfts");
73+
74+
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
75+
final Function<JsonNode, URI> nextUriExtractionFunction = node -> getNextUri(node);
76+
77+
return new RestBasedPage<>(objectMapper, restClient,
78+
uri,
79+
dataExtractionFunction, nextUriExtractionFunction);
7280
}
7381

7482
@Override
75-
public List<Nft> queryNftsByAccountAndTokenId(@NonNull final AccountId accountId, @NonNull final TokenId tokenId)
83+
public Page<Nft> queryNftsByAccountAndTokenId(@NonNull final AccountId accountId, @NonNull final TokenId tokenId)
7684
throws HederaException {
7785
Objects.requireNonNull(accountId, "newAccountId must not be null");
7886
Objects.requireNonNull(tokenId, "tokenId must not be null");
79-
final JsonNode jsonNode = doGetCall("/api/v1/tokens/" + tokenId + "/nfts", Map.of("account.id", accountId));
80-
return jsonNodeToNftList(jsonNode);
87+
88+
final URI uri = URI.create(
89+
getUriPrefix()
90+
+ doGetCall ("/api/v1/tokens/" + tokenId + "/nfts", Map.of("account.id", accountId)));
91+
92+
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
93+
final Function<JsonNode, URI> nextUriExtractionFunction = node -> getNextUri(node);
94+
95+
return new RestBasedPage<>(objectMapper, restClient,
96+
uri,
97+
dataExtractionFunction, nextUriExtractionFunction);
98+
99+
81100
}
82101

83102
@Override

hedera-spring/src/test/java/com/openelements/hedera/spring/test/NftRepositoryTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ void findByAccountId() throws Exception {
146146
hederaTestUtils.waitForMirrorNodeRecords();
147147

148148
//when
149-
final List<Nft> result = nftRepository.findByOwner(newOwner);
149+
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
150+
final List<Nft> result = getAll(slice);
150151

151152
//then
152153
Assertions.assertNotNull(result);
@@ -168,7 +169,8 @@ void findByAccountIdWIthZeroResult() throws Exception {
168169
hederaTestUtils.waitForMirrorNodeRecords();
169170

170171
//when
171-
final List<Nft> result = nftRepository.findByOwner(newOwner);
172+
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
173+
final List<Nft> result = getAll(slice);
172174

173175
//then
174176
Assertions.assertNotNull(result);
@@ -196,7 +198,8 @@ void findByTokenIdAndAccountId() throws Exception {
196198
hederaTestUtils.waitForMirrorNodeRecords();
197199

198200
//when
199-
final List<Nft> result = nftRepository.findByOwnerAndType(newOwner, tokenId);
201+
final Page<Nft> slice = nftRepository.findByOwnerAndType(newOwner, tokenId);
202+
final List<Nft> result = getAll(slice);
200203

201204
//then
202205
Assertions.assertNotNull(result);
@@ -218,7 +221,8 @@ void findByTokenIdAndAccountIdWithZeroResult() throws Exception {
218221
hederaTestUtils.waitForMirrorNodeRecords();
219222

220223
//when
221-
final List<Nft> result = nftRepository.findByOwnerAndType(newOwner, tokenId);
224+
final Page<Nft> slice = nftRepository.findByOwnerAndType(newOwner, tokenId);
225+
final List<Nft> result = getAll(slice);
222226

223227
//then
224228
Assertions.assertNotNull(result);

0 commit comments

Comments
 (0)