-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
122 lines (94 loc) · 3 KB
/
api.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
package crunchclient
import (
"context"
b64 "encoding/base64"
"encoding/json"
"fmt"
"math"
"net/http"
"time"
)
// return all the api's for a specific api collection
func (apis ApiService) ListApis(ctx context.Context, id string) (c ApiResult, err error) {
capis, err := apis.ListApisPaged(ctx, id, 1, 50)
if err != nil {
return
}
apiItems := make([]ApiItem, 0)
apiItems = append(apiItems, capis.Items...)
if capis.Count > 50 {
pages := math.Ceil(float64(capis.Count) / 50)
// we will start at page 2 here given we already got page one
for i := 2; i < int(pages)+1; i++ {
pagedResult, perr := apis.ListApisPaged(ctx, id, i, 50)
if perr != nil {
err = fmt.Errorf("unable to read page of while retrieving api's. %w", err)
return
}
apiItems = append(apiItems, pagedResult.Items...)
}
}
var result ApiResult
result.Count = int32(len(apiItems))
result.Items = apiItems
return result, nil
}
// return paged results of api's for a specific api collection
func (apis ApiService) ListApisPaged(ctx context.Context, id string, page int, perPage int) (c ApiResult, err error) {
req, err := apis.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v2/collections/%s/apis?withTags=true&page=%d&perPage=%d", id, page, perPage))
if err != nil {
return
}
_, err = apis.client.doRequest(req, &c)
return
}
func (apis ApiService) ReadApi(ctx context.Context, id string) (c ApiItem, err error) {
req, err := apis.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/apis/%s", id))
if err != nil {
return
}
_, err = apis.client.doRequest(req, &c)
return
}
func (apis ApiService) ReadApiStatus(ctx context.Context, id string) (res ApiStatus, err error) {
c, err := apis.ReadApi(ctx, id)
if err != nil {
return
}
// we don't care if the time conversion fail, the default will work
lastAssessment, _ := time.Parse(time.RFC3339, c.Assessment.Last)
lastScan, _ := time.Parse(time.RFC3339, c.Scan.Last)
res = ApiStatus{
LastAssessment: lastAssessment,
IsAssessmentProcessed: c.Assessment.IsProcessed,
LastScan: lastScan,
IsScanProcessed: c.Scan.IsProcessed,
}
return
}
func (apis ApiService) ReadAssessmentReport(ctx context.Context, id string) (report AssessmentReport, err error) {
req, err := apis.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/apis/%s/assessmentreport", id))
if err != nil {
return
}
// todo: we first need to ensure the api is processed before we can return this report
var resp ApiAssessmentResponse
_, err = apis.client.doRequest(req, &resp)
if err != nil {
return report, err
}
if resp.Encoding != "base64" {
err = fmt.Errorf("unsupported data type")
return report, err
}
sDec, err := b64.StdEncoding.DecodeString(resp.Data)
if resp.Encoding != "base64" {
err = fmt.Errorf("Unable to decode report document data: %w", err)
return report, err
}
err = json.Unmarshal(sDec, &report)
if err != nil {
err = fmt.Errorf("Unable to decode report document: %w", err)
}
return report, err
}