Skip to content

Commit 909e777

Browse files
authored
Merge pull request #18 from xataio/feat/files-client
add delete file method
2 parents 52f14bc + 2671482 commit 909e777

File tree

5 files changed

+242
-4
lines changed

5 files changed

+242
-4
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package integrationtests
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/go-retryablehttp"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/xataio/xata-go/xata"
11+
)
12+
13+
func Test_filesClient(t *testing.T) {
14+
cfg, err := setupDatabase()
15+
if err != nil {
16+
t.Fatalf("unable to setup database: %v", err)
17+
}
18+
19+
ctx := context.TODO()
20+
err = setupTableWithColumns(ctx, cfg)
21+
if err != nil {
22+
t.Fatalf("unable to setup table: %v", err)
23+
}
24+
25+
t.Cleanup(func() {
26+
err = cleanup(cfg)
27+
if err != nil {
28+
t.Fatalf("unable to cleanup test setup: %v", err)
29+
}
30+
})
31+
32+
filesCli, err := xata.NewFilesClient(xata.WithAPIKey(cfg.apiKey),
33+
xata.WithBaseURL(fmt.Sprintf(
34+
"https://%s.%s.xata.sh",
35+
cfg.wsID,
36+
cfg.region,
37+
)),
38+
xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()),
39+
)
40+
if err != nil {
41+
t.Fatalf("unable to construct files cli: %v", err)
42+
}
43+
44+
recordsCli, err := xata.NewRecordsClient(
45+
xata.WithAPIKey(cfg.apiKey),
46+
xata.WithBaseURL(fmt.Sprintf(
47+
"https://%s.%s.xata.sh",
48+
cfg.wsID,
49+
cfg.region,
50+
)),
51+
xata.WithHTTPClient(retryablehttp.NewClient().StandardClient()),
52+
)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
insertRecordRequest := generateInsertRecordRequest(cfg.databaseName, cfg.tableName)
58+
59+
record, err := recordsCli.Insert(ctx, insertRecordRequest)
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
assert.NotNil(t, record)
64+
65+
t.Run("delete a file", func(t *testing.T) {
66+
delRes, err := filesCli.Delete(ctx, xata.DeleteFileRequest{
67+
BranchRequestOptional: xata.BranchRequestOptional{
68+
DatabaseName: xata.String(cfg.databaseName),
69+
},
70+
TableName: cfg.tableName,
71+
RecordId: record.Id,
72+
ColumnName: fileColumn,
73+
})
74+
assert.NoError(t, err)
75+
assert.Equal(t, testFileName, delRes.Name)
76+
})
77+
}

xata/files_cilent.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package xata
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
xatagenworkspace "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go"
8+
xatagenclient "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go/core"
9+
)
10+
11+
type FilesClient interface {
12+
// GetFileItem(ctx context.Context, dbBranchName DbBranchName, tableName TableName, recordId RecordId, columnName ColumnName, fileId FileItemId) error
13+
// PutFileItem(ctx context.Context, dbBranchName DbBranchName, tableName TableName, recordId RecordId, columnName ColumnName, fileId FileItemId) (*FileResponse, error)
14+
// DeleteFileItem(ctx context.Context, dbBranchName DbBranchName, tableName TableName, recordId RecordId, columnName ColumnName, fileId FileItemId) (*FileResponse, error)
15+
// GetFile(ctx context.Context, dbBranchName DbBranchName, tableName TableName, recordId RecordId, columnName ColumnName) error
16+
// PutFile(ctx context.Context, dbBranchName DbBranchName, tableName TableName, recordId RecordId, columnName ColumnName) (*FileResponse, error)
17+
Delete(ctx context.Context, request DeleteFileRequest) (*xatagenworkspace.FileResponse, error)
18+
}
19+
20+
type filesClient struct {
21+
generated xatagenworkspace.FilesClient
22+
dbName string
23+
branchName string
24+
}
25+
26+
func (f filesClient) dbBranchName(request BranchRequestOptional) (string, error) {
27+
if request.DatabaseName == nil {
28+
if f.dbName == "" {
29+
return "", fmt.Errorf("database name cannot be empty")
30+
}
31+
request.DatabaseName = String(f.dbName)
32+
}
33+
34+
if request.BranchName == nil {
35+
if f.branchName == "" {
36+
return "", fmt.Errorf("branch name cannot be empty")
37+
}
38+
request.BranchName = String(f.branchName)
39+
}
40+
41+
return fmt.Sprintf("%s:%s", *request.DatabaseName, *request.BranchName), nil
42+
}
43+
44+
type DeleteFileRequest struct {
45+
BranchRequestOptional
46+
TableName string
47+
RecordId string
48+
ColumnName string
49+
}
50+
51+
func (f filesClient) Delete(ctx context.Context, request DeleteFileRequest) (*xatagenworkspace.FileResponse, error) {
52+
dbBranchName, err := f.dbBranchName(request.BranchRequestOptional)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
return f.generated.DeleteFile(ctx, dbBranchName, request.TableName, request.RecordId, request.ColumnName)
58+
}
59+
60+
func NewFilesClient(opts ...ClientOption) (FilesClient, error) {
61+
cliOpts, dbCfg, err := consolidateClientOptionsForWorkspace(opts...)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
return filesClient{
67+
generated: xatagenworkspace.NewFilesClient(
68+
func(options *xatagenclient.ClientOptions) {
69+
options.HTTPClient = cliOpts.HTTPClient
70+
options.BaseURL = cliOpts.BaseURL
71+
options.Bearer = cliOpts.Bearer
72+
}),
73+
dbName: dbCfg.dbName,
74+
branchName: dbCfg.branchName,
75+
},
76+
nil
77+
}

