Dynamic Client#

The dynamic client is used to interact with services without a code-generated client. The dynamic client loads Smithy models at runtime, converting them to a schema-based client. Users can call a modeled service using document types as input and output.

Warning

The dynamic client does not currently support streaming or event streaming.

Usage#

Add the dynamic-client module as a dependency of your project:

build.gradle.kts#
 dependencies {
     implementation("software.amazon.smithy.java:dynamicclient:0.0.1")
 }

Then, load a Smithy model:

import software.amazon.smithy.java.dynamicclient
import software.amazon.smithy.model.Model;
...

var model = Model.assembler()
    .addImport("/path/to/model.json")
    .assemble()
    .unwrap();

Then, select a service shape in the loaded model to call. In this case, the CoffeeShop service:

var shapeId = ShapeId.from("com.example#CoffeeShop");

Now, create the DynamicClient instance for this model and service:

var client = DynamicClient.builder()
    .service(shapeId)
    .model(model)
    .protocol(new RestJsonClientProtocol(shapeId))
    .transport(new JavaHttpClientTransport())
    .endpointResolver(EndpointResolver.staticEndpoint("https://api.cafe.example.com"))
    .build();

Important

If an explicit protocol and transport are not provided to the builder, the builder will attempt to find protocol and transport implementations on the classpath that match the protocol traits attached to the service.

To call the service, create an input using a Document that mirrors what's defined in the Smithy model. The Document.createFromObject method can create a Document from a map:

var input = Document.createFromObject(Map.of("coffeeType", "COLD_BREW"));
var result = client.call("CreateOrder", input).get();
System.out.println(result);