-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuforia_client.go
416 lines (352 loc) · 12.5 KB
/
vuforia_client.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package vuforia
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// vuforiaUrl is the endpoint for the Vuforia Web Services API
const vuforiaUrl = "vws.vuforia.com"
type Client interface {
// PostTarget adds a new target
PostTarget(context.Context, *PostTargetRequest) (*PostTargetResponse, error)
// GetTarget retrieves the target
GetTarget(context.Context, *GetTargetRequest) (*GetTargetResponse, error)
// UpdateTarget updates the target
UpdateTarget(context.Context, *UpdateTargetRequest) (*UpdateTargetResponse, error)
// DeleteTarget deletes the target
DeleteTarget(context.Context, *DeleteTargetRequest) (*DeleteTargetResponse, error)
// TargetSummary retrieves summary of the target
TargetSummary(context.Context, *TargetSummaryRequest) (*TargetSummaryResponse, error)
// DatabaseSummary retrieves the summary of the database
DatabaseSummary(context.Context) (*DatabaseSummaryResponse, error)
}
type ClientConfig struct {
SecretKey, AccessKey string
Client *http.Client
}
type client struct {
cfg ClientConfig
}
func NewClient(cfg ClientConfig) (Client, error) {
if cfg.SecretKey == "" {
return nil, fmt.Errorf("vuforia SecretKey must be set")
}
if cfg.AccessKey == "" {
return nil, fmt.Errorf("vuforia AccessKey must be set")
}
if cfg.Client == nil {
cfg.Client = http.DefaultClient
}
return &client{cfg: cfg}, nil
}
type PostTargetRequest struct {
// Name of the target, unique within a database
Name string `json:"name"`
// Width of the target in scene unit
Width float64 `json:"width"`
// Image is the base64 encoded binary recognition image data
Image string `json:"image"`
// Active indicates whether or not the target is active for query (Optional)
Active *bool `json:"active_flag,omitempty"`
// Metadata is the base64 encoded application metadata associated with the target (Optional)
Metadata *string `json:"application_metadata,omitempty"`
}
type PostTargetResponse struct {
// TargetId is the ID of the target
TargetId string `json:"target_id"`
// TransactionId is the ID of the transaction
TransactionId string `json:"transaction_id"`
// ResultCode is one of the VWS API Result Code.
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
ResultCode string `json:"result_code"`
}
func (c *client) PostTarget(ctx context.Context, input *PostTargetRequest) (*PostTargetResponse, error) {
if input == nil {
panic("input is <nil>")
}
body, err := json.Marshal(input)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("https://%s/targets", vuforiaUrl), bytes.NewReader(body))
if err != nil {
return nil, err
}
if err = prepare(c.cfg.SecretKey, c.cfg.AccessKey, req, body); err != nil {
return nil, err
}
resp, err := c.cfg.Client.Do(req)
if err != nil {
return nil, err
}
defer safeClose(resp)
if err := checkError(resp); err != nil {
return nil, err
}
var v PostTargetResponse
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, err
}
return &v, nil
}
type GetTargetRequest struct {
// TargetId is the ID of the target to retrieve
TargetId string
}
type GetTargetResponse struct {
// TransactionId is the ID of the transaction
TransactionId string `json:"transaction_id"`
// ResultCode is one of the VWS API Result Code.
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
ResultCode string `json:"result_code"`
// Status of the target (Processing, Success, Failure)
Status string `json:"status"`
// TargetRecord is the target information
TargetRecord struct {
// TargetId is the ID of the target
TargetId string `json:"target_id"`
// Active indicates whether or not the target is active for query; default is true
Active bool `json:"active_flag"`
// Name of the target, unique within a database
Name string `json:"name"`
// Width of the target in scene unit
Width float64 `json:"width"`
// TrackingRating is the rating of the target recognition image for tracking purposes
TrackingRating int `json:"tracking_rating"`
} `json:"target_record"`
}
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Retrieve-a-Target-Record
func (c *client) GetTarget(ctx context.Context, input *GetTargetRequest) (*GetTargetResponse, error) {
if input == nil {
panic("input is <nil>")
}
if input.TargetId == "" {
return nil, errors.New("TargetId must be provided")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://%s/targets/%s", vuforiaUrl, input.TargetId), nil)
if err != nil {
return nil, err
}
if err = prepare(c.cfg.SecretKey, c.cfg.AccessKey, req, nil); err != nil {
return nil, err
}
resp, err := c.cfg.Client.Do(req)
if err != nil {
return nil, err
}
defer safeClose(resp)
if err := checkError(resp); err != nil {
return nil, err
}
var v GetTargetResponse
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, err
}
return &v, nil
}
type UpdateTargetRequest struct {
// TargetId is the ID of the target to update
TargetId string `json:"-"`
// Name of the target, unique within a database (Optional)
Name *string `json:"name,omitempty"`
// Width of the target in scene unit (Optional)
Width *float64 `json:"width,omitempty"`
// Image is the base64 encoded binary recognition image data (Optional)
// https://library.vuforia.com/features/images/image-targets.html
Image *string `json:"image,omitempty"`
// Active Iidicates whether or not the target is active for query (Optional)
Active *bool `json:"active_flag,omitempty"`
// Metadata is the base64 encoded application metadata associated with the target (Optional)
Metadata *string `json:"application_metadata,omitempty"`
}
type UpdateTargetResponse struct {
// TransactionId is the ID of the transaction
TransactionId string `json:"transaction_id"`
// ResultCode is one of the VWS API Result Code.
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
ResultCode string `json:"result_code"`
}
func (c *client) UpdateTarget(ctx context.Context, input *UpdateTargetRequest) (*UpdateTargetResponse, error) {
if input == nil {
panic("input is <nil>")
}
if input.TargetId == "" {
return nil, errors.New("TargetId must be provided")
}
body, err := json.Marshal(input)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, fmt.Sprintf("https://%s/targets/%s", vuforiaUrl, input.TargetId), bytes.NewReader(body))
if err != nil {
return nil, err
}
if err = prepare(c.cfg.SecretKey, c.cfg.AccessKey, req, body); err != nil {
return nil, err
}
resp, err := c.cfg.Client.Do(req)
if err != nil {
return nil, err
}
defer safeClose(resp)
if err := checkError(resp); err != nil {
return nil, err
}
var v UpdateTargetResponse
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, err
}
return &v, nil
}
type DeleteTargetRequest struct {
// TargetId is the ID of target to delete
TargetId string
}
type DeleteTargetResponse struct {
// TransactionId is the ID of the transaction
TransactionId string `json:"transaction_id"`
// ResultCode is one of the VWS API Result Code.
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
ResultCode string `json:"result_code"`
}
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Delete-a-Target
func (c *client) DeleteTarget(ctx context.Context, input *DeleteTargetRequest) (*DeleteTargetResponse, error) {
if input == nil {
panic("input is <nil>")
}
if input.TargetId == "" {
return nil, errors.New("TargetId must be provided")
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("https://%s/targets/%s", vuforiaUrl, input.TargetId), nil)
if err != nil {
return nil, err
}
if err = prepare(c.cfg.SecretKey, c.cfg.AccessKey, req, nil); err != nil {
return nil, err
}
resp, err := c.cfg.Client.Do(req)
if err != nil {
return nil, err
}
defer safeClose(resp)
if err := checkError(resp); err != nil {
return nil, err
}
var v DeleteTargetResponse
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, err
}
return &v, nil
}
type TargetSummaryRequest struct {
// TargetId is the ID of the target to retrieve summary of
TargetId string
}
type TargetSummaryResponse struct {
// TransactionId is the ID of the transaction
TransactionId string `json:"transaction_id"`
// ResultCode is one of the VWS API Result Code.
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
ResultCode string `json:"result_code"`
// Status of the target (Processing, Success, Failure)
Status string `json:"status"`
// DatabaseName of is the database name the current target resides in
DatabaseName string `json:"database_name"`
// TargteName is the target name
TargetName string `json:"target_name"`
// UploadDate is the date of target upload (Specified as YYYY-MM-DD)
UploadDate string `json:"upload_date"`
// Active indicates whether or not the target is active for query; default is true
Active bool `json:"active_flag"`
// TrackingRating is the rating of the target recognition image for tracking purposes
TrackingRating int `json:"tracking_rating"`
// TotalRecos is the total count of the recognitions for this target
TotalRecos int `json:"total_recos"`
// CurrentMonthRecos is the total count of recognitions in the current month (Set to 0 if the Status is not "Success")
CurrentMonthRecos int `json:"current_month_recos"`
// PreviousMonthRecos is the total count of the recognitions in the previous month (Set to 0 if the Status is not "Success")
PreviousMonthRecos int `json:"previous_month_recos"`
}
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Retrieve-a-Target-Summary-Report
func (c *client) TargetSummary(ctx context.Context, input *TargetSummaryRequest) (*TargetSummaryResponse, error) {
if input == nil {
panic("input is <nil>")
}
if input.TargetId == "" {
return nil, errors.New("TargetId must be provided")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://%s/summary/%s", vuforiaUrl, input.TargetId), nil)
if err != nil {
return nil, err
}
if err = prepare(c.cfg.SecretKey, c.cfg.AccessKey, req, nil); err != nil {
return nil, err
}
resp, err := c.cfg.Client.Do(req)
if err != nil {
return nil, err
}
defer safeClose(resp)
if err := checkError(resp); err != nil {
return nil, err
}
var v TargetSummaryResponse
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, err
}
return &v, nil
}
type DatabaseSummaryResponse struct {
// TransactionId is the ID of the transaction
TransactionId string `json:"transaction_id"`
// ResultCode is one of the VWS API Result Code.
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Interperete-VWS-API-Result-Codes
ResultCode string `json:"result_code"`
// Name is the database name
Name string `json:"name"`
// ActiveImages is the total number of images with active_flag = true, and status = success for the database
ActiveImages int `json:"active_images"`
// InactiveImages is the total number of images with active_flag = false, and status = success for the database
InactiveImages int `json:"inactive_images"`
// FailedImages is the total number of images with status = fail for the data
FailedImages int `json:"failed_images"`
}
// https://library.vuforia.com/articles/Solution/How-To-Use-the-Vuforia-Web-Services-API.html#How-To-Get-a-Database-Summary-Report
func (c *client) DatabaseSummary(ctx context.Context) (*DatabaseSummaryResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://%s/summary", vuforiaUrl), nil)
if err != nil {
return nil, err
}
if err = prepare(c.cfg.SecretKey, c.cfg.AccessKey, req, nil); err != nil {
return nil, err
}
resp, err := c.cfg.Client.Do(req)
if err != nil {
return nil, err
}
defer safeClose(resp)
if err := checkError(resp); err != nil {
return nil, err
}
var v DatabaseSummaryResponse
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, err
}
return &v, nil
}
func safeClose(resp *http.Response) {
if resp.Body != nil {
_, _ = ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
}
}