Skip to content

Commit c0854e0

Browse files
authored
Merge pull request #20 from duanemay/main
feat: add PostBom
2 parents 93d6806 + 1a2e077 commit c0854e0

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

bom.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"net/url"
78

89
"github.com/google/uuid"
910
)
@@ -97,6 +98,48 @@ func (bs BOMService) Upload(ctx context.Context, uploadReq BOMUploadRequest) (to
9798
return
9899
}
99100

101+
func (bs BOMService) PostBom(ctx context.Context, uploadReq BOMUploadRequest) (token BOMUploadToken, err error) {
102+
params := make(url.Values)
103+
if uploadReq.ProjectUUID != nil {
104+
params["project"] = append(params["project"], uploadReq.ProjectUUID.String())
105+
}
106+
if uploadReq.AutoCreate {
107+
params["autoCreate"] = append(params["autoCreate"], "true")
108+
}
109+
if uploadReq.ProjectName != "" {
110+
params["projectName"] = append(params["projectName"], uploadReq.ProjectName)
111+
}
112+
if uploadReq.ProjectVersion != "" {
113+
params["projectVersion"] = append(params["projectVersion"], uploadReq.ProjectVersion)
114+
}
115+
if uploadReq.ParentUUID != nil {
116+
params["parentUUID"] = append(params["parentUUID"], uploadReq.ParentUUID.String())
117+
}
118+
if uploadReq.ParentName != "" {
119+
params["parentName"] = append(params["parentName"], uploadReq.ParentName)
120+
}
121+
if uploadReq.ParentVersion != "" {
122+
params["parentVersion"] = append(params["parentVersion"], uploadReq.ParentVersion)
123+
}
124+
if uploadReq.BOM != "" {
125+
params["bom"] = append(params["bom"], uploadReq.BOM)
126+
}
127+
128+
req, err := bs.client.newRequest(ctx, http.MethodPost, "/api/v1/bom", withMultiPart(params))
129+
if err != nil {
130+
return
131+
}
132+
133+
var uploadRes bomUploadResponse
134+
_, err = bs.client.doRequest(req, &uploadRes)
135+
if err != nil {
136+
return
137+
}
138+
139+
token = uploadRes.Token
140+
return
141+
}
142+
100143
type bomProcessingResponse struct {
101144
Processing bool `json:"processing"`
102145
}

client.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"log"
13+
"mime/multipart"
1314
"net/http"
1415
"net/http/httputil"
1516
"net/url"
@@ -196,6 +197,29 @@ func withBody(body interface{}) requestOption {
196197
}
197198
}
198199

200+
func withMultiPart(body url.Values) requestOption {
201+
return func(req *http.Request) error {
202+
if body == nil {
203+
return nil
204+
}
205+
206+
var bodyBuf bytes.Buffer
207+
multipartWriter := multipart.NewWriter(&bodyBuf)
208+
for key, valueList := range body {
209+
for _, value := range valueList {
210+
fw, _ := multipartWriter.CreateFormField(key)
211+
_, _ = fw.Write([]byte(value))
212+
}
213+
}
214+
215+
_ = multipartWriter.Close()
216+
req.Body = io.NopCloser(&bodyBuf)
217+
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
218+
219+
return nil
220+
}
221+
}
222+
199223
type Page[T any] struct {
200224
Items []T // Items on this page
201225
TotalCount int // Total number of items

0 commit comments

Comments
 (0)