Skip to content

Commit ee3630c

Browse files
wip
1 parent d4c1727 commit ee3630c

File tree

9 files changed

+203
-73
lines changed

9 files changed

+203
-73
lines changed

internal/fileupload/client_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//go:generate go run github.com/golang/mock/mockgen -package=mocks -destination=./mocks/mock_http_client.go github.com/snyk/cli-extension-os-flows/internal/fileupload/lowlevel SealableClient
2+
package fileupload
3+
4+
import (
5+
"github.com/golang/mock/gomock"
6+
"github.com/snyk/cli-extension-os-flows/internal/fileupload/lowlevel"
7+
"github.com/snyk/cli-extension-os-flows/internal/fileupload/mocks"
8+
"os"
9+
"testing"
10+
)
11+
12+
type File struct {
13+
filename string
14+
content string
15+
}
16+
17+
type FileUploadClient struct {
18+
}
19+
20+
func (c FileUploadClient) UploadDir(dir os.File) (RevisionID, error) {
21+
22+
}
23+
24+
func Test_uploadingADirectory(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
defer ctrl.Finish()
27+
sealableClientMock := mocks.NewMockSealableClient(ctrl)
28+
client := NewFileUploadClient(sealableClientMock)
29+
30+
dir := createDirWithFiles(File{filename: "file1", content: "foo"})
31+
32+
revisionID, err := client.UploadDir(dir)
33+
34+
sealableClientFake.uploadedAndSealed(File{filename: "file1", content: "foo"})
35+
36+
}
37+
38+
func NewFileUploadClient(client lowlevel.SealableClient) FileUploadClient {
39+
40+
}
41+
42+
func createDirWithFiles(file File) os.File {
43+
44+
}

internal/fileupload/lowlevel/client.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package lowlevel_fileupload //nolint:revive // underscore naming is intentional for this internal package
1+
package lowlevel
22

33
import (
44
"bytes"
55
"context"
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"github.com/snyk/cli-extension-os-flows/internal/fileupload"
910
"io"
1011
"mime/multipart"
1112
"net/http"
@@ -14,23 +15,23 @@ import (
1415
"github.com/snyk/error-catalog-golang-public/snyk_errors"
1516
)
1617

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 {
1920
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)
2223
}
2324

2425
// This will force go to complain if the type doesn't satisfy the interface.
25-
var _ Client = (*HTTPClient)(nil)
26+
var _ SealableClient = (*HTTPSealableClient)(nil)
2627

2728
// Config contains configuration for the file upload client.
2829
type Config struct {
2930
BaseURL string
3031
}
3132

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 {
3435
cfg Config
3536
httpClient *http.Client
3637
}
@@ -48,8 +49,8 @@ const FileCountLimit = 100 // arbitrary number, will need to be re-evaluated
4849
const ContentType = "Content-Type"
4950

5051
// 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}
5354

5455
for _, opt := range opts {
5556
opt(&c)
@@ -59,7 +60,7 @@ func NewClient(cfg Config, opts ...Opt) *HTTPClient {
5960
}
6061

6162
// 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) {
6364
if orgID == uuid.Nil {
6465
return nil, ErrEmptyOrgID
6566
}
@@ -103,7 +104,7 @@ func (c *HTTPClient) CreateRevision(ctx context.Context, orgID OrgID) (*UploadRe
103104
}
104105

105106
// 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 {
107108
if orgID == uuid.Nil {
108109
return ErrEmptyOrgID
109110
}
@@ -165,7 +166,7 @@ func (c *HTTPClient) UploadFiles(ctx context.Context, orgID OrgID, revisionID Re
165166
return nil
166167
}
167168

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) {
169170
var streamError error
170171
defer func() {
171172
pWriter.CloseWithError(streamError)
@@ -189,7 +190,7 @@ func (c *HTTPClient) streamFilesToPipe(pWriter *io.PipeWriter, mpartWriter *mult
189190
}
190191

191192
// 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) {
193194
if orgID == uuid.Nil {
194195
return nil, ErrEmptyOrgID
195196
}
@@ -237,7 +238,7 @@ func (c *HTTPClient) SealRevision(ctx context.Context, orgID OrgID, revisionID R
237238
return &respBody, nil
238239
}
239240

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 {
241242
bts, err := io.ReadAll(body)
242243
if err != nil {
243244
return fmt.Errorf("failed to read response body: %w", err)

0 commit comments

Comments
 (0)