Skip to content

Commit

Permalink
Merge pull request #20 from duanemay/main
Browse files Browse the repository at this point in the history
feat: add PostBom
  • Loading branch information
nscuro committed Nov 9, 2023
2 parents 93d6806 + 1a2e077 commit c0854e0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
43 changes: 43 additions & 0 deletions bom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/google/uuid"
)
Expand Down Expand Up @@ -97,6 +98,48 @@ func (bs BOMService) Upload(ctx context.Context, uploadReq BOMUploadRequest) (to
return
}

func (bs BOMService) PostBom(ctx context.Context, uploadReq BOMUploadRequest) (token BOMUploadToken, err error) {
params := make(url.Values)
if uploadReq.ProjectUUID != nil {
params["project"] = append(params["project"], uploadReq.ProjectUUID.String())
}
if uploadReq.AutoCreate {
params["autoCreate"] = append(params["autoCreate"], "true")
}
if uploadReq.ProjectName != "" {
params["projectName"] = append(params["projectName"], uploadReq.ProjectName)
}
if uploadReq.ProjectVersion != "" {
params["projectVersion"] = append(params["projectVersion"], uploadReq.ProjectVersion)
}
if uploadReq.ParentUUID != nil {
params["parentUUID"] = append(params["parentUUID"], uploadReq.ParentUUID.String())
}
if uploadReq.ParentName != "" {
params["parentName"] = append(params["parentName"], uploadReq.ParentName)
}
if uploadReq.ParentVersion != "" {
params["parentVersion"] = append(params["parentVersion"], uploadReq.ParentVersion)
}
if uploadReq.BOM != "" {
params["bom"] = append(params["bom"], uploadReq.BOM)
}

req, err := bs.client.newRequest(ctx, http.MethodPost, "/api/v1/bom", withMultiPart(params))
if err != nil {
return
}

var uploadRes bomUploadResponse
_, err = bs.client.doRequest(req, &uploadRes)
if err != nil {
return
}

token = uploadRes.Token
return
}

type bomProcessingResponse struct {
Processing bool `json:"processing"`
}
Expand Down
24 changes: 24 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -196,6 +197,29 @@ func withBody(body interface{}) requestOption {
}
}

func withMultiPart(body url.Values) requestOption {
return func(req *http.Request) error {
if body == nil {
return nil
}

var bodyBuf bytes.Buffer
multipartWriter := multipart.NewWriter(&bodyBuf)
for key, valueList := range body {
for _, value := range valueList {
fw, _ := multipartWriter.CreateFormField(key)
_, _ = fw.Write([]byte(value))
}
}

_ = multipartWriter.Close()
req.Body = io.NopCloser(&bodyBuf)
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())

return nil
}
}

type Page[T any] struct {
Items []T // Items on this page
TotalCount int // Total number of items
Expand Down

0 comments on commit c0854e0

Please sign in to comment.