|
| 1 | +package dev.arcade.example; |
| 2 | + |
| 3 | +import dev.arcade.client.ArcadeClient; |
| 4 | +import dev.arcade.client.okhttp.ArcadeOkHttpClient; |
| 5 | +import dev.arcade.models.tools.ExecuteToolRequest; |
| 6 | +import dev.arcade.models.tools.ExecuteToolResponse; |
| 7 | +import dev.arcade.models.tools.ToolExecuteParams; |
| 8 | + |
| 9 | +/** |
| 10 | + * Example of calling a tool using the Arcade Java SDK. |
| 11 | + */ |
| 12 | +public class PlaySpotifyExample { |
| 13 | + |
| 14 | + /** |
| 15 | + * Executes the Spotify.ResumePlayback, both the <code>ARCADE_USER_ID</code> and <code>ARCADE_API_KEY</code> environment variables must be set. |
| 16 | + * See the <a href="https://docs.arcade.dev/en/get-started/setup/api-keys">Getting Your API Key</a> guide to create an API Key. |
| 17 | + * Your username can be found in the lower left corner of your <a href="https://app.arcade.dev/home">Arcade console</a>. |
| 18 | + * @param args Not used. |
| 19 | + */ |
| 20 | + public static void main(String[] args) { |
| 21 | + |
| 22 | + String userId = System.getenv("ARCADE_USER_ID"); // the Spotify tool requires a userId |
| 23 | + if (userId == null) { |
| 24 | + throw new IllegalArgumentException("ARCADE_USER_ID and ARCADE_API_KEY environment variables must be set"); |
| 25 | + } |
| 26 | + |
| 27 | + // Configures using the `ARCADE_API_KEY` environment variable |
| 28 | + ArcadeClient client = ArcadeOkHttpClient.fromEnv(); |
| 29 | + |
| 30 | + ToolExecuteParams params = ToolExecuteParams.builder() |
| 31 | + .executeToolRequest(ExecuteToolRequest.builder() |
| 32 | + .toolName("Spotify.ResumePlayback@1.0.2") |
| 33 | + .userId(userId) |
| 34 | + .build()) |
| 35 | + .build(); |
| 36 | + ExecuteToolResponse executeToolResponse = client.tools().execute(params); |
| 37 | + executeToolResponse |
| 38 | + .output() |
| 39 | + .ifPresentOrElse( |
| 40 | + output -> System.out.println("Tool output: " + output._value()), |
| 41 | + () -> System.out.println("No output for this tool")); |
| 42 | + } |
| 43 | +} |
0 commit comments