mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
180 lines
4.1 KiB
Go
180 lines
4.1 KiB
Go
// 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
|
|
}
|