Skip to content

Commit cfa7d4e

Browse files
authored
Add a simple tool call example (#5)
This example uses the Spotify.ResumePlayback tool
1 parent 28845bd commit cfa7d4e

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

CONTRIBUTING.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@ modify the contents of the `arcade-java/lib/` and `arcade-java-examples/` direct
66

77
## Adding and running examples
88

9-
TODO
9+
Set the following environment variables:
10+
11+
| ENV Var | Description |
12+
|-------------------|-------------------------------------------------------------------------|
13+
| `ARCADE_USER_ID` | Arcade user Id or email address |
14+
| `ARCADE_API_KEY` | [Arcade API key](https://docs.arcade.dev/en/get-started/setup/api-keys) |
15+
| `ARCADE_BASE_URL` | Arcade URL, defaults to: `https://api.arcade.dev` |
16+
17+
Use `./gradlew :arcade-java-example:run -Pexample=<Name>` to run `<Name>Example`
18+
19+
For example, if you have the `potify.ResumePlayback` tool configured, you can run:
20+
```shell
21+
export ARCADE_API_KEY='your-api-key'
22+
export ARCADE_USER_ID='your-arcade-user'
23+
./gradlew :arcade-java-example:run -Pexample=PlaySpotify
24+
```
1025

1126
## Building the repository from source
1227

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
 (0)