-
Notifications
You must be signed in to change notification settings - Fork 4
/
asset_model.go
743 lines (630 loc) · 22.7 KB
/
asset_model.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package goqradar
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
//------------------------------------------------------------------------------
// Structures
//------------------------------------------------------------------------------
// Asset is a QRadar asset
type Asset struct {
VulnerabilityCount int `json:"vulnerability_count"`
Interfaces []Interfaces `json:"interfaces"`
RiskScoreSum float64 `json:"risk_score_sum"`
Hostnames []Hostnames `json:"hostnames"`
ID int `json:"id"`
DomainID int `json:"domain_id"`
Properties []Properties `json:"properties"`
Users []Users `json:"users"`
Products []Products `json:"products"`
}
// IPAddresses is a list of QRadar Ip addresse
type IPAddresses struct {
LastSeenProfiler int `json:"last_seen_profiler"`
Created int `json:"created"`
FirstSeenScanner int `json:"first_seen_scanner"`
LastSeenScanner int `json:"last_seen_scanner"`
NetworkID int `json:"network_id"`
ID int `json:"id"`
Type string `json:"type"`
FirstSeenProfiler int `json:"first_seen_profiler"`
Value string `json:"value"`
}
// Interfaces is a list of QRadar interface
type Interfaces struct {
MacAddress string `json:"mac_address"`
LastSeenProfiler int `json:"last_seen_profiler"`
Created int `json:"created"`
FirstSeenScanner int `json:"first_seen_scanner"`
LastSeenScanner int `json:"last_seen_scanner"`
IPAddresses []IPAddresses `json:"ip_addresses"`
ID int `json:"id"`
FirstSeenProfiler int `json:"first_seen_profiler"`
}
// Hostnames is a list of QRadar hostname
type Hostnames struct {
LastSeenProfiler int `json:"last_seen_profiler"`
Created int `json:"created"`
Name string `json:"name"`
FirstSeenScanner int `json:"first_seen_scanner"`
LastSeenScanner int `json:"last_seen_scanner"`
ID int `json:"id"`
Type string `json:"type"`
FirstSeenProfiler int `json:"first_seen_profiler"`
}
// Properties is a list of QRadar propertie
type Properties struct {
LastReported int `json:"last_reported"`
Name string `json:"name"`
TypeID int `json:"type_id"`
ID int `json:"id"`
LastReportedBy string `json:"last_reported_by"`
Value string `json:"value"`
}
// Users is a list of QRadar user
type Users struct {
LastSeenProfiler int `json:"last_seen_profiler"`
FirstSeenScanner int `json:"first_seen_scanner"`
LastSeenScanner int `json:"last_seen_scanner"`
ID int `json:"id"`
FirstSeenProfiler int `json:"first_seen_profiler"`
Username string `json:"username"`
}
// Products is a list of QRadar product
type Products struct {
LastScannedFor int `json:"last_scanned_for"`
LastSeenProfiler int `json:"last_seen_profiler"`
ProductVariantID int `json:"product_variant_id"`
FirstSeenScanner int `json:"first_seen_scanner"`
LastSeenScanner int `json:"last_seen_scanner"`
ID int `json:"id"`
FirstSeenProfiler int `json:"first_seen_profiler"`
}
// AssetsPaginatedResponse is the paginated response.
type AssetsPaginatedResponse struct {
Total int `json:"total"`
Min int `json:"min"`
Max int `json:"max"`
Assets []*Asset `json:"offense_types"`
}
// AssetPropertie is an asset propertie
type AssetPropertie struct {
Custom bool `json:"custom"`
DataType string `json:"data_type"`
Display bool `json:"display"`
ID int `json:"id"`
Name string `json:"name"`
State int `json:"state"`
}
// AssetPropertiePaginatedResponse is the paginated response.
type AssetPropertiePaginatedResponse struct {
Total int `json:"total"`
Min int `json:"min"`
Max int `json:"max"`
AssetProperties []*AssetPropertie `json:"offense_types"`
}
// AssetSavedSearchGroups is an asset saved search groups
type AssetSavedSearchGroups struct {
ChildGroups []int `json:"child_groups"`
ChildItems []string `json:"child_items"`
Description string `json:"description"`
ID int `json:"id"`
Level int `json:"level"`
ModifiedTime int `json:"modified_time"`
Name string `json:"name"`
Owner string `json:"owner"`
ParentID int `json:"parent_id"`
Type string `json:"type"`
}
// AssetSavedSearchGroupPaginatedResponse is the paginated response.
type AssetSavedSearchGroupPaginatedResponse struct {
Total int `json:"total"`
Min int `json:"min"`
Max int `json:"max"`
AssetsSavedSearchGroups []*AssetSavedSearchGroups `json:"offense_types"`
}
// SavedSearche is a saved searches
type SavedSearche struct {
Columns []Column `json:"columns"`
Description string `json:"description"`
Filters []Filter `json:"filters"`
ID int `json:"id"`
IsShared bool `json:"is_shared"`
Name string `json:"name"`
Owner string `json:"owner"`
}
// Column is a column
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
}
// Filter is a filter
type Filter struct {
Operator string `json:"operator"`
Parameter string `json:"parameter"`
Value string `json:"value"`
}
// SavedSearchesPaginatedResponse is the paginated response.
type SavedSearchesPaginatedResponse struct {
Total int `json:"total"`
Min int `json:"min"`
Max int `json:"max"`
SavedSearches []*SavedSearche `json:"offense_types"`
}
// AssetBasedOnSavedSearch is an asset based on the result of an asset saved search
type AssetBasedOnSavedSearch struct {
DomainID int `json:"domain_id"`
ID int `json:"id"`
Interfaces []Interfaces `json:"interfaces"`
Properties []Properties `json:"properties"`
}
// AssetBasedOnSavedSearchPaginatedResponse is the paginated response.
type AssetBasedOnSavedSearchPaginatedResponse struct {
Total int `json:"total"`
Min int `json:"min"`
Max int `json:"max"`
AssetsBasedOnSavedSearch []*AssetBasedOnSavedSearch `json:"offense_types"`
}
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
// ListAssets returns the assets with given fields, filters and sort.
func (endpoint *Endpoint) ListAssets(ctx context.Context, fields, filter, sort string, min, max int) (*AssetsPaginatedResponse, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
if filter != "" {
options = append(options, WithParam("filter", filter))
}
if sort != "" {
options = append(options, WithParam("sort", sort))
}
options = append(options, WithHeader("Range", fmt.Sprintf("items=%d-%d", min, max)))
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "asset_model/assets", options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Process the Content-Range
min, max, total, err := parseContentRange(resp.Header.Get("Content-Range"))
if err != nil {
return nil, fmt.Errorf("error while parsing the content-range [%s]: %s", resp.Header.Get("Content-Range"), err)
}
// Prepare the response
response := &AssetsPaginatedResponse{
Total: total,
Min: min,
Max: max,
}
// Decode the response
err = json.NewDecoder(resp.Body).Decode(&response.Assets)
if err != nil {
return nil, fmt.Errorf("error while decoding the response: %s", err)
}
return response, nil
}
// UpdateAsset by name
func (endpoint *Endpoint) UpdateAsset(ctx context.Context, name string, data map[string]map[string]string) (string, error) {
// Prepare the URL
var reqURL *url.URL
reqURL, err := url.Parse(endpoint.client.BaseURL)
if err != nil {
return "", fmt.Errorf("Error while parsing the URL : %s", err)
}
reqURL.Path += "/asset_model/assets/"
reqURL.Path += name
// Create the data
d, err := json.Marshal(data)
if err != nil {
return "", fmt.Errorf("Error while marshalling the values : %s", err)
}
// Create the request
req, err := http.NewRequest("POST", reqURL.String(), bytes.NewBuffer(d))
if err != nil {
return "", fmt.Errorf("Error while creating the request : %s", err)
}
// Set HTTP headers
req.Header.Set("SEC", endpoint.client.Token)
req.Header.Set("Version", endpoint.client.Version)
req.Header.Set("Content-Type", "application/json")
// Do the request
resp, err := endpoint.client.client.Do(req)
if err != nil {
return "", fmt.Errorf("Error while doing the request : %s", err)
}
defer resp.Body.Close()
// Read the respsonse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error while reading the request : %s", err)
}
// Prepare the response
var response string
// Unmarshal the response
err = json.Unmarshal([]byte(body), &response)
if err != nil {
return "", fmt.Errorf("Error while unmarshalling the response : %s. HTTP response is : %s", err, string(body))
}
return response, nil
}
// ListAssetProperties returns the assets properties.
func (endpoint *Endpoint) ListAssetProperties(ctx context.Context, fields, filter string, min, max int) (*AssetPropertiePaginatedResponse, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
if filter != "" {
options = append(options, WithParam("filter", filter))
}
options = append(options, WithHeader("Range", fmt.Sprintf("items=%d-%d", min, max)))
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "/asset_model/properties", options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Process the Content-Range
min, max, total, err := parseContentRange(resp.Header.Get("Content-Range"))
if err != nil {
return nil, fmt.Errorf("error while parsing the content-range [%s]: %s", resp.Header.Get("Content-Range"), err)
}
// Prepare the response
response := &AssetPropertiePaginatedResponse{
Total: total,
Min: min,
Max: max,
}
// Decode the response
err = json.NewDecoder(resp.Body).Decode(&response.AssetProperties)
if err != nil {
return nil, fmt.Errorf("error while decoding the response: %s", err)
}
return response, nil
}
// ListAssetsSavedSearchGroups returns the assets saved searh groups.
func (endpoint *Endpoint) ListAssetsSavedSearchGroups(ctx context.Context, fields, filter string, min, max int) (*AssetSavedSearchGroupPaginatedResponse, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
if filter != "" {
options = append(options, WithParam("filter", filter))
}
options = append(options, WithHeader("Range", fmt.Sprintf("items=%d-%d", min, max)))
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "/asset_model/saved_search_groups", options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Process the Content-Range
min, max, total, err := parseContentRange(resp.Header.Get("Content-Range"))
if err != nil {
return nil, fmt.Errorf("error while parsing the content-range [%s]: %s", resp.Header.Get("Content-Range"), err)
}
// Prepare the response
response := &AssetSavedSearchGroupPaginatedResponse{
Total: total,
Min: min,
Max: max,
}
// Decode the response
err = json.NewDecoder(resp.Body).Decode(&response.AssetsSavedSearchGroups)
if err != nil {
return nil, fmt.Errorf("error while decoding the response: %s", err)
}
return response, nil
}
// GetAssetSavedSearchGroups retrieves an asset saved search group.
func (endpoint *Endpoint) GetAssetSavedSearchGroups(ctx context.Context, id int, fields string) (*AssetSavedSearchGroups, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "/asset_model/saved_search_groups/"+strconv.Itoa(id), options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Read the respsonse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error while reading the request : %s", err)
}
// Prepare the response
var response *AssetSavedSearchGroups
// Unmarshal the response
err = json.Unmarshal([]byte(body), &response)
if err != nil {
return nil, fmt.Errorf("Error while unmarshalling the response : %s. HTTP response is : %s", err, string(body))
}
return response, nil
}
// UpdateAssetSavedSeachGroup update the owner of the asset saved search group
func (endpoint *Endpoint) UpdateAssetSavedSeachGroup(ctx context.Context, name int, fields string, data map[string]map[string]string) (*AssetSavedSearchGroups, error) {
// Prepare the URL
var reqURL *url.URL
reqURL, err := url.Parse(endpoint.client.BaseURL)
if err != nil {
return nil, fmt.Errorf("Error while parsing the URL : %s", err)
}
reqURL.Path += "/asset_model/saved_search_groups/"
reqURL.Path += strconv.Itoa(name)
// Create the data
d, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("Error while marshalling the values : %s", err)
}
// Create the request
req, err := http.NewRequest("POST", reqURL.String(), bytes.NewBuffer(d))
if err != nil {
return nil, fmt.Errorf("Error while creating the request : %s", err)
}
// Set HTTP headers
req.Header.Set("SEC", endpoint.client.Token)
req.Header.Set("Version", endpoint.client.Version)
req.Header.Set("Content-Type", "application/json")
if fields != "" {
req.Header.Set("fields", fields)
}
// Do the request
resp, err := endpoint.client.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error while doing the request : %s", err)
}
defer resp.Body.Close()
// Read the respsonse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error while reading the request : %s", err)
}
// Prepare the response
var response *AssetSavedSearchGroups
// Unmarshal the response
err = json.Unmarshal([]byte(body), &response)
if err != nil {
return nil, fmt.Errorf("Error while unmarshalling the response : %s. HTTP response is : %s", err, string(body))
}
return response, nil
}
// DeleteAssetSavedSearchGroups by groupID
func (endpoint *Endpoint) DeleteAssetSavedSearchGroups(ctx context.Context, name int) error {
// Prepare the URL
var reqURL *url.URL
reqURL, err := url.Parse(endpoint.client.BaseURL)
if err != nil {
return fmt.Errorf("Error while parsing the URL : %s", err)
}
reqURL.Path += "/asset_model/saved_search_groups/"
reqURL.Path += strconv.Itoa(name)
// Create the request
req, err := http.NewRequest("DELETE", reqURL.String(), nil)
if err != nil {
return fmt.Errorf("Error while creating the request : %s", err)
}
// Set HTTP headers
req.Header.Set("SEC", endpoint.client.Token)
req.Header.Set("Version", endpoint.client.Version)
req.Header.Set("Content-Type", "application/json")
// Do the request
resp, err := endpoint.client.client.Do(req)
if err != nil {
return fmt.Errorf("Error while doing the request : %s", err)
}
defer resp.Body.Close()
// Check the status code
if resp.StatusCode != 204 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Status code is %d : Error while reading the body", resp.StatusCode)
}
return fmt.Errorf("Status code is %d : %s", resp.StatusCode, string(body))
}
return nil
}
// ListSavedSearches returns a list of saved searches, filters and sort.
func (endpoint *Endpoint) ListSavedSearches(ctx context.Context, fields, filter string, min, max int) (*SavedSearchesPaginatedResponse, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
if filter != "" {
options = append(options, WithParam("filter", filter))
}
options = append(options, WithHeader("Range", fmt.Sprintf("items=%d-%d", min, max)))
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "/asset_model/saved_searches", options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Process the Content-Range
min, max, total, err := parseContentRange(resp.Header.Get("Content-Range"))
if err != nil {
return nil, fmt.Errorf("error while parsing the content-range [%s]: %s", resp.Header.Get("Content-Range"), err)
}
// Prepare the response
response := &SavedSearchesPaginatedResponse{
Total: total,
Min: min,
Max: max,
}
// Decode the response
err = json.NewDecoder(resp.Body).Decode(&response.SavedSearches)
if err != nil {
return nil, fmt.Errorf("error while decoding the response: %s", err)
}
return response, nil
}
// GetAssetSavedSearch retrieves an asset saved search .
func (endpoint *Endpoint) GetAssetSavedSearch(ctx context.Context, id int, fields string) (*SavedSearche, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "/asset_model/saved_searches/"+strconv.Itoa(id), options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Read the respsonse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error while reading the request : %s", err)
}
// Prepare the response
var response *SavedSearche
// Unmarshal the response
err = json.Unmarshal([]byte(body), &response)
if err != nil {
return nil, fmt.Errorf("Error while unmarshalling the response : %s. HTTP response is : %s", err, string(body))
}
return response, nil
}
// UpdateAssetSavedSearch by name
func (endpoint *Endpoint) UpdateAssetSavedSearch(ctx context.Context, name int, data map[string]map[string]string, fields string) (*SavedSearche, error) {
// Prepare the URL
var reqURL *url.URL
reqURL, err := url.Parse(endpoint.client.BaseURL)
if err != nil {
return nil, fmt.Errorf("Error while parsing the URL : %s", err)
}
reqURL.Path += "/asset_model/saved_searches/"
reqURL.Path += strconv.Itoa(name)
// Create the data
d, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("Error while marshalling the values : %s", err)
}
// Create the request
req, err := http.NewRequest("POST", reqURL.String(), bytes.NewBuffer(d))
if err != nil {
return nil, fmt.Errorf("Error while creating the request : %s", err)
}
// Set HTTP headers
req.Header.Set("SEC", endpoint.client.Token)
req.Header.Set("Version", endpoint.client.Version)
req.Header.Set("Content-Type", "application/json")
if fields != "" {
req.Header.Set("fields", fields)
}
// Do the request
resp, err := endpoint.client.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error while doing the request : %s", err)
}
defer resp.Body.Close()
// Read the respsonse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error while reading the request : %s", err)
}
// Prepare the response
var response *SavedSearche
// Unmarshal the response
err = json.Unmarshal([]byte(body), &response)
if err != nil {
return nil, fmt.Errorf("Error while unmarshalling the response : %s. HTTP response is : %s", err, string(body))
}
return response, nil
}
// DeleteAssetSavedSearch by groupID
func (endpoint *Endpoint) DeleteAssetSavedSearch(ctx context.Context, name int) error {
// Prepare the URL
var reqURL *url.URL
reqURL, err := url.Parse(endpoint.client.BaseURL)
if err != nil {
return fmt.Errorf("Error while parsing the URL : %s", err)
}
reqURL.Path += "/asset_model/saved_searches/"
reqURL.Path += strconv.Itoa(name)
// Create the request
req, err := http.NewRequest("DELETE", reqURL.String(), nil)
if err != nil {
return fmt.Errorf("Error while creating the request : %s", err)
}
// Set HTTP headers
req.Header.Set("SEC", endpoint.client.Token)
req.Header.Set("Version", endpoint.client.Version)
req.Header.Set("Content-Type", "application/json")
// Do the request
resp, err := endpoint.client.client.Do(req)
if err != nil {
return fmt.Errorf("Error while doing the request : %s", err)
}
defer resp.Body.Close()
// Check the status code
if resp.StatusCode != 204 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Status code is %d : Error while reading the body", resp.StatusCode)
}
return fmt.Errorf("Status code is %d : %s", resp.StatusCode, string(body))
}
return nil
}
// ListAssetSavedSearches returns a list of saved searches, filters and sort.
func (endpoint *Endpoint) ListAssetSavedSearches(ctx context.Context, name, fields, filter string, min, max int) (*AssetBasedOnSavedSearchPaginatedResponse, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
if filter != "" {
options = append(options, WithParam("filter", filter))
}
options = append(options, WithHeader("Range", fmt.Sprintf("items=%d-%d", min, max)))
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "/asset_model/saved_searches/"+name+"/results", options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Process the Content-Range
min, max, total, err := parseContentRange(resp.Header.Get("Content-Range"))
if err != nil {
return nil, fmt.Errorf("error while parsing the content-range [%s]: %s", resp.Header.Get("Content-Range"), err)
}
// Prepare the response
response := &AssetBasedOnSavedSearchPaginatedResponse{
Total: total,
Min: min,
Max: max,
}
// Decode the response
err = json.NewDecoder(resp.Body).Decode(&response.AssetsBasedOnSavedSearch)
if err != nil {
return nil, fmt.Errorf("error while decoding the response: %s", err)
}
return response, nil
}