maxapi: rewrite vip level request

This commit is contained in:
c9s 2022-04-20 13:35:17 +08:00
parent 68abeb826b
commit 387c0bfb8b
4 changed files with 163 additions and 1 deletions

View File

@ -635,7 +635,7 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
} }
} }
vipLevel, err := e.client.AccountService.VipLevel() vipLevel, err := e.client.AccountService.NewGetVipLevelRequest().Do(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -75,6 +75,15 @@ type VipLevel struct {
Next VipLevelSettings `json:"next_vip_level"` Next VipLevelSettings `json:"next_vip_level"`
} }
//go:generate GetRequest -url "v2/members/vip_level" -type GetVipLevelRequest -responseType .VipLevel
type GetVipLevelRequest struct {
client requestgen.AuthenticatedAPIClient
}
func (s *AccountService) NewGetVipLevelRequest() *GetVipLevelRequest {
return &GetVipLevelRequest{ client: s.client }
}
func (s *AccountService) VipLevel() (*VipLevel, error) { func (s *AccountService) VipLevel() (*VipLevel, error) {
req, err := s.client.newAuthenticatedRequest(context.Background(), "GET", "v2/members/vip_level", nil, nil, nil) req, err := s.client.newAuthenticatedRequest(context.Background(), "GET", "v2/members/vip_level", nil, nil, nil)
if err != nil { if err != nil {

View File

@ -52,3 +52,21 @@ func TestAccountService_GetAccountRequest(t *testing.T) {
assert.NotNil(t, account) assert.NotNil(t, account)
t.Logf("account: %+v", account) t.Logf("account: %+v", account)
} }
func TestAccountService_GetVipLevelRequest(t *testing.T) {
key, secret, ok := integrationTestConfigured(t, "MAX")
if !ok {
t.SkipNow()
}
ctx := context.Background()
client := NewRestClient(ProductionAPIURL)
client.Auth(key, secret)
req := client.AccountService.NewGetVipLevelRequest()
vipLevel, err := req.Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, vipLevel)
t.Logf("vipLevel: %+v", vipLevel)
}

View File

@ -0,0 +1,135 @@
// Code generated by "requestgen -method GET -url v2/members/vip_level -type GetVipLevelRequest -responseType .VipLevel"; DO NOT EDIT.
package max
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetVipLevelRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetVipLevelRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetVipLevelRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
if g.isVarSlice(v) {
g.iterateSlice(v, func(it interface{}) {
query.Add(k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(k, fmt.Sprintf("%v", v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetVipLevelRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetVipLevelRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetVipLevelRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for k, v := range slugs {
needleRE := regexp.MustCompile(":" + k + "\\b")
url = needleRE.ReplaceAllString(url, v)
}
return url
}
func (g *GetVipLevelRequest) iterateSlice(slice interface{}, f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for i := 0; i < sliceValue.Len(); i++ {
it := sliceValue.Index(i).Interface()
f(it)
}
}
func (g *GetVipLevelRequest) isVarSlice(v interface{}) bool {
rt := reflect.TypeOf(v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetVipLevelRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for k, v := range params {
slugs[k] = fmt.Sprintf("%v", v)
}
return slugs, nil
}
func (g *GetVipLevelRequest) Do(ctx context.Context) (*VipLevel, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "v2/members/vip_level"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse VipLevel
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return &apiResponse, nil
}