forked from sanity-io/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_documents.go
44 lines (35 loc) · 1012 Bytes
/
get_documents.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
package sanity
import (
"context"
"strings"
"github.com/sanity-io/client-go/api"
)
// GetDocuments returns a new GetDocuments builder.
func (c *Client) GetDocuments(docIDs ...string) *GetDocumentsBuilder {
return &GetDocumentsBuilder{c: c, docIDs: docIDs}
}
// QueryBuilder is a builder for GET documents API.
type GetDocumentsBuilder struct {
c *Client
docIDs []string
tag string
}
func (b *GetDocumentsBuilder) Tag(tag string) *GetDocumentsBuilder {
b.tag = tag
return b
}
// Do performs the query.
// On API request failure, this will return an error of type *RequestError.
func (b *GetDocumentsBuilder) Do(ctx context.Context) (*api.GetDocumentsResponse, error) {
if len(b.docIDs) == 0 {
return &api.GetDocumentsResponse{}, nil
}
req := b.c.newAPIRequest().
AppendPath("data/doc", b.c.dataset, strings.Join(b.docIDs, ",")).
Tag(b.tag, b.c.tag)
var resp api.GetDocumentsResponse
if _, err := b.c.do(ctx, req, &resp); err != nil {
return nil, err
}
return &resp, nil
}