From 717bf7ebfe8e75d3d418aae5edb48c3d8bbe1de9 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Tue, 6 Apr 2021 09:13:50 +0300 Subject: [PATCH] go: bump planetscale-go to v0.15.0 --- go.mod | 2 +- go.sum | 4 +- internal/cmdutil/terminal.go | 9 +- .../planetscale-go/planetscale/backups.go | 39 +----- .../planetscale-go/planetscale/branches.go | 49 +------ .../planetscale-go/planetscale/certs.go | 9 +- .../planetscale-go/planetscale/client.go | 128 +++++++++++++++--- .../planetscale-go/planetscale/databases.go | 37 +---- .../planetscale/deploy_requests.go | 79 ++--------- .../planetscale/organizations.go | 25 +--- .../planetscale/schema_snapshots.go | 42 +----- .../planetscale/service_tokens.go | 58 ++------ vendor/modules.txt | 2 +- 13 files changed, 165 insertions(+), 318 deletions(-) diff --git a/go.mod b/go.mod index cf89259a..d2a65fa2 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/browser v0.0.0-20201112035734-206646e67786 github.com/pkg/errors v0.9.1 - github.com/planetscale/planetscale-go v0.14.0 + github.com/planetscale/planetscale-go v0.15.0 github.com/planetscale/sql-proxy v0.2.0 github.com/spf13/cobra v1.1.3 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 635b774a..00b8b2c4 100644 --- a/go.sum +++ b/go.sum @@ -246,8 +246,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/planetscale/planetscale-go v0.9.0/go.mod h1:R+07GStW2oGSfeTwWoplnimSEFzxIzEofOHgPFxArg0= -github.com/planetscale/planetscale-go v0.14.0 h1:vlR5okb353ne253SvSpuRu5xaMhpy01jjAYZvPG5iMU= -github.com/planetscale/planetscale-go v0.14.0/go.mod h1:R+07GStW2oGSfeTwWoplnimSEFzxIzEofOHgPFxArg0= +github.com/planetscale/planetscale-go v0.15.0 h1:lmC0xkgCQZAf5AuLaUs51FEoQjKx1q4eGvXshbQ119s= +github.com/planetscale/planetscale-go v0.15.0/go.mod h1:R+07GStW2oGSfeTwWoplnimSEFzxIzEofOHgPFxArg0= github.com/planetscale/sql-proxy v0.2.0 h1:E6BDEYSP+t68kTxnyCNEMowJfiyGcJUTYplY4Q+VMag= github.com/planetscale/sql-proxy v0.2.0/go.mod h1:ZEBm1sist+VFnuxx0lWcMSKq/NfIWRqn2uDIfCERcI4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/internal/cmdutil/terminal.go b/internal/cmdutil/terminal.go index 00d3b7b4..cbe72fe2 100644 --- a/internal/cmdutil/terminal.go +++ b/internal/cmdutil/terminal.go @@ -50,10 +50,11 @@ func Bold(msg string) string { return color.New(color.Bold).Sprint(msg) } -func IsNotFoundError(ogErr error) bool { - err, ok := ogErr.(*planetscale.ErrorResponse) - if ok { - return err.Code == "not_found" +func IsNotFoundError(err error) bool { + if pErr, ok := err.(*planetscale.Error); ok { + if pErr.Code == planetscale.ErrNotFound { + return true + } } return false diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/backups.go b/vendor/github.com/planetscale/planetscale-go/planetscale/backups.go index d56c906a..1d933de2 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/backups.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/backups.go @@ -2,7 +2,6 @@ package planetscale import ( "context" - "encoding/json" "fmt" "net/http" "time" @@ -79,16 +78,9 @@ func (d *backupsService) Create(ctx context.Context, createReq *CreateBackupRequ if err != nil { return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() backup := &Backup{} - err = json.NewDecoder(res.Body).Decode(&backup) - - if err != nil { + if err := d.client.do(ctx, req, &backup); err != nil { return nil, err } @@ -103,16 +95,8 @@ func (d *backupsService) Get(ctx context.Context, getReq *GetBackupRequest) (*Ba return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - backup := &Backup{} - err = json.NewDecoder(res.Body).Decode(&backup) - - if err != nil { + if err := d.client.do(ctx, req, &backup); err != nil { return nil, err } @@ -126,16 +110,8 @@ func (d *backupsService) List(ctx context.Context, listReq *ListBackupsRequest) return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - backups := &backupsResponse{} - err = json.NewDecoder(res.Body).Decode(&backups) - - if err != nil { + if err := d.client.do(ctx, req, &backups); err != nil { return nil, err } @@ -150,13 +126,8 @@ func (d *backupsService) Delete(ctx context.Context, deleteReq *DeleteBackupRequ return errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return err - } - defer res.Body.Close() - - return nil + err = d.client.do(ctx, req, nil) + return err } func backupsAPIPath(org, db, branch string) string { diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/branches.go b/vendor/github.com/planetscale/planetscale-go/planetscale/branches.go index 3070b24a..cf7b01be 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/branches.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/branches.go @@ -2,7 +2,6 @@ package planetscale import ( "context" - "encoding/json" "fmt" "net/http" "time" @@ -106,16 +105,9 @@ func (d *databaseBranchesService) Create(ctx context.Context, createReq *CreateD if err != nil { return nil, errors.Wrap(err, "error creating request for branch database") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() dbBranch := &DatabaseBranch{} - err = json.NewDecoder(res.Body).Decode(&dbBranch) - - if err != nil { + if err := d.client.do(ctx, req, &dbBranch); err != nil { return nil, err } @@ -130,16 +122,8 @@ func (d *databaseBranchesService) Get(ctx context.Context, getReq *GetDatabaseBr return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dbBranch := &DatabaseBranch{} - err = json.NewDecoder(res.Body).Decode(&dbBranch) - - if err != nil { + if err := d.client.do(ctx, req, &dbBranch); err != nil { return nil, err } @@ -154,16 +138,8 @@ func (d *databaseBranchesService) List(ctx context.Context, listReq *ListDatabas return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dbBranches := &databaseBranchesResponse{} - err = json.NewDecoder(res.Body).Decode(&dbBranches) - - if err != nil { + if err := d.client.do(ctx, req, &dbBranches); err != nil { return nil, err } @@ -178,13 +154,8 @@ func (d *databaseBranchesService) Delete(ctx context.Context, deleteReq *DeleteD return errors.Wrap(err, "error creating request for delete branch") } - res, err := d.client.Do(ctx, req) - if err != nil { - return err - } - defer res.Body.Close() - - return nil + err = d.client.do(ctx, req, nil) + return err } // Status returns the status of a specific database branch @@ -195,16 +166,8 @@ func (d *databaseBranchesService) GetStatus(ctx context.Context, statusReq *GetD return nil, errors.Wrap(err, "error creating request for branch status") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - status := &DatabaseBranchStatus{} - err = json.NewDecoder(res.Body).Decode(&status) - - if err != nil { + if err := d.client.do(ctx, req, &status); err != nil { return nil, err } diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/certs.go b/vendor/github.com/planetscale/planetscale-go/planetscale/certs.go index 049ba05b..fb63b41f 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/certs.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/certs.go @@ -8,7 +8,6 @@ import ( "crypto/tls" "crypto/x509" "crypto/x509/pkix" - "encoding/json" "encoding/pem" "errors" "fmt" @@ -88,19 +87,13 @@ func (c *certificatesService) Create(ctx context.Context, r *CreateCertificateRe return nil, fmt.Errorf("error creating request for create certificates: %s", err) } - res, err := c.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - var cr struct { Certificate string `json:"certificate"` CertificateChain string `json:"certificate_chain"` RemoteAddr string `json:"remote_addr"` } - err = json.NewDecoder(res.Body).Decode(&cr) + err = c.client.do(ctx, req, &cr) if err != nil { return nil, err } diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/client.go b/vendor/github.com/planetscale/planetscale-go/planetscale/client.go index 769ed4ed..86e98ec0 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/client.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/client.go @@ -18,6 +18,18 @@ const ( jsonMediaType = "application/json" ) +// ErrorCode defines the code of an error. +type ErrorCode string + +const ( + ErrInternal ErrorCode = "internal" // Internal error. + ErrInvalid ErrorCode = "invalid" // Invalid operation, e.g wrong params + ErrPermission ErrorCode = "permission" // Permission denied. + ErrNotFound ErrorCode = "not_found" // Resource not found. + ErrRetry ErrorCode = "retry" // Operation should be retried. + ErrResponseMalformed ErrorCode = "response_malformed" // Response body is malformed. +) + // Client encapsulates a client that talks to the PlanetScale API type Client struct { // client represents the HTTP client used for making HTTP requests. @@ -131,25 +143,52 @@ func NewClient(opts ...ClientOption) (*Client, error) { return c, nil } -// Do sends an HTTP request and returns an HTTP response with the configured -// HTTP client. -func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error) { +// do makes an HTTP request and populates the given struct v from the response. +func (c *Client) do(ctx context.Context, req *http.Request, v interface{}) error { req = req.WithContext(ctx) res, err := c.client.Do(req) if err != nil { - return nil, err + return err + } + defer res.Body.Close() + + return c.handleResponse(ctx, res, v) +} + +// handleResponse makes an HTTP request and populates the given struct v from +// the response. This is meant for internal testing and shouldn't be used +// directly. Instead please use `Client.do`. +func (c *Client) handleResponse(ctx context.Context, res *http.Response, v interface{}) error { + out, err := ioutil.ReadAll(res.Body) + if err != nil { + return err } if res.StatusCode >= 400 { out, err := ioutil.ReadAll(res.Body) if err != nil { - return nil, err + return err } - errorRes := &ErrorResponse{} + // errorResponse represents an error response from the API + type errorResponse struct { + Code string `json:"code"` + Message string `json:"message"` + } + + errorRes := &errorResponse{} err = json.Unmarshal(out, errorRes) if err != nil { - return nil, err + if _, ok := err.(*json.SyntaxError); ok { + return &Error{ + msg: "malformed response body received", + Code: ErrResponseMalformed, + Meta: map[string]string{ + "body": string(out), + }, + } + } + return err } // json.Unmarshal doesn't return an error if the response @@ -158,14 +197,54 @@ func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, err // not, we return the full response back to the user, so // they can debug the issue. // TODO(fatih): fix the behavior on the API side - if *errorRes == (ErrorResponse{}) { - return nil, errors.New(string(out)) + if *errorRes == (errorResponse{}) { + return &Error{ + msg: "internal error, please open an issue to github.com/planetscale/planetscale-go", + Code: ErrInternal, + Meta: map[string]string{ + "body": string(out), + }, + } + } + + var errCode ErrorCode + switch errorRes.Code { + case "not_found": + errCode = ErrNotFound + case "unauthorized": + errCode = ErrPermission + case "invalid_params": + errCode = ErrInvalid + case "unprocessable": + errCode = ErrRetry + } + + return &Error{ + msg: errorRes.Message, + Code: errCode, } + } + + // this means we don't care about unmrarshaling the response body into v + if v == nil { + return nil + } - return res, errorRes + err = json.Unmarshal(out, &v) + if err != nil { + if _, ok := err.(*json.SyntaxError); ok { + return &Error{ + msg: "malformed response body received", + Code: ErrResponseMalformed, + Meta: map[string]string{ + "body": string(out), + }, + } + } + return err } - return res, nil + return nil } func (c *Client) newRequest(method string, path string, body interface{}) (*http.Request, error) { @@ -203,16 +282,6 @@ func (c *Client) newRequest(method string, path string, body interface{}) (*http return req, nil } -// ErrorResponse represents an error response from the API -type ErrorResponse struct { - Code string `json:"code"` - Message string `json:"message"` -} - -func (e ErrorResponse) Error() string { - return e.Message -} - type serviceTokenTransport struct { rt http.RoundTripper token string @@ -223,3 +292,20 @@ func (t *serviceTokenTransport) RoundTrip(req *http.Request) (*http.Response, er req.Header.Add("Authorization", t.tokenName+":"+t.token) return t.rt.RoundTrip(req) } + +// Error represents common errors originating from the Client. +type Error struct { + // msg contains the human readable string + msg string + + // Code specifies the error code. i.e; NotFound, RateLimited, etc... + Code ErrorCode + + // Meta contains additional information depending on the error code. As an + // example, if the Code is "ErrResponseMalformed", the map will be: ["body"] + // = "body of the response" + Meta map[string]string +} + +// Error returns the string representation of the error. +func (e *Error) Error() string { return e.msg } diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/databases.go b/vendor/github.com/planetscale/planetscale-go/planetscale/databases.go index 7eca79b2..10abdfb6 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/databases.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/databases.go @@ -2,7 +2,6 @@ package planetscale import ( "context" - "encoding/json" "fmt" "net/http" "time" @@ -76,15 +75,8 @@ func (ds *databasesService) List(ctx context.Context, listReq *ListDatabasesRequ return nil, errors.Wrap(err, "error creating http request") } - res, err := ds.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dbResponse := databasesResponse{} - err = json.NewDecoder(res.Body).Decode(&dbResponse) - + err = ds.client.do(ctx, req, &dbResponse) if err != nil { return nil, err } @@ -98,15 +90,8 @@ func (ds *databasesService) Create(ctx context.Context, createReq *CreateDatabas return nil, errors.Wrap(err, "error creating request for create database") } - res, err := ds.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - db := &Database{} - err = json.NewDecoder(res.Body).Decode(db) - + err = ds.client.do(ctx, req, &db) if err != nil { return nil, err } @@ -121,15 +106,8 @@ func (ds *databasesService) Get(ctx context.Context, getReq *GetDatabaseRequest) return nil, errors.Wrap(err, "error creating request for get database") } - res, err := ds.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - db := &Database{} - err = json.NewDecoder(res.Body).Decode(&db) - + err = ds.client.do(ctx, req, &db) if err != nil { return nil, err } @@ -144,13 +122,8 @@ func (ds *databasesService) Delete(ctx context.Context, deleteReq *DeleteDatabas return errors.Wrap(err, "error creating request for delete database") } - res, err := ds.client.Do(ctx, req) - if err != nil { - return err - } - defer res.Body.Close() - - return nil + err = ds.client.do(ctx, req, nil) + return err } func databasesAPIPath(org string) string { diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/deploy_requests.go b/vendor/github.com/planetscale/planetscale-go/planetscale/deploy_requests.go index ec088ea3..af973ad6 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/deploy_requests.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/deploy_requests.go @@ -2,7 +2,6 @@ package planetscale import ( "context" - "encoding/json" "fmt" "net/http" "time" @@ -154,15 +153,8 @@ func (d *deployRequestsService) Get(ctx context.Context, getReq *GetDeployReques return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dr := &DeployRequest{} - err = json.NewDecoder(res.Body).Decode(dr) - if err != nil { + if err := d.client.do(ctx, req, &dr); err != nil { return nil, err } @@ -184,15 +176,8 @@ func (d *deployRequestsService) CloseDeploy(ctx context.Context, closeReq *Close return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dr := &DeployRequest{} - err = json.NewDecoder(res.Body).Decode(dr) - if err != nil { + if err := d.client.do(ctx, req, &dr); err != nil { return nil, err } @@ -207,15 +192,8 @@ func (d *deployRequestsService) Deploy(ctx context.Context, deployReq *PerformDe return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dr := &DeployRequest{} - err = json.NewDecoder(res.Body).Decode(dr) - if err != nil { + if err := d.client.do(ctx, req, &dr); err != nil { return nil, err } @@ -233,18 +211,10 @@ func (d *deployRequestsService) Create(ctx context.Context, createReq *CreateDep return nil, err } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dr := &DeployRequest{} - err = json.NewDecoder(res.Body).Decode(dr) - if err != nil { + if err := d.client.do(ctx, req, &dr); err != nil { return nil, err } - return dr, nil } @@ -256,15 +226,8 @@ func (d *deployRequestsService) CancelDeploy(ctx context.Context, deployReq *Can return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dr := &DeployRequest{} - err = json.NewDecoder(res.Body).Decode(dr) - if err != nil { + if err := d.client.do(ctx, req, &dr); err != nil { return nil, err } @@ -299,15 +262,8 @@ func (d *deployRequestsService) Diff(ctx context.Context, diffReq *DiffRequest) return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - diffs := &diffResponse{} - err = json.NewDecoder(res.Body).Decode(&diffs) - if err != nil { + if err := d.client.do(ctx, req, &diffs); err != nil { return nil, err } @@ -320,20 +276,12 @@ func (d *deployRequestsService) List(ctx context.Context, listReq *ListDeployReq return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { + drReq := &deployRequestsResponse{} + if err := d.client.do(ctx, req, &drReq); err != nil { return nil, err } - defer res.Body.Close() - deployRequests := &deployRequestsResponse{} - err = json.NewDecoder(res.Body).Decode(&deployRequests) - - if err != nil { - return nil, err - } - - return deployRequests.DeployRequests, nil + return drReq.DeployRequests, nil } func (d *deployRequestsService) CreateReview(ctx context.Context, reviewReq *ReviewDeployRequestRequest) (*DeployRequestReview, error) { @@ -356,15 +304,8 @@ func (d *deployRequestsService) CreateReview(ctx context.Context, reviewReq *Rev return nil, errors.Wrap(err, "error creating http request") } - res, err := d.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - drr := &DeployRequestReview{} - err = json.NewDecoder(res.Body).Decode(drr) - if err != nil { + if err := d.client.do(ctx, req, &drr); err != nil { return nil, err } diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/organizations.go b/vendor/github.com/planetscale/planetscale-go/planetscale/organizations.go index ded4ed0e..5d66f1bb 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/organizations.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/organizations.go @@ -2,7 +2,6 @@ package planetscale import ( "context" - "encoding/json" "fmt" "net/http" "time" @@ -55,20 +54,12 @@ func (o *organizationsService) Get(ctx context.Context, getReq *GetOrganizationR return nil, errors.Wrap(err, "error creating request for get organization") } - res, err := o.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - - organization := &Organization{} - err = json.NewDecoder(res.Body).Decode(&organization) - - if err != nil { + org := &Organization{} + if err := o.client.do(ctx, req, &org); err != nil { return nil, err } - return organization, nil + return org, nil } // List returns all the organizations for a user. @@ -78,16 +69,8 @@ func (o *organizationsService) List(ctx context.Context) ([]*Organization, error return nil, errors.Wrap(err, "error creating request for list organization") } - res, err := o.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - orgResponse := &organizationsResponse{} - err = json.NewDecoder(res.Body).Decode(&orgResponse) - - if err != nil { + if err := o.client.do(ctx, req, &orgResponse); err != nil { return nil, err } diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/schema_snapshots.go b/vendor/github.com/planetscale/planetscale-go/planetscale/schema_snapshots.go index 13596111..1a3dc462 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/schema_snapshots.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/schema_snapshots.go @@ -2,7 +2,6 @@ package planetscale import ( "context" - "encoding/json" "fmt" "net/http" "time" @@ -82,17 +81,11 @@ func (s *schemaSnapshotsService) Create(ctx context.Context, createReq *CreateSc return nil, errors.Wrap(err, "error creating http request") } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - ss := &SchemaSnapshot{} - err = json.NewDecoder(res.Body).Decode(ss) - if err != nil { + if err := s.client.do(ctx, req, &ss); err != nil { return nil, err } + return ss, nil } @@ -103,19 +96,12 @@ func (s *schemaSnapshotsService) List(ctx context.Context, listReq *ListSchemaSn return nil, errors.Wrap(err, "error creating http request") } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - - schemaSnapshotsResponse := schemaSnapshotsResponse{} - err = json.NewDecoder(res.Body).Decode(&schemaSnapshotsResponse) - if err != nil { + ssr := schemaSnapshotsResponse{} + if err := s.client.do(ctx, req, &ssr); err != nil { return nil, err } - return schemaSnapshotsResponse.SchemaSnapshots, nil + return ssr.SchemaSnapshots, nil } // Get returns a single schema snapshot. @@ -125,15 +111,8 @@ func (s *schemaSnapshotsService) Get(ctx context.Context, getReq *GetSchemaSnaps return nil, errors.Wrap(err, "error creating http request") } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - ss := &SchemaSnapshot{} - err = json.NewDecoder(res.Body).Decode(ss) - if err != nil { + if err := s.client.do(ctx, req, &ss); err != nil { return nil, err } @@ -147,15 +126,8 @@ func (s *schemaSnapshotsService) RequestDeploy(ctx context.Context, deployReq *S return nil, err } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - dr := &DeployRequest{} - err = json.NewDecoder(res.Body).Decode(dr) - if err != nil { + if err := s.client.do(ctx, req, &dr); err != nil { return nil, err } diff --git a/vendor/github.com/planetscale/planetscale-go/planetscale/service_tokens.go b/vendor/github.com/planetscale/planetscale-go/planetscale/service_tokens.go index 1fdf2b3b..7da31266 100644 --- a/vendor/github.com/planetscale/planetscale-go/planetscale/service_tokens.go +++ b/vendor/github.com/planetscale/planetscale-go/planetscale/service_tokens.go @@ -2,11 +2,12 @@ package planetscale import ( "context" - "encoding/json" "fmt" "net/http" ) +var _ ServiceTokenService = &serviceTokenService{} + // ServiceTokenService is an interface for communicating with the PlanetScale // Service Token API. type ServiceTokenService interface { @@ -28,14 +29,8 @@ func (s *serviceTokenService) Create(ctx context.Context, createReq *CreateServi return nil, err } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - defer res.Body.Close() - st := &ServiceToken{} - if err := json.NewDecoder(res.Body).Decode(st); err != nil { + if err := s.client.do(ctx, req, &st); err != nil { return nil, err } @@ -48,17 +43,11 @@ func (s *serviceTokenService) List(ctx context.Context, listReq *ListServiceToke return nil, err } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - - defer res.Body.Close() - tokenListResponse := serviceTokensResponse{} - if err := json.NewDecoder(res.Body).Decode(&tokenListResponse); err != nil { + if err := s.client.do(ctx, req, &tokenListResponse); err != nil { return nil, err } + return tokenListResponse.ServiceTokens, nil } @@ -68,12 +57,8 @@ func (s *serviceTokenService) Delete(ctx context.Context, delReq *DeleteServiceT return err } - res, err := s.client.Do(ctx, req) - if err != nil { - return err - } - defer res.Body.Close() - return nil + err = s.client.do(ctx, req, nil) + return err } func (s *serviceTokenService) GetAccess(ctx context.Context, accessReq *GetServiceTokenAccessRequest) ([]*ServiceTokenAccess, error) { @@ -82,15 +67,8 @@ func (s *serviceTokenService) GetAccess(ctx context.Context, accessReq *GetServi return nil, err } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - - defer res.Body.Close() - tokenAccess := serviceTokenAccessResponse{} - if err := json.NewDecoder(res.Body).Decode(&tokenAccess); err != nil { + if err := s.client.do(ctx, req, &tokenAccess); err != nil { return nil, err } return tokenAccess.ServiceTokenAccesses, nil @@ -102,15 +80,8 @@ func (s *serviceTokenService) AddAccess(ctx context.Context, addReq *AddServiceT return nil, err } - res, err := s.client.Do(ctx, req) - if err != nil { - return nil, err - } - - defer res.Body.Close() - tokenAccess := serviceTokenAccessResponse{} - if err := json.NewDecoder(res.Body).Decode(&tokenAccess); err != nil { + if err := s.client.do(ctx, req, &tokenAccess); err != nil { return nil, err } return tokenAccess.ServiceTokenAccesses, nil @@ -122,17 +93,10 @@ func (s *serviceTokenService) DeleteAccess(ctx context.Context, delReq *DeleteSe return err } - res, err := s.client.Do(ctx, req) - if err != nil { - return err - } - - defer res.Body.Close() - return nil + err = s.client.do(ctx, req, nil) + return err } -var _ ServiceTokenService = &serviceTokenService{} - type CreateServiceTokenRequest struct { Organization string `json:"-"` } diff --git a/vendor/modules.txt b/vendor/modules.txt index 37aba8b9..2c1fd06c 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -73,7 +73,7 @@ github.com/pkg/browser # github.com/pkg/errors v0.9.1 ## explicit github.com/pkg/errors -# github.com/planetscale/planetscale-go v0.14.0 +# github.com/planetscale/planetscale-go v0.15.0 ## explicit github.com/planetscale/planetscale-go/planetscale # github.com/planetscale/sql-proxy v0.2.0