Skip to content

Commit

Permalink
chore: refactor tenant api client
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed May 2, 2024
1 parent 0f04429 commit 064c806
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 31 deletions.
13 changes: 6 additions & 7 deletions internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/internal/utils/tenant"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/cli/pkg/fetcher"
)

var updatedConfig ConfigCopy
Expand Down Expand Up @@ -130,8 +129,8 @@ func linkPostgrest(ctx context.Context, projectRef string) error {
return nil
}

func linkPostgrestVersion(ctx context.Context, api *fetcher.Fetcher, fsys afero.Fs) error {
version, err := tenant.GetPostgrestVersion(ctx, api)
func linkPostgrestVersion(ctx context.Context, api tenant.TenantAPI, fsys afero.Fs) error {
version, err := api.GetPostgrestVersion(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -163,16 +162,16 @@ func readCsv(line string) []string {
return result
}

func linkGotrueVersion(ctx context.Context, api *fetcher.Fetcher, fsys afero.Fs) error {
version, err := tenant.GetGotrueVersion(ctx, api)
func linkGotrueVersion(ctx context.Context, api tenant.TenantAPI, fsys afero.Fs) error {
version, err := api.GetGotrueVersion(ctx)
if err != nil {
return err
}
return utils.WriteFile(utils.GotrueVersionPath, []byte(version), fsys)
}

func linkStorageVersion(ctx context.Context, api *fetcher.Fetcher, fsys afero.Fs) error {
version, err := tenant.GetStorageVersion(ctx, api)
func linkStorageVersion(ctx context.Context, api tenant.TenantAPI, fsys afero.Fs) error {
version, err := api.GetStorageVersion(ctx)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ func GetRemoteImages(ctx context.Context, projectRef string) map[string]string {
wg.Add(3)
go func() {
defer wg.Done()
if version, err := tenant.GetGotrueVersion(ctx, api); err == nil {
if version, err := api.GetGotrueVersion(ctx); err == nil {
linked[utils.Config.Auth.Image] = version
}
}()
go func() {
defer wg.Done()
if version, err := tenant.GetPostgrestVersion(ctx, api); err == nil {
if version, err := api.GetPostgrestVersion(ctx); err == nil {
linked[utils.Config.Api.Image] = version
}
}()
go func() {
defer wg.Done()
if version, err := tenant.GetStorageVersion(ctx, api); err == nil {
if version, err := api.GetStorageVersion(ctx); err == nil {
linked[utils.Config.Storage.Image] = version
}
}()
Expand Down
10 changes: 7 additions & 3 deletions internal/utils/tenant/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ func GetApiKeys(ctx context.Context, projectRef string) (ApiKey, error) {
return keys, nil
}

func NewTenantAPI(ctx context.Context, projectRef, anonKey string) *fetcher.Fetcher {
type TenantAPI struct {
*fetcher.Fetcher
}

func NewTenantAPI(ctx context.Context, projectRef, anonKey string) TenantAPI {
server := "https://" + utils.GetSupabaseHost(projectRef)
client := &http.Client{
Timeout: 10 * time.Second,
}
header := func(req *http.Request) {
req.Header.Add("apikey", anonKey)
}
api := fetcher.NewFetcher(
api := TenantAPI{Fetcher: fetcher.NewFetcher(
server,
fetcher.WithHTTPClient(client),
fetcher.WithRequestEditor(header),
fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
)
)}
return api
}
4 changes: 2 additions & 2 deletions internal/utils/tenant/gotrue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type HealthResponse struct {
Description string `json:"description"`
}

func GetGotrueVersion(ctx context.Context, api *fetcher.Fetcher) (string, error) {
resp, err := api.Send(ctx, http.MethodGet, "/auth/v1/health", nil)
func (t *TenantAPI) GetGotrueVersion(ctx context.Context) (string, error) {
resp, err := t.Send(ctx, http.MethodGet, "/auth/v1/health", nil)
if err != nil {
return "", err
}
Expand Down
10 changes: 6 additions & 4 deletions internal/utils/tenant/gotrue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"gopkg.in/h2non/gock.v1"
)

var mockApi = fetcher.NewFetcher("http://127.0.0.1")
var mockApi = TenantAPI{Fetcher: fetcher.NewFetcher(
"http://127.0.0.1",
)}

func TestGotrueVersion(t *testing.T) {
t.Run("gets gotrue version", func(t *testing.T) {
Expand All @@ -22,7 +24,7 @@ func TestGotrueVersion(t *testing.T) {
Reply(http.StatusOK).
JSON(HealthResponse{Version: "v2.92.1"})
// Run test
version, err := GetGotrueVersion(context.Background(), mockApi)
version, err := mockApi.GetGotrueVersion(context.Background())
// Check error
assert.NoError(t, err)
assert.Equal(t, "v2.92.1", version)
Expand All @@ -35,7 +37,7 @@ func TestGotrueVersion(t *testing.T) {
Get("/auth/v1/health").
ReplyError(errors.New("network error"))
// Run test
version, err := GetGotrueVersion(context.Background(), mockApi)
version, err := mockApi.GetGotrueVersion(context.Background())
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, version)
Expand All @@ -49,7 +51,7 @@ func TestGotrueVersion(t *testing.T) {
Reply(http.StatusOK).
JSON(HealthResponse{})
// Run test
version, err := GetGotrueVersion(context.Background(), mockApi)
version, err := mockApi.GetGotrueVersion(context.Background())
// Check error
assert.ErrorIs(t, err, errGotrueVersion)
assert.Empty(t, version)
Expand Down
4 changes: 2 additions & 2 deletions internal/utils/tenant/postgrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type SwaggerResponse struct {
Info SwaggerInfo `json:"info"`
}

func GetPostgrestVersion(ctx context.Context, api *fetcher.Fetcher) (string, error) {
resp, err := api.Send(ctx, http.MethodGet, "/rest/v1/", nil)
func (t *TenantAPI) GetPostgrestVersion(ctx context.Context) (string, error) {
resp, err := t.Send(ctx, http.MethodGet, "/rest/v1/", nil)
if err != nil {
return "", err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/utils/tenant/postgrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestPostgrestVersion(t *testing.T) {
Reply(http.StatusOK).
JSON(SwaggerResponse{Info: SwaggerInfo{Version: "11.1.0"}})
// Run test
version, err := GetPostgrestVersion(context.Background(), mockApi)
version, err := mockApi.GetPostgrestVersion(context.Background())
// Check error
assert.NoError(t, err)
assert.Equal(t, "v11.1.0", version)
Expand All @@ -33,7 +33,7 @@ func TestPostgrestVersion(t *testing.T) {
Reply(http.StatusOK).
JSON(SwaggerResponse{Info: SwaggerInfo{Version: "11.2.0 (c820efb)"}})
// Run test
version, err := GetPostgrestVersion(context.Background(), mockApi)
version, err := mockApi.GetPostgrestVersion(context.Background())
// Check error
assert.NoError(t, err)
assert.Equal(t, "v11.2.0", version)
Expand All @@ -46,7 +46,7 @@ func TestPostgrestVersion(t *testing.T) {
Get("/rest/v1/").
ReplyError(errors.New("network error"))
// Run test
version, err := GetPostgrestVersion(context.Background(), mockApi)
version, err := mockApi.GetPostgrestVersion(context.Background())
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, version)
Expand All @@ -60,7 +60,7 @@ func TestPostgrestVersion(t *testing.T) {
Reply(http.StatusOK).
JSON(SwaggerResponse{})
// Run test
version, err := GetPostgrestVersion(context.Background(), mockApi)
version, err := mockApi.GetPostgrestVersion(context.Background())
// Check error
assert.ErrorIs(t, err, errPostgrestVersion)
assert.Empty(t, version)
Expand Down
5 changes: 2 additions & 3 deletions internal/utils/tenant/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"net/http"

"github.com/go-errors/errors"
"github.com/supabase/cli/pkg/fetcher"
)

var errStorageVersion = errors.New("Storage version not found.")

func GetStorageVersion(ctx context.Context, api *fetcher.Fetcher) (string, error) {
resp, err := api.Send(ctx, http.MethodGet, "/storage/v1/version", nil)
func (t *TenantAPI) GetStorageVersion(ctx context.Context) (string, error) {
resp, err := t.Send(ctx, http.MethodGet, "/storage/v1/version", nil)
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/utils/tenant/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestStorageVersion(t *testing.T) {
Reply(http.StatusOK).
BodyString("0.40.4")
// Run test
version, err := GetStorageVersion(context.Background(), mockApi)
version, err := mockApi.GetStorageVersion(context.Background())
// Check error
assert.NoError(t, err)
assert.Equal(t, "v0.40.4", version)
Expand All @@ -32,7 +32,7 @@ func TestStorageVersion(t *testing.T) {
Get("/storage/v1/version").
ReplyError(errors.New("network error"))
// Run test
version, err := GetStorageVersion(context.Background(), mockApi)
version, err := mockApi.GetStorageVersion(context.Background())
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, version)
Expand All @@ -46,7 +46,7 @@ func TestStorageVersion(t *testing.T) {
Reply(http.StatusOK).
BodyString("0.0.0")
// Run test
version, err := GetStorageVersion(context.Background(), mockApi)
version, err := mockApi.GetStorageVersion(context.Background())
// Check error
assert.ErrorIs(t, err, errStorageVersion)
assert.Empty(t, version)
Expand Down

0 comments on commit 064c806

Please sign in to comment.