Skip to content

singnet/snet-sdk-java

Repository files navigation

SingularityNet Java SDK

CircleCI codecov Release Javadoc

Implementing SingularityNet service client in Java

  1. Create new maven project, see Maven getting started. Add Jitpack maven repo into the project to be able using SingularityNet Java SDK artifacts:
<project>

  <repositories>
    <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
    </repository>
  </repositories>

</project>
  1. Use snet-sdk-maven-plugin to get service protobuf API within your project:
<project>
  <properties>
    <snet.sdk.java.version>master-SNAPSHOT</snet.sdk.java.version>
  </properties>

  <build>
    <plugins>

      <plugin>
        <groupId>com.github.singnet.snet-sdk-java</groupId>
        <artifactId>snet-sdk-maven-plugin</artifactId>
        <version>${snet.sdk.java.version}</version>

        <executions>
          <execution>

            <configuration>
              <!-- service organization id -->
              <orgId>snet</orgId>
              <!-- service id -->
              <serviceId>cntk-image-recon</serviceId>
              <!-- API output dir -->
              <outputDir>${project.build.directory}/proto</outputDir>
              <!-- desired java package for classes generated -->
              <javaPackage>io.singularitynet.client.cntk</javaPackage>
              <!-- Ethereum RPC endpoint to use -->
              <ethereumJsonRpcEndpoint>https://mainnet.infura.io/v3/e7732e1f679e461b9bb4da5653ac3fc2</ethereumJsonRpcEndpoint>
            </configuration>

            <goals>
              <goal>get</goal>
            </goals>

          </execution>
        </executions>

      </plugin>

    </plugins>
  </build>
</project>
  1. Add SingularityNet Java SDK as dependency:
	<dependency>
	    <groupId>com.github.singnet.snet-sdk-java</groupId>
	    <artifactId>snet-sdk-java</artifactId>
	    <version>${snet.sdk.java.version}</version>
	</dependency>
  1. Use protobuf-maven-plugin to generate Java stubs of service API:

  2. Write Java client app using SDK API, see CntkImageRecognition.java as example:

    // Create SDK configuration
    Configuration config = Configuration.newBuilder()
        .setEthereumJsonRpcEndpoint("https://mainnet.infura.io/v3/" /* + your infura project id */)
        .setIdentityType(Configuration.IdentityType.PRIVATE_KEY)
        .setIdentityPrivateKey(new byte[] { /* identity private key */ });

    // Create SDK instance
    Sdk sdk = new Sdk(config);
    try {

        // Choose payment strategy
        PaymentStrategy paymentStrategy = new OnDemandPaymentChannelPaymentStrategy();

        // Create new service client instance
        ServiceClient serviceClient = sdk.newServiceClient("snet",
                "cntk-image-recon", "default_group", paymentStrategy); 
        try {

            // Get gRPC API stub
            RecognizerBlockingStub stub = serviceClient.getGrpcStub(RecognizerGrpc::newBlockingStub);
            
            // Call method
            Input input = Input.newBuilder()
                .setModel("ResNet152")
                .setImgPath("https://d2z4fd79oscvvx.cloudfront.net/0027071_1_single_rose_385.jpeg")
                .build();
            Output output = stub.flowers(input);
            System.out.println("Response received: " + output);

        } finally {
            // Shutdown service client
            serviceClient.close();
        }

    } finally {
        // Shutdown SDK
        sdk.close();
    }

How to build

Integration testing is disabled by default. To run full build including integration tests use:

mvn install -DskipITs=false -P run-integration-environment

The command about automatically starts integration environment docker before running tests and stops after it. To start integration environment manually execute:

docker run -d \
    --name java-sdk-integration-environment \
    -p 5002:5002 -p 8545:8545 -p 7000:7000 \
    singularitynet/java-sdk-integration-test-env:5.0.1

Then you can run build with integration testing using:

mvn install -DskipITs=false

Running integration tests is a time consuming process so to make fast build running unit tests only use:

mvn install

Class diagram

Class diagram Source code