1
- package lowlevel_fileupload //nolint:revive // underscore naming is intentional for this internal package
1
+ package lowlevel
2
2
3
3
import (
4
4
"bytes"
5
5
"context"
6
6
"encoding/json"
7
7
"errors"
8
8
"fmt"
9
+ "github.com/snyk/cli-extension-os-flows/internal/fileupload"
9
10
"io"
10
11
"mime/multipart"
11
12
"net/http"
@@ -14,23 +15,23 @@ import (
14
15
"github.com/snyk/error-catalog-golang-public/snyk_errors"
15
16
)
16
17
17
- // Client defines the interface for file upload API operations.
18
- type Client interface {
18
+ // SealableClient defines the interface for file upload API operations.
19
+ type SealableClient interface {
19
20
CreateRevision (ctx context.Context , orgID OrgID ) (* UploadRevisionResponseBody , error )
20
- UploadFiles (ctx context.Context , orgID OrgID , revisionID RevisionID , files []UploadFile ) error
21
- SealRevision (ctx context.Context , orgID OrgID , revisionID RevisionID ) (* SealUploadRevisionResponseBody , error )
21
+ UploadFiles (ctx context.Context , orgID OrgID , revisionID fileupload. RevisionID , files []UploadFile ) error
22
+ SealRevision (ctx context.Context , orgID OrgID , revisionID fileupload. RevisionID ) (* SealUploadRevisionResponseBody , error )
22
23
}
23
24
24
25
// This will force go to complain if the type doesn't satisfy the interface.
25
- var _ Client = (* HTTPClient )(nil )
26
+ var _ SealableClient = (* HTTPSealableClient )(nil )
26
27
27
28
// Config contains configuration for the file upload client.
28
29
type Config struct {
29
30
BaseURL string
30
31
}
31
32
32
- // HTTPClient implements the Client interface for file upload operations via HTTP API.
33
- type HTTPClient struct {
33
+ // HTTPSealableClient implements the SealableClient interface for file upload operations via HTTP API.
34
+ type HTTPSealableClient struct {
34
35
cfg Config
35
36
httpClient * http.Client
36
37
}
@@ -48,8 +49,8 @@ const FileCountLimit = 100 // arbitrary number, will need to be re-evaluated
48
49
const ContentType = "Content-Type"
49
50
50
51
// NewClient creates a new file upload client with the given configuration and options.
51
- func NewClient (cfg Config , opts ... Opt ) * HTTPClient {
52
- c := HTTPClient {cfg , http .DefaultClient }
52
+ func NewClient (cfg Config , opts ... Opt ) * HTTPSealableClient {
53
+ c := HTTPSealableClient {cfg , http .DefaultClient }
53
54
54
55
for _ , opt := range opts {
55
56
opt (& c )
@@ -59,7 +60,7 @@ func NewClient(cfg Config, opts ...Opt) *HTTPClient {
59
60
}
60
61
61
62
// CreateRevision creates a new upload revision for the specified organization.
62
- func (c * HTTPClient ) CreateRevision (ctx context.Context , orgID OrgID ) (* UploadRevisionResponseBody , error ) {
63
+ func (c * HTTPSealableClient ) CreateRevision (ctx context.Context , orgID OrgID ) (* UploadRevisionResponseBody , error ) {
63
64
if orgID == uuid .Nil {
64
65
return nil , ErrEmptyOrgID
65
66
}
@@ -103,7 +104,7 @@ func (c *HTTPClient) CreateRevision(ctx context.Context, orgID OrgID) (*UploadRe
103
104
}
104
105
105
106
// UploadFiles uploads the provided files to the specified revision. It will not close the file descriptors.
106
- func (c * HTTPClient ) UploadFiles (ctx context.Context , orgID OrgID , revisionID RevisionID , files []UploadFile ) error {
107
+ func (c * HTTPSealableClient ) UploadFiles (ctx context.Context , orgID OrgID , revisionID fileupload. RevisionID , files []UploadFile ) error {
107
108
if orgID == uuid .Nil {
108
109
return ErrEmptyOrgID
109
110
}
@@ -165,7 +166,7 @@ func (c *HTTPClient) UploadFiles(ctx context.Context, orgID OrgID, revisionID Re
165
166
return nil
166
167
}
167
168
168
- func (c * HTTPClient ) streamFilesToPipe (pWriter * io.PipeWriter , mpartWriter * multipart.Writer , files []UploadFile ) {
169
+ func (c * HTTPSealableClient ) streamFilesToPipe (pWriter * io.PipeWriter , mpartWriter * multipart.Writer , files []UploadFile ) {
169
170
var streamError error
170
171
defer func () {
171
172
pWriter .CloseWithError (streamError )
@@ -189,7 +190,7 @@ func (c *HTTPClient) streamFilesToPipe(pWriter *io.PipeWriter, mpartWriter *mult
189
190
}
190
191
191
192
// SealRevision seals the specified upload revision, marking it as complete.
192
- func (c * HTTPClient ) SealRevision (ctx context.Context , orgID OrgID , revisionID RevisionID ) (* SealUploadRevisionResponseBody , error ) {
193
+ func (c * HTTPSealableClient ) SealRevision (ctx context.Context , orgID OrgID , revisionID fileupload. RevisionID ) (* SealUploadRevisionResponseBody , error ) {
193
194
if orgID == uuid .Nil {
194
195
return nil , ErrEmptyOrgID
195
196
}
@@ -237,7 +238,7 @@ func (c *HTTPClient) SealRevision(ctx context.Context, orgID OrgID, revisionID R
237
238
return & respBody , nil
238
239
}
239
240
240
- func (c * HTTPClient ) handleUnexpectedStatusCodes (body io.ReadCloser , statusCode int , status , operation string ) error {
241
+ func (c * HTTPSealableClient ) handleUnexpectedStatusCodes (body io.ReadCloser , statusCode int , status , operation string ) error {
241
242
bts , err := io .ReadAll (body )
242
243
if err != nil {
243
244
return fmt .Errorf ("failed to read response body: %w" , err )
0 commit comments