maxapi: refactor withdrawal request

This commit is contained in:
c9s 2022-04-20 16:38:08 +08:00
parent 72ea9f7e24
commit 8b9383ecfa
3 changed files with 345 additions and 91 deletions

View File

@ -0,0 +1,154 @@
// Code generated by "requestgen -method GET -url v2/withdraw_addresses -type GetWithdrawalAddressesRequest -responseType []WithdrawalAddress"; DO NOT EDIT.
package max
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (g *GetWithdrawalAddressesRequest) Currency(currency string) *GetWithdrawalAddressesRequest {
g.currency = currency
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetWithdrawalAddressesRequest) 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 *GetWithdrawalAddressesRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check currency field -> json key currency
currency := g.currency
// TEMPLATE check-required
if len(currency) == 0 {
return nil, fmt.Errorf("currency is required, empty string given")
}
// END TEMPLATE check-required
// assign parameter of currency
params["currency"] = currency
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetWithdrawalAddressesRequest) 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 *GetWithdrawalAddressesRequest) 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 *GetWithdrawalAddressesRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (g *GetWithdrawalAddressesRequest) 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 *GetWithdrawalAddressesRequest) 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 *GetWithdrawalAddressesRequest) isVarSlice(v interface{}) bool {
rt := reflect.TypeOf(v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetWithdrawalAddressesRequest) 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 *GetWithdrawalAddressesRequest) Do(ctx context.Context) ([]WithdrawalAddress, error) {
// empty params for GET operation
var params interface{}
query, err := g.GetParametersQuery()
if err != nil {
return nil, err
}
apiURL := "v2/withdraw_addresses"
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 []WithdrawalAddress
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse, nil
}

View File

@ -1,8 +1,10 @@
package max
//go:generate -command GetRequest requestgen -method GET
//go:generate -command PostRequest requestgen -method POST
import (
"context"
"errors"
"github.com/c9s/requestgen"
)
/*
@ -30,63 +32,13 @@ import (
}
*/
//go:generate PostRequest -url "v2/withdrawal" -type WithdrawalRequest -responseType .Withdraw
type WithdrawalRequest struct {
client *RestClient
addressUUID string
currency string
amount float64
}
client requestgen.AuthenticatedAPIClient
func (r *WithdrawalRequest) Currency(currency string) *WithdrawalRequest {
r.currency = currency
return r
}
func (r *WithdrawalRequest) AddressUUID(uuid string) *WithdrawalRequest {
r.addressUUID = uuid
return r
}
func (r *WithdrawalRequest) Amount(amount float64) *WithdrawalRequest {
r.amount = amount
return r
}
func (r *WithdrawalRequest) Do(ctx context.Context) (*Withdraw, error) {
if r.currency == "" {
return nil, errors.New("currency field is required")
}
if r.addressUUID == "" {
return nil, errors.New("withdraw_address_uuid field is required")
}
if r.amount <= 0 {
return nil, errors.New("amount is required")
}
payload := map[string]interface{}{
"currency": r.currency,
"withdraw_address_uuid": r.addressUUID,
"amount": r.amount,
}
req, err := r.client.newAuthenticatedRequest(context.Background(), "POST", "v2/withdrawal", nil, payload, nil)
if err != nil {
return nil, err
}
response, err := r.client.SendRequest(req)
if err != nil {
return nil, err
}
var resp Withdraw
if err := response.DecodeJSON(&resp); err != nil {
return nil, err
}
return &resp, nil
addressUUID string `param:"address_uuid,required"`
currency string `param:"currency,required"`
amount float64 `param:"amount"`
}
type WithdrawalAddress struct {
@ -102,41 +54,10 @@ type WithdrawalAddress struct {
IsInternal bool `json:"is_internal"`
}
//go:generate GetRequest -url "v2/withdraw_addresses" -type GetWithdrawalAddressesRequest -responseType []WithdrawalAddress
type GetWithdrawalAddressesRequest struct {
client *RestClient
currency string
}
func (r *GetWithdrawalAddressesRequest) Currency(currency string) *GetWithdrawalAddressesRequest {
r.currency = currency
return r
}
func (r *GetWithdrawalAddressesRequest) Do(ctx context.Context) ([]WithdrawalAddress, error) {
if r.currency == "" {
return nil, errors.New("currency field is required")
}
payload := map[string]interface{}{
"currency": r.currency,
}
req, err := r.client.newAuthenticatedRequest(context.Background(), "GET", "v2/withdraw_addresses", nil, payload, nil)
if err != nil {
return nil, err
}
response, err := r.client.SendRequest(req)
if err != nil {
return nil, err
}
var addresses []WithdrawalAddress
if err := response.DecodeJSON(&addresses); err != nil {
return nil, err
}
return addresses, nil
client requestgen.AuthenticatedAPIClient
currency string `param:"currency,required"`
}
type WithdrawalService struct {

View File

@ -0,0 +1,179 @@
// Code generated by "requestgen -method POST -url v2/withdrawal -type WithdrawalRequest -responseType .Withdraw"; DO NOT EDIT.
package max
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (w *WithdrawalRequest) AddressUUID(addressUUID string) *WithdrawalRequest {
w.addressUUID = addressUUID
return w
}
func (w *WithdrawalRequest) Currency(currency string) *WithdrawalRequest {
w.currency = currency
return w
}
func (w *WithdrawalRequest) Amount(amount float64) *WithdrawalRequest {
w.amount = amount
return w
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (w *WithdrawalRequest) 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 (w *WithdrawalRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check addressUUID field -> json key address_uuid
addressUUID := w.addressUUID
// TEMPLATE check-required
if len(addressUUID) == 0 {
return nil, fmt.Errorf("address_uuid is required, empty string given")
}
// END TEMPLATE check-required
// assign parameter of addressUUID
params["address_uuid"] = addressUUID
// check currency field -> json key currency
currency := w.currency
// TEMPLATE check-required
if len(currency) == 0 {
return nil, fmt.Errorf("currency is required, empty string given")
}
// END TEMPLATE check-required
// assign parameter of currency
params["currency"] = currency
// check amount field -> json key amount
amount := w.amount
// assign parameter of amount
params["amount"] = amount
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (w *WithdrawalRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := w.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
if w.isVarSlice(v) {
w.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 (w *WithdrawalRequest) GetParametersJSON() ([]byte, error) {
params, err := w.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 (w *WithdrawalRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (w *WithdrawalRequest) 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 (w *WithdrawalRequest) 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 (w *WithdrawalRequest) isVarSlice(v interface{}) bool {
rt := reflect.TypeOf(v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (w *WithdrawalRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := w.GetSlugParameters()
if err != nil {
return slugs, nil
}
for k, v := range params {
slugs[k] = fmt.Sprintf("%v", v)
}
return slugs, nil
}
func (w *WithdrawalRequest) Do(ctx context.Context) (*Withdraw, error) {
params, err := w.GetParameters()
if err != nil {
return nil, err
}
query := url.Values{}
apiURL := "v2/withdrawal"
req, err := w.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := w.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse Withdraw
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return &apiResponse, nil
}