Code Monkey home page Code Monkey logo

snet-sdk-java's Introduction

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(sdk);

        // 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:3.0.0

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

snet-sdk-java's People

Contributors

mvpeterson avatar raamb avatar vsbogd avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.