requestgen

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2021 License: MIT Imports: 13 Imported by: 68

README

requestgen

requestgen generates the cascade call for your request object

Installation

go get github.com/c9s/requestgen/cmd/requestgen

Usage

requestgen scans all the fields of the target struct, and generate setter methods and getParameters method.

package api

import "github.com/c9s/requestgen"

//go:generate requestgen -type PlaceOrderRequest
type PlaceOrderRequest struct {
	// client is an optional field to implement
	// if you add this field with the APIClient interface type, the Do() method will be generated
	// note, you will have to add flag "-url" and "-method" to specify your endpoint and the request method.
	client requestgen.APIClient
	
	// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters.
	clientOrderID *string `param:"clientOid,required" defaultValuer:"uuid()"`

	symbol string `param:"symbol,required"`

	// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 8 characters.
	tag *string `param:"tag"`

	// "buy" or "sell"
	side SideType `param:"side,required" validValues:"buy,sell"`

	orderType OrderType `param:"ordType" validValues:"limit,market"`

	size string `param:"size"`

	// limit order parameters
	price *string `param:"price,omitempty"`

	timeInForce *TimeInForceType `param:"timeInForce,omitempty" validValues:"GTC,GTT,FOK"`

	complexArg ComplexArg `param:"complexArg"`

	startTime *time.Time `param:"startTime,milliseconds" defaultValuer:"now()"`
}

Or you can run generate command manually like this:

go run ./cmd/requestgen -type PlaceOrderRequest -method GET -url "/api/v1/bullet" -debug ./example/api 

Then you can do:

req := &PlaceOrderRequest{}
err := req.Tag(..).
	OrderType(OrderTypeLimit).
	Side(SideTypeBuy).
	Do(ctx)

See the generated example

Command Options

-responseType [responseTypeSelector]

When responseTypeSelector is not given, interface{} will be used for decoding the response content from the API server.

You can define your own responseType struct that can decode the API response, like this, e.g.,

type Response struct {
	Code    string          `json:"code"`
	Message string          `json:"msg"`
	CurrentPage int `json:"currentPage"`
	PageSize    int `json:"pageSize"`
	TotalNum    int `json:"totalNum"`
	TotalPage   int `json:"totalPage"`
	Orders      []Orders `json:"orders"`
}

And then use the type selector like this:

# if the type is in a relative package
requestgen -responseType '"./example/api".Response'

# if the type is in the same package
requestgen -responseType '".".Response'

-responseDataField [dataField]

When dataField is given, it means your data is inside the responseType, the field name is where you want to extract the data from. Be sure to define dataField as a json.RawMessage so that the generated code can handle the decoding separately.

For example:

type Response struct {
	Code    string          `json:"code"`
	Message string          `json:"msg"`
	CurrentPage int `json:"currentPage"`
	PageSize    int `json:"pageSize"`
	TotalNum    int `json:"totalNum"`
	TotalPage   int `json:"totalPage"`
	Data        json.RawMessage `json:"data"`
}

-responseDataType [dataType]

When dataType is given, it means your data is inside the responseType. the raw json message will be decoded with this given type.

APIClient

You can implement your own http API client struct that satisfies the following interface (defined in requestgen.APIClient)

// APIClient defines the request builder method and request method for the API service
type APIClient interface {
	// NewRequest builds up the http request for public endpoints
	NewRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error)

	// SendRequest sends the request object to the api gateway
	SendRequest(req *http.Request) (*Response, error)
}

// AuthenticatedAPIClient defines the authenticated request builder
type AuthenticatedAPIClient interface {
	APIClient
	AuthenticatedRequestBuilder
}

See a real example implementation of the APIClient interface. kucoin exchange client

See Also

LICENSE

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIClient

type APIClient interface {
	// NewRequest builds up the http request for public endpoints
	NewRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error)

	// SendRequest sends the request object to the api gateway
	SendRequest(req *http.Request) (*Response, error)
}

APIClient defines the request builder method and request method for the API service

type AuthenticatedAPIClient added in v1.0.0

type AuthenticatedAPIClient interface {
	APIClient
	AuthenticatedRequestBuilder
}

type AuthenticatedRequestBuilder added in v1.0.0

type AuthenticatedRequestBuilder interface {
	// NewAuthenticatedRequest builds up the http request for authentication-required endpoints
	NewAuthenticatedRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error)
}

type Response

type Response struct {
	*http.Response

	// Body overrides the composited Body field.
	Body []byte
}

Response is wrapper for standard http.Response and provides more methods.

func NewResponse

func NewResponse(r *http.Response) (response *Response, err error)

NewResponse is a wrapper of the http.Response instance, it reads the response body and close the file.

func (*Response) DecodeJSON

func (r *Response) DecodeJSON(o interface{}) error

func (*Response) IsError

func (r *Response) IsError() bool

func (*Response) IsHTML

func (r *Response) IsHTML() bool

func (*Response) IsJSON

func (r *Response) IsJSON() bool

func (*Response) String

func (r *Response) String() string

String converts response body to string. An empty string will be returned if error.

type TypeSelector added in v1.0.0

type TypeSelector struct {
	Package string
	Member  string
}

func ParseTypeSelector added in v1.0.0

func ParseTypeSelector(main string) (*TypeSelector, error)

Directories

Path Synopsis
cmd
requestgen command
requestgen generates the request builder methods.
requestgen generates the request builder methods.
example
api

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL