-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcollection.go
50 lines (42 loc) · 1.29 KB
/
collection.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
package replicate
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
type Collection struct {
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
Models *[]Model `json:"models,omitempty"`
rawJSON json.RawMessage `json:"-"`
}
func (c *Collection) RawJSON() json.RawMessage {
return c.rawJSON
}
var _ json.Unmarshaler = (*Collection)(nil)
func (c *Collection) UnmarshalJSON(data []byte) error {
c.rawJSON = data
type Alias Collection
alias := &struct{ *Alias }{Alias: (*Alias)(c)}
return json.Unmarshal(data, alias)
}
// ListCollections returns a list of all collections.
func (r *Client) ListCollections(ctx context.Context) (*Page[Collection], error) {
response := &Page[Collection]{}
err := r.fetch(ctx, http.MethodGet, "/collections", nil, response)
if err != nil {
return nil, fmt.Errorf("failed to list collections: %w", err)
}
return response, nil
}
// GetCollection returns a collection by slug.
func (r *Client) GetCollection(ctx context.Context, slug string) (*Collection, error) {
collection := &Collection{}
err := r.fetch(ctx, http.MethodGet, fmt.Sprintf("/collections/%s", slug), nil, collection)
if err != nil {
return nil, fmt.Errorf("failed to get collection: %w", err)
}
return collection, nil
}