-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespaces.go
72 lines (61 loc) · 2.11 KB
/
namespaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tpuf
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
)
type NamespaceCursor string
type NamespacesRequest struct {
// Prefix is an optional prefix by which to filter namespaces.
Prefix string `json:"prefix,omitempty"`
// PageSize is the maximum number of namespaces to return. Default is 1000.
PageSize int `json:"page_size,omitempty"`
// Cursor the cursor to use for pagination. Omit to get the first page.
Cursor NamespaceCursor `json:"cursor,omitempty"`
}
type Namespace struct {
ID string `json:"id"`
}
type NamespacesResponse struct {
// Namespaces is the list of namespaces.
Namespaces []*Namespace `json:"namespaces"`
// NextCursor is the cursor which can be used to fetch the next page.
NextCursor NamespaceCursor `json:"next_cursor,omitempty"`
}
// Namespaces lists all namespaces, optionally filtered by prefix.
// This query is paginated according to the input page size. The returned NextCursor may be used to fetch the next page.
// See https://turbopuffer.com/docs/namespaces for more details.
func (c *Client) Namespaces(ctx context.Context, request *NamespacesRequest) (*NamespacesResponse, error) {
path := "/v1/namespaces"
params := url.Values{}
if request.PageSize > 0 {
params.Set("page_size", strconv.Itoa(request.PageSize))
}
if request.Prefix != "" {
params.Set("prefix", request.Prefix)
}
if request.Cursor != "" {
params.Set("cursor", string(request.Cursor))
}
respData, err := c.get(ctx, path, params)
if err != nil {
return nil, fmt.Errorf("failed to list namespaces: %w", err)
}
var response NamespacesResponse
if err := json.Unmarshal(respData, &response); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &response, nil
}
// DeleteNamespace deletes a namespace entirely, including all documents.
// See https://turbopuffer.com/docs/delete-namespace for more details.
func (c *Client) DeleteNamespace(ctx context.Context, namespace string) error {
path := fmt.Sprintf("/v1/namespaces/%s", namespace)
_, err := c.delete(ctx, path)
if err != nil {
return fmt.Errorf("failed to delete namespace: %w", err)
}
return nil
}