Skip to content

Commit

Permalink
Working on examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Nov 4, 2024
1 parent d1f1811 commit f3b5b55
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions java-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ Below is a list of all Java code examples currently supported in this repo:

[Get collections by ID.](src/main/java/org/onflow/examples/java/getCollection/GetCollectionAccessAPIConnector.java)

- Get collection by id
- Get full collection by id (returns all transactions in collection response)

#### Get Execution Data

[Get execution data by block ID.](src/main/java/org/onflow/examples/java/getExecutionData/GetExecutionDataAccessAPIConnector.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.onflow.flow.sdk.FlowAccessApi.AccessApiCallResponse;
import org.onflow.flow.sdk.FlowCollection;
import org.onflow.flow.sdk.FlowId;
import org.onflow.flow.sdk.FlowTransaction;

import java.util.List;

public class GetCollectionAccessAPIConnector {
private final FlowAccessApi accessAPI;
Expand All @@ -21,4 +24,14 @@ public FlowCollection getCollectionById(FlowId collectionId) {
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
}
}

public List<FlowTransaction> getFullCollectionById(FlowId collectionId) {
AccessApiCallResponse<List<FlowTransaction>> response = accessAPI.getFullCollectionById(collectionId);
if (response instanceof AccessApiCallResponse.Success) {
return ((AccessApiCallResponse.Success<List<FlowTransaction>>) response).getData();
} else {
AccessApiCallResponse.Error errorResponse = (AccessApiCallResponse.Error) response;
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.onflow.flow.sdk.crypto.Crypto;
import org.onflow.flow.sdk.crypto.PublicKey;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
Expand Down Expand Up @@ -51,4 +53,16 @@ public void canFetchCollectionById() {
assertNotNull(collection, "Collection should not be null");
assertEquals(collectionId, collection.getId(), "Collection ID should match the fetched collection ID");
}

@Test
public void canFetchFullCollectionById() {
List<FlowTransaction> fullCollectionResponse = connector.getFullCollectionById(collectionId);

assertNotNull(fullCollectionResponse, "Collection transactions should not be null");
assertFalse(fullCollectionResponse.isEmpty(), "Collection transactions should not be empty");

FlowTransaction firstTransaction = fullCollectionResponse.get(0);
assertNotNull(firstTransaction.getId(), "Transaction ID should not be null");
assertNotNull(firstTransaction.getScript(), "Transaction script should not be null");
}
}
3 changes: 3 additions & 0 deletions kotlin-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ Below is a list of all Kotlin code examples currently supported in this repo:

[Get collections by ID.](src/main/kotlin/org/onflow/examples/kotlin/getCollection/GetCollectionAccessAPIConnector.kt)

- Get collection by id
- Get full collection by id (returns all transactions in collection response)

#### Get Execution Data

[Get execution data by block ID.](src/main/kotlin/org/onflow/examples/kotlin/getExecutionData/GetExecutionDataAccessAPIConnector.kt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ class GetCollectionAccessAPIConnector(
is AccessApiCallResponse.Success -> response.data
is AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
}

fun getFullCollectionById(collectionId: FlowId): List<FlowTransaction> =
when (val response = accessAPI.getFullCollectionById(collectionId)) {
is AccessApiCallResponse.Success -> response.data
is AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import org.onflow.flow.common.test.FlowEmulatorProjectTest
import org.onflow.flow.common.test.FlowServiceAccountCredentials
import org.onflow.flow.common.test.FlowTestClient
import org.onflow.flow.common.test.TestAccount
import org.onflow.flow.sdk.FlowAccessApi
import org.onflow.flow.sdk.FlowCollection
import org.onflow.flow.sdk.FlowId
import org.onflow.flow.sdk.SignatureAlgorithm
import org.onflow.flow.sdk.*
import org.onflow.flow.sdk.crypto.Crypto

@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
Expand Down Expand Up @@ -53,4 +50,16 @@ class GetCollectionAccessAPIConnectorTest {
assertNotNull(collection, "Collection should not be null")
assertEquals(collectionId, collection.id, "Collection ID should match the fetched collection ID")
}

@Test
fun `Can fetch full collection by ID`() {
val collectionTransactions: List<FlowTransaction> = connector.getFullCollectionById(collectionId)

assertNotNull(collectionTransactions, "Collection transactions should not be null")
assertTrue(collectionTransactions.isNotEmpty(), "Collection transactions should not be empty")

val firstTransaction = collectionTransactions.first()
assertNotNull(firstTransaction.id, "Transaction ID should not be null")
assertNotNull(firstTransaction.script, "Transaction script should not be null")
}
}

0 comments on commit f3b5b55

Please sign in to comment.