From 9d9f5f31987f64a56d04bae9f79838af346e78fa Mon Sep 17 00:00:00 2001 From: Vitaliy Saveliev <70613571+milkrage@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:00:31 +0300 Subject: [PATCH] Marshal JSON payload via Gophercloud (#166) - removed RawBody in RequestsService - added JSONBody (now payload will do convert to bytes via gophercloud) - improved test for check content-type in requests with payload --- selvpcclient/clients/services/requests.go | 6 ++--- selvpcclient/quotamanager/quotas/requests.go | 13 ++-------- .../resell/v2/capabilities/requests.go | 1 - .../resell/v2/floatingips/requests.go | 15 ++--------- selvpcclient/resell/v2/keypairs/requests.go | 12 ++------- selvpcclient/resell/v2/licenses/requests.go | 15 ++--------- selvpcclient/resell/v2/projects/requests.go | 25 +++++-------------- selvpcclient/resell/v2/roles/requests.go | 17 ++----------- selvpcclient/resell/v2/subnets/requests.go | 15 ++--------- selvpcclient/resell/v2/tokens/requests.go | 13 +++------- selvpcclient/resell/v2/traffic/requests.go | 1 - selvpcclient/resell/v2/users/requests.go | 25 +++++-------------- .../resell/v2/vrrpsubnets/requests.go | 15 ++--------- selvpcclient/testutils/handlers.go | 19 ++++++++++++++ 14 files changed, 51 insertions(+), 141 deletions(-) diff --git a/selvpcclient/clients/services/requests.go b/selvpcclient/clients/services/requests.go index 6e4e254..596444a 100644 --- a/selvpcclient/clients/services/requests.go +++ b/selvpcclient/clients/services/requests.go @@ -14,8 +14,8 @@ type RequestService struct { } type RequestOptions struct { - Body io.Reader - OkCodes []int + JSONBody interface{} + OkCodes []int } func NewRequestService(serviceClient *gophercloud.ServiceClient) *RequestService { @@ -25,7 +25,7 @@ func NewRequestService(serviceClient *gophercloud.ServiceClient) *RequestService func (s *RequestService) Do(method, url string, options *RequestOptions) (*ResponseResult, error) { requestOpts := gophercloud.RequestOpts{ OkCodes: options.OkCodes, - RawBody: options.Body, + JSONBody: options.JSONBody, KeepResponseBody: true, } diff --git a/selvpcclient/quotamanager/quotas/requests.go b/selvpcclient/quotamanager/quotas/requests.go index 2cfbaf5..0fd23c1 100644 --- a/selvpcclient/quotamanager/quotas/requests.go +++ b/selvpcclient/quotamanager/quotas/requests.go @@ -1,8 +1,6 @@ package quotas import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -24,7 +22,6 @@ func GetLimits(client *selvpcclient.Client, projectID, region string, url := strings.Join([]string{endpoint, resourcePrefix, projectID, "limits"}, "/") responseResult, err := client.QuotaManager.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -56,7 +53,6 @@ func GetProjectQuotas(client *selvpcclient.Client, projectID, region string, url := strings.Join([]string{endpoint, resourcePrefix, projectID, "quotas"}, "/") responseResult, err := client.QuotaManager.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -81,11 +77,6 @@ func GetProjectQuotas(client *selvpcclient.Client, projectID, region string, func UpdateProjectQuotas(client *selvpcclient.Client, projectID, region string, updateOpts UpdateProjectQuotasOpts, ) ([]*Quota, *clientservices.ResponseResult, error) { - requestBody, err := json.Marshal(&updateOpts) - if err != nil { - return nil, nil, err - } - endpoint, err := client.QuotaManager.GetEndpoint(region) if err != nil { return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err) @@ -94,8 +85,8 @@ func UpdateProjectQuotas(client *selvpcclient.Client, projectID, region string, url := strings.Join([]string{endpoint, resourcePrefix, projectID, "quotas"}, "/") responseResult, err := client.QuotaManager.Requests.Do(http.MethodPatch, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &updateOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err diff --git a/selvpcclient/resell/v2/capabilities/requests.go b/selvpcclient/resell/v2/capabilities/requests.go index bb20583..d7fd13d 100644 --- a/selvpcclient/resell/v2/capabilities/requests.go +++ b/selvpcclient/resell/v2/capabilities/requests.go @@ -20,7 +20,6 @@ func Get(client *selvpcclient.Client) (*Capabilities, *clientservices.ResponseRe url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { diff --git a/selvpcclient/resell/v2/floatingips/requests.go b/selvpcclient/resell/v2/floatingips/requests.go index 66103a0..226064a 100644 --- a/selvpcclient/resell/v2/floatingips/requests.go +++ b/selvpcclient/resell/v2/floatingips/requests.go @@ -1,8 +1,6 @@ package floatingips import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -24,7 +22,6 @@ func Get(client *selvpcclient.Client, id string) (*FloatingIP, *clientservices.R url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -63,7 +60,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*FloatingIP, *clientser url = strings.Join([]string{url, queryParams.Encode()}, "?") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -87,12 +83,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*FloatingIP, *clientser // Create requests a creation of the floating ip in the specified project. func Create(client *selvpcclient.Client, projectID string, createOpts FloatingIPOpts) ([]*FloatingIP, *clientservices.ResponseResult, error) { - createFloatingIPOpts := &createOpts - requestBody, err := json.Marshal(createFloatingIPOpts) - if err != nil { - return nil, nil, err - } - endpoint, err := client.Resell.GetEndpoint() if err != nil { return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err) @@ -100,8 +90,8 @@ func Create(client *selvpcclient.Client, projectID string, createOpts FloatingIP url := strings.Join([]string{endpoint, resourceURL, "projects", projectID}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -131,7 +121,6 @@ func Delete(client *selvpcclient.Client, id string) (*clientservices.ResponseRes url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/keypairs/requests.go b/selvpcclient/resell/v2/keypairs/requests.go index 991d476..e869a8b 100644 --- a/selvpcclient/resell/v2/keypairs/requests.go +++ b/selvpcclient/resell/v2/keypairs/requests.go @@ -1,8 +1,6 @@ package keypairs import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -22,7 +20,6 @@ func List(client *selvpcclient.Client) ([]*Keypair, *clientservices.ResponseResu url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -53,10 +50,6 @@ func Create(client *selvpcclient.Client, createOpts KeypairOpts) ([]*Keypair, *c createKeypairOpts := nestedCreateOpts{ Keypair: createOpts, } - requestBody, err := json.Marshal(&createKeypairOpts) - if err != nil { - return nil, nil, err - } endpoint, err := client.Resell.GetEndpoint() if err != nil { @@ -65,8 +58,8 @@ func Create(client *selvpcclient.Client, createOpts KeypairOpts) ([]*Keypair, *c url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createKeypairOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -96,7 +89,6 @@ func Delete(client *selvpcclient.Client, name, userID string) (*clientservices.R url := strings.Join([]string{endpoint, resourceURL, name, "users", userID}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/licenses/requests.go b/selvpcclient/resell/v2/licenses/requests.go index c719a77..160549a 100644 --- a/selvpcclient/resell/v2/licenses/requests.go +++ b/selvpcclient/resell/v2/licenses/requests.go @@ -1,8 +1,6 @@ package licenses import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -24,7 +22,6 @@ func Get(client *selvpcclient.Client, id string) (*License, *clientservices.Resp url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -63,7 +60,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*License, *clientservic url = strings.Join([]string{url, queryParams.Encode()}, "?") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -87,12 +83,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*License, *clientservic // Create requests a creation of the licenses in the specified project. func Create(client *selvpcclient.Client, projectID string, createOpts LicenseOpts) ([]*License, *clientservices.ResponseResult, error) { - createLicenseOpts := &createOpts - requestBody, err := json.Marshal(createLicenseOpts) - if err != nil { - return nil, nil, err - } - endpoint, err := client.Resell.GetEndpoint() if err != nil { return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err) @@ -100,8 +90,8 @@ func Create(client *selvpcclient.Client, projectID string, createOpts LicenseOpt url := strings.Join([]string{endpoint, resourceURL, "projects", projectID}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -131,7 +121,6 @@ func Delete(client *selvpcclient.Client, id string) (*clientservices.ResponseRes url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/projects/requests.go b/selvpcclient/resell/v2/projects/requests.go index 0d940dc..ad116f0 100644 --- a/selvpcclient/resell/v2/projects/requests.go +++ b/selvpcclient/resell/v2/projects/requests.go @@ -1,8 +1,6 @@ package projects import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -22,7 +20,6 @@ func Get(client *selvpcclient.Client, id string) (*Project, *clientservices.Resp url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -53,7 +50,6 @@ func List(client *selvpcclient.Client) ([]*Project, *clientservices.ResponseResu url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -81,11 +77,7 @@ func Create(client *selvpcclient.Client, createOpts CreateOpts) (*Project, *clie type createProject struct { Options CreateOpts `json:"project"` } - createProjectOpts := &createProject{Options: createOpts} - requestBody, err := json.Marshal(createProjectOpts) - if err != nil { - return nil, nil, err - } + createProjectOpts := createProject{Options: createOpts} endpoint, err := client.Resell.GetEndpoint() if err != nil { @@ -94,8 +86,8 @@ func Create(client *selvpcclient.Client, createOpts CreateOpts) (*Project, *clie url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createProjectOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -122,11 +114,7 @@ func Update(client *selvpcclient.Client, id string, updateOpts UpdateOpts) (*Pro type updateProject struct { Options UpdateOpts `json:"project"` } - updateProjectOpts := &updateProject{Options: updateOpts} - requestBody, err := json.Marshal(updateProjectOpts) - if err != nil { - return nil, nil, err - } + updateProjectOpts := updateProject{Options: updateOpts} endpoint, err := client.Resell.GetEndpoint() if err != nil { @@ -135,8 +123,8 @@ func Update(client *selvpcclient.Client, id string, updateOpts UpdateOpts) (*Pro url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPatch, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &updateProjectOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -166,7 +154,6 @@ func Delete(client *selvpcclient.Client, id string) (*clientservices.ResponseRes url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/roles/requests.go b/selvpcclient/resell/v2/roles/requests.go index a438a12..0eb4d46 100644 --- a/selvpcclient/resell/v2/roles/requests.go +++ b/selvpcclient/resell/v2/roles/requests.go @@ -1,8 +1,6 @@ package roles import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -22,7 +20,6 @@ func List(client *selvpcclient.Client) ([]*Role, *clientservices.ResponseResult, url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -53,7 +50,6 @@ func ListProject(client *selvpcclient.Client, id string) ([]*Role, *clientservic url := strings.Join([]string{endpoint, resourceURL, "projects", id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -84,7 +80,6 @@ func ListUser(client *selvpcclient.Client, id string) ([]*Role, *clientservices. url := strings.Join([]string{endpoint, resourceURL, "users", id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -115,7 +110,6 @@ func Create(client *selvpcclient.Client, createOpts RoleOpt) (*Role, *clientserv url := strings.Join([]string{endpoint, resourceURL, "projects", createOpts.ProjectID, "users", createOpts.UserID}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -139,12 +133,6 @@ func Create(client *selvpcclient.Client, createOpts RoleOpt) (*Role, *clientserv // CreateBulk requests a creation of several roles. func CreateBulk(client *selvpcclient.Client, createOpts RoleOpts) ([]*Role, *clientservices.ResponseResult, error) { - createRolesOpts := &createOpts - requestBody, err := json.Marshal(createRolesOpts) - if err != nil { - return nil, nil, err - } - endpoint, err := client.Resell.GetEndpoint() if err != nil { return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err) @@ -152,8 +140,8 @@ func CreateBulk(client *selvpcclient.Client, createOpts RoleOpts) ([]*Role, *cli url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200, 208}, + JSONBody: &createOpts, + OkCodes: []int{200, 208}, }) if err != nil { return nil, nil, err @@ -183,7 +171,6 @@ func Delete(client *selvpcclient.Client, deleteOpts RoleOpt) (*clientservices.Re url := strings.Join([]string{endpoint, resourceURL, "projects", deleteOpts.ProjectID, "users", deleteOpts.UserID}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/subnets/requests.go b/selvpcclient/resell/v2/subnets/requests.go index 3b16653..7265d0a 100644 --- a/selvpcclient/resell/v2/subnets/requests.go +++ b/selvpcclient/resell/v2/subnets/requests.go @@ -1,8 +1,6 @@ package subnets import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -24,7 +22,6 @@ func Get(client *selvpcclient.Client, id string) (*Subnet, *clientservices.Respo url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -63,7 +60,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*Subnet, *clientservice url = strings.Join([]string{url, queryParams.Encode()}, "?") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -87,12 +83,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*Subnet, *clientservice // Create requests a creation of the subnets in the specified project. func Create(client *selvpcclient.Client, projectID string, createOpts SubnetOpts) ([]*Subnet, *clientservices.ResponseResult, error) { - createSubnetsOpts := &createOpts - requestBody, err := json.Marshal(createSubnetsOpts) - if err != nil { - return nil, nil, err - } - endpoint, err := client.Resell.GetEndpoint() if err != nil { return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err) @@ -100,8 +90,8 @@ func Create(client *selvpcclient.Client, projectID string, createOpts SubnetOpts url := strings.Join([]string{endpoint, resourceURL, "projects", projectID}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -131,7 +121,6 @@ func Delete(client *selvpcclient.Client, id string) (*clientservices.ResponseRes url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/tokens/requests.go b/selvpcclient/resell/v2/tokens/requests.go index cd528ec..1169879 100644 --- a/selvpcclient/resell/v2/tokens/requests.go +++ b/selvpcclient/resell/v2/tokens/requests.go @@ -1,8 +1,6 @@ package tokens import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -19,11 +17,7 @@ func Create(client *selvpcclient.Client, createOpts TokenOpts) (*Token, *clients type createToken struct { Options TokenOpts `json:"token"` } - createTokenOpts := &createToken{Options: createOpts} - requestBody, err := json.Marshal(createTokenOpts) - if err != nil { - return nil, nil, err - } + createTokenOpts := createToken{Options: createOpts} endpoint, err := client.Resell.GetEndpoint() if err != nil { @@ -32,8 +26,8 @@ func Create(client *selvpcclient.Client, createOpts TokenOpts) (*Token, *clients url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createTokenOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -63,7 +57,6 @@ func Delete(client *selvpcclient.Client, id string) (*clientservices.ResponseRes url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/traffic/requests.go b/selvpcclient/resell/v2/traffic/requests.go index accb28e..5b1047f 100644 --- a/selvpcclient/resell/v2/traffic/requests.go +++ b/selvpcclient/resell/v2/traffic/requests.go @@ -20,7 +20,6 @@ func Get(client *selvpcclient.Client) (*DomainTraffic, *clientservices.ResponseR url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { diff --git a/selvpcclient/resell/v2/users/requests.go b/selvpcclient/resell/v2/users/requests.go index 2062b76..fda0a4a 100644 --- a/selvpcclient/resell/v2/users/requests.go +++ b/selvpcclient/resell/v2/users/requests.go @@ -1,8 +1,6 @@ package users import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -22,7 +20,6 @@ func Get(client *selvpcclient.Client, id string) (*User, *clientservices.Respons url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -53,7 +50,6 @@ func List(client *selvpcclient.Client) ([]*User, *clientservices.ResponseResult, url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -81,11 +77,7 @@ func Create(client *selvpcclient.Client, createOpts UserOpts) (*User, *clientser type createUser struct { Options UserOpts `json:"user"` } - createUserOpts := &createUser{Options: createOpts} - requestBody, err := json.Marshal(createUserOpts) - if err != nil { - return nil, nil, err - } + createUserOpts := createUser{Options: createOpts} endpoint, err := client.Resell.GetEndpoint() if err != nil { @@ -94,8 +86,8 @@ func Create(client *selvpcclient.Client, createOpts UserOpts) (*User, *clientser url := strings.Join([]string{endpoint, resourceURL}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createUserOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -122,11 +114,7 @@ func Update(client *selvpcclient.Client, id string, updateOpts UserOpts) (*User, type updateUser struct { Options UserOpts `json:"user"` } - updateUserOpts := &updateUser{Options: updateOpts} - requestBody, err := json.Marshal(updateUserOpts) - if err != nil { - return nil, nil, err - } + updateUserOpts := updateUser{Options: updateOpts} endpoint, err := client.Resell.GetEndpoint() if err != nil { @@ -135,8 +123,8 @@ func Update(client *selvpcclient.Client, id string, updateOpts UserOpts) (*User, url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPatch, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &updateUserOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -166,7 +154,6 @@ func Delete(client *selvpcclient.Client, id string) (*clientservices.ResponseRes url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/resell/v2/vrrpsubnets/requests.go b/selvpcclient/resell/v2/vrrpsubnets/requests.go index 065caa0..6a3238c 100644 --- a/selvpcclient/resell/v2/vrrpsubnets/requests.go +++ b/selvpcclient/resell/v2/vrrpsubnets/requests.go @@ -1,8 +1,6 @@ package vrrpsubnets import ( - "bytes" - "encoding/json" "fmt" "net/http" "strings" @@ -24,7 +22,6 @@ func Get(client *selvpcclient.Client, id string) (*VRRPSubnet, *clientservices.R url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -63,7 +60,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*VRRPSubnet, *clientser url = strings.Join([]string{url, queryParams.Encode()}, "?") responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{200}, }) if err != nil { @@ -87,12 +83,6 @@ func List(client *selvpcclient.Client, opts ListOpts) ([]*VRRPSubnet, *clientser // Create requests a creation of the VRRP subnets in the specified project. func Create(client *selvpcclient.Client, projectID string, createOpts VRRPSubnetOpts) ([]*VRRPSubnet, *clientservices.ResponseResult, error) { - createVRRPSubnetsOpts := &createOpts - requestBody, err := json.Marshal(createVRRPSubnetsOpts) - if err != nil { - return nil, nil, err - } - endpoint, err := client.Resell.GetEndpoint() if err != nil { return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err) @@ -100,8 +90,8 @@ func Create(client *selvpcclient.Client, projectID string, createOpts VRRPSubnet url := strings.Join([]string{endpoint, resourceURL, "projects", projectID}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodPost, url, &clientservices.RequestOptions{ - Body: bytes.NewReader(requestBody), - OkCodes: []int{200}, + JSONBody: &createOpts, + OkCodes: []int{200}, }) if err != nil { return nil, nil, err @@ -131,7 +121,6 @@ func Delete(client *selvpcclient.Client, id string) (*clientservices.ResponseRes url := strings.Join([]string{endpoint, resourceURL, id}, "/") responseResult, err := client.Resell.Requests.Do(http.MethodDelete, url, &clientservices.RequestOptions{ - Body: nil, OkCodes: []int{204}, }) if err != nil { diff --git a/selvpcclient/testutils/handlers.go b/selvpcclient/testutils/handlers.go index 6c07508..9625f4d 100644 --- a/selvpcclient/testutils/handlers.go +++ b/selvpcclient/testutils/handlers.go @@ -58,6 +58,25 @@ func HandleReqWithBody(t *testing.T, opts *HandleReqOpts) { t.Fatalf("expected %s method but got %s", opts.Method, r.Method) } + _, contentTypeExists := r.Header["Content-Type"] + if !contentTypeExists { + t.Fatalf("request doesn't contain content-type in headers") + } + + contentTypeFound := false + + for _, contentTypeValue := range r.Header["Content-Type"] { + if contentTypeValue == "application/json" { + contentTypeFound = true + + break + } + } + + if !contentTypeFound { + t.Fatalf("content-type is not equal to application/json") + } + b, err := io.ReadAll(r.Body) if err != nil { t.Errorf("unable to read the request body: %v", err)