|
| 1 | +package dev.arcade.example; |
| 2 | + |
| 3 | +import dev.arcade.client.ArcadeClient; |
| 4 | +import dev.arcade.models.tools.ExecuteToolRequest; |
| 5 | +import dev.arcade.models.tools.ExecuteToolResponse; |
| 6 | +import dev.arcade.models.tools.ToolExecuteParams; |
| 7 | +import org.springframework.boot.ApplicationRunner; |
| 8 | +import org.springframework.boot.SpringApplication; |
| 9 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | + |
| 12 | +/** |
| 13 | + * Example of calling a tool using the Arcade Java SDK. |
| 14 | + */ |
| 15 | +@SpringBootApplication |
| 16 | +public class SpringBootExample { |
| 17 | + |
| 18 | + /** |
| 19 | + * Starts the Spring Boot application. |
| 20 | + * @param args All args are passed into the SpringApplication |
| 21 | + */ |
| 22 | + public static void main(String[] args) { |
| 23 | + SpringApplication.run(SpringBootExample.class, args); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Injects an <code>ArcadeClient</code>, and returns an ApplicationRunner that makes a tool call. |
| 28 | + * @param client Arcade Client is autoinjected if ARCADE_API_KEY, or equivalent application.properties var is set. |
| 29 | + * @return Runs code on application start. |
| 30 | + */ |
| 31 | + @Bean |
| 32 | + ApplicationRunner appRunner(ArcadeClient client) { |
| 33 | + return args -> { |
| 34 | + String userId = System.getenv("ARCADE_USER_ID"); // the Spotify tool require a userId |
| 35 | + if (userId == null) { |
| 36 | + throw new IllegalArgumentException("Missing ARCADE_USER_ID environment variable"); |
| 37 | + } |
| 38 | + |
| 39 | + ToolExecuteParams params = ToolExecuteParams.builder() |
| 40 | + .executeToolRequest(ExecuteToolRequest.builder() |
| 41 | + .toolName("Spotify.ResumePlayback@1.0.2") |
| 42 | + .userId(userId) |
| 43 | + .build()) |
| 44 | + .build(); |
| 45 | + ExecuteToolResponse executeToolResponse = client.tools().execute(params); |
| 46 | + executeToolResponse |
| 47 | + .output() |
| 48 | + .ifPresentOrElse( |
| 49 | + output -> System.out.println("Tool output: " + output._value()), |
| 50 | + () -> System.out.println("No output for this tool")); |
| 51 | + }; |
| 52 | + } |
| 53 | +} |
0 commit comments