Skip to content

Get

Get allows you to retrieve Items by one of their full key paths. If no Item exists at that key path, nothing is returned. Like Put, Get supports batch requests of up to 50 key paths at a time, returning all the items that exist.

In order to Get an Item you must know one of its key paths, which were defined as part of the Item Type’s schema. Each SDK contains some helpers to build key paths.

Clients automatically parse the returned Items into typed objects, though depending on the language you may have different methods for checking which type an Item is.

For this example we’ll use the schema defined in Example: Movies Schema, which declares Movie as an Item Type with the key path /movies-:id.

15 collapsed lines
1
package main
2
3
import (
4
"context"
5
"fmt"
6
"os"
7
"slices"
8
"strconv"
9
"time"
10
11
"github.com/StatelyCloud/go-sdk/stately"
12
// This is the code you generated from schema
13
"github.com/StatelyCloud/stately/go-sdk-sample/schema"
14
)
15
16
func sampleGet(
17
ctx context.Context,
18
client stately.Client,
19
movieID []byte,
20
) (*schema.Movie, error) {
21
item, err := client.Get(
22
ctx,
23
// Construct the key path for the movie
24
"/movie-"+stately.ToKeyID(movieID),
25
)
26
if err != nil {
27
return nil, err
28
}
29
if item == nil { // Item not found
30
return nil, nil
31
}
32
if movie, ok := item.(*schema.Movie); ok {
33
return movie, nil
34
}
35
return nil, fmt.Errorf("item is not a Movie")
36
}
37
38
item, err := client.WithAllowStale(true).Get(
39
ctx,
40
"/movie-"+stately.ToKeyID(movieID),
41
)