xata/files_cilent_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package xata_test
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/xataio/xata-go/xata"
10+
xatagenworkspace "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go"
11+
12+
xatagencore "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go/core"
13+
)
14+
15+
func TestNewFilesClient(t *testing.T) {
16+
t.Run("should construct a new client", func(t *testing.T) {
17+
got, err := xata.NewFilesClient(
18+
xata.WithBaseURL("https://www.example.com"),
19+
xata.WithAPIKey("my-api-token"),
20+
)
21+
assert.NoError(t, err)
22+
assert.NotNil(t, got)
23+
})
24+
}
25+
26+
func Test_filesClient_Delete(t *testing.T) {
27+
assert := assert.New(t)
28+
29+
type tc struct {
30+
name string
31+
want *xatagenworkspace.FileResponse
32+
statusCode int
33+
apiErr *xatagencore.APIError
34+
}
35+
36+
tests := []tc{
37+
{
38+
name: "should delete a file successfully",
39+
want: &xatagenworkspace.FileResponse{
40+
Name: "test-file.txt",
41+
},
42+
statusCode: http.StatusOK,
43+
},
44+
}
45+
46+
for _, eTC := range errTestCasesWorkspace {
47+
tests = append(tests, tc{
48+
name: eTC.name,
49+
statusCode: eTC.statusCode,
50+
apiErr: eTC.apiErr,
51+
})
52+
}
53+
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
testSrv := testService(t, http.MethodDelete, "/db", tt.statusCode, tt.apiErr != nil, tt.want)
57+
58+
cli, err := xata.NewFilesClient(xata.WithBaseURL(testSrv.URL), xata.WithAPIKey("test-key"))
59+
assert.NoError(err)
60+
assert.NotNil(cli)
61+
62+
got, err := cli.Delete(context.TODO(), xata.DeleteFileRequest{
63+
BranchRequestOptional: xata.BranchRequestOptional{
64+
DatabaseName: xata.String("my-db"),
65+
},
66+
TableName: "my-table",
67+
RecordId: "my-id",
68+
ColumnName: "file-column",
69+
})
70+
71+
if tt.apiErr != nil {
72+
errAPI := tt.apiErr.Unwrap()
73+
if errAPI == nil {
74+
t.Fatal("expected error but got nil")
75+
}
76+
assert.ErrorAs(err, &errAPI)
77+
assert.Equal(err.Error(), tt.apiErr.Error())
78+
assert.Nil(got)
79+
} else {
80+
assert.Equal(tt.want.Name, got.Name)
81+
assert.NoError(err)
82+
}
83+
})
84+
}
85+
}

xata/records_client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"strings"
88
"time"
99

10-
xatagenclient "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go/core"
11-
1210
xatagenworkspace "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go"
11+
xatagenclient "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go/core"
1312
)
1413

1514
type RecordRequest struct {

xata/records_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"net/http"
66
"testing"
77

8-
xatagenworkspace "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go"
9-
108
"github.com/stretchr/testify/assert"
119
"github.com/xataio/xata-go/xata"
10+
11+
xatagenworkspace "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go"
1212
xatagencore "github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go/core"
1313
)
1414

0 commit comments

Comments
 (0)