-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.go
182 lines (172 loc) · 5.09 KB
/
fetch.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package gcloudcx
import (
"context"
"encoding/json"
"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
"github.com/google/uuid"
)
// Fetch fetches a resource from the Genesys Cloud API
//
// # The object must implement the Fetchable interface
//
// Resources can be fetched by their ID:
//
// user, err := Fetch[gcloudcx.User](context, client, uuid.UUID)
//
// user, err := Fetch[gcloudcx.User](context, client, gcloudcx.User{ID: uuid.UUID})
//
// or by their URI:
//
// user, err := Fetch[gcloudcx.User](context, client, gcloudcx.User{}.GetURI(uuid.UUID))
func Fetch[T Fetchable, PT interface {
Initializable
*T
}](context context.Context, client *Client, parameters ...any) (*T, error) {
id, query, selfURI, log := parseFetchParameters(context, client, parameters...)
if len(selfURI) > 0 {
var object T
if err := client.Get(context, selfURI.WithQuery(query), &object); err != nil {
return nil, err
}
PT(&object).Initialize(client, log)
return &object, nil
}
if id != uuid.Nil {
var object T
if err := client.Get(context, object.GetURI(id).WithQuery(query), &object); err != nil {
return nil, err
}
PT(&object).Initialize(client, log)
return &object, nil
}
return nil, errors.NotFound.WithStack()
}
// FetchBy fetches a resource from the Genesys Cloud API by a match function
//
// The resource must implement the Fetchable interface
//
// match := func(user gcloudcx.User) bool {
// return user.Name == "John Doe"
// }
// user, err := FetchBy(context, client, match)
//
// A gcloudcx.Query can be added to narrow the request:
//
// user, err := FetchBy(context, client, match, gcloudcx.Query{Language: "en-US"})
func FetchBy[T Fetchable, PT interface {
Initializable
*T
}](context context.Context, client *Client, match func(T) bool, parameters ...interface{}) (*T, error) {
if match == nil {
return nil, errors.ArgumentMissing.With("match function")
}
_, query, _, log := parseFetchParameters(context, client, parameters...)
entities := Entities{}
page := uint64(1)
var addressable T
for {
uri := addressable.GetURI().WithQuery(query).WithQuery(Query{"pageNumber": page})
if err := client.Get(context, uri, &entities); err != nil {
return nil, err
}
for _, entity := range entities.Entities {
var object T
if err := json.Unmarshal(entity, &object); err == nil && match(object) {
PT(&object).Initialize(client, log)
return &object, nil
}
}
if page++; page > entities.PageCount {
break
}
}
return nil, errors.NotFound.WithStack()
}
// FetchAll fetches all objects from the Genesys Cloud API
//
// The objects must implement the Fetchable interface
//
// users, err := FetchAll[gcloudcx.User](context, client)
//
// A gcloudcx.Query can be added to narrow the request:
//
// users, err := FetchAll[gcloudcx.User](context, client, gcloudcx.Query{Language: "en-US"})
func FetchAll[T Fetchable, PT interface {
Initializable
*T
}](context context.Context, client *Client, parameters ...interface{}) ([]*T, error) {
_, query, _, log := parseFetchParameters(context, client, parameters...)
entities := Entities{}
objects := []*T{}
page := uint64(1)
var addressable T
for {
uri := addressable.GetURI().WithQuery(query).WithQuery(Query{"pageNumber": page})
if err := client.Get(context, uri, &entities); err != nil {
return nil, err
}
for _, entity := range entities.Entities {
var object T
if err := json.Unmarshal(entity, &object); err == nil {
PT(&object).Initialize(client, log)
objects = append(objects, &object)
}
}
if page++; page > entities.PageCount {
break
}
}
return objects, nil
}
/*
func (client *Client) FetchAll(context context.Context, object Addressable) ([]interface{}, error) {
entities := struct {
Entities []json.RawMessage `json:"entities"`
PageSize int `json:"pageSize"`
PageNumber int `json:"pageNumber"`
PageCount int `json:"pageCount"`
PageTotal int `json:"total"`
FirstURI string `json:"firstUri"`
SelfURI string `json:"selfUri"`
LastURI string `json:"lastUri"`
}{}
page := 1
for {
uri := URI(addressable.GetURI().Base().String() + "?messengerType=" + messengerType + "&pageNumber=" + strconv.FormatUint(page, 10))
if err := client.Get(context, uri, &entities); err != nil {
return nil, err
}
log.Record("response", entities).Infof("Got a response")
}
return []interface{}{}, nil
}
*/
func parseFetchParameters(context context.Context, client *Client, parameters ...any) (uuid.UUID, Query, URI, *logger.Logger) {
var id uuid.UUID
var query Query
var uri URI
log, _ := logger.FromContext(context)
if log == nil {
log = client.Logger
}
for _, parameter := range parameters {
switch parameter := parameter.(type) {
case uuid.UUID:
id = parameter
case Query:
query = parameter
case URI:
uri = parameter
case *logger.Logger:
log = parameter
default:
if identifiable, ok := parameter.(Identifiable); ok {
id = identifiable.GetID()
} else if addressable, ok := parameter.(Addressable); ok {
uri = addressable.GetURI()
}
}
}
return id, query, uri, log
}