top

package module
v0.0.0-...-b4b70d3 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: MIT Imports: 17 Imported by: 0

README

topsdk-go (淘宝开放平台 SDK in GO)

HTChang/topsdk-go implements the TOP protocol that deals with APIs in single or batch request with simple json response.

go get -u github.com/HTChang/topsdk-go
import "github.com/HTChang/topsdk-go"

Reference

淘宝开放平台API文档

Examples

Create client
client, err := top.NewClient("APP_KEY", "APP_SECRET",
	top.WithSession("SESSION_KEY"))
if err != nil {
    // handle error
    return
}
Single request
res, err := client.DoJson(context.Background(), "taobao.items.onsale.get", top.Parameters{
	"fields":    "total_results,approve_status,num_iid,num",
	"page_no":   pageNo,
	"page_size": pageSize,
})
if tErr, ok := err.(*top.ErrorResponse); ok {
    // can handle the top error for business logic
    return
} else if err != nil {
    // handle other errors 
    return
}

root := res.Get("items_onsale_get_response")
total := root.Get("total_results").MustInt()
itemsBytes, err := root.Get("items").Get("item").MarshalJSON()
// unmarshal to self-defined struct 
// do stuff...
...
Batch request
params := make([]top.Parameters, len(tids))
for i, tid := range tids {
    params[i] = top.Parameters{
        "method": "taobao.trade.fullinfo.get", 
        "tid": tid, 
        "fields": "tid,tid_str,receiver_mobile,receiver_phone",
    }
}
results, err := client.DoJsonBatch(context.Background(), params...)
if tErr, ok := err.(*top.ErrorResponse); ok {
    // can handle the top error for business logic
    return
} else if err != nil {
    // handle other errors
    return
}

for i, res := range results {
    tid := params[i]["tid"].(int64)
    if tErr, ok := res.Err.(*top.ErrorResponse); ok {
        // can handle the top error for business logic 
    	continue
    } else if res.Err != nil { 
        // handle other errors
        continue
    }
    trade := res.Get("trade_fullinfo_get_response").Get("trade")
    mobile := trade.Get("receiver_mobile").MustString()
    phone := trade.Get("receiver_phone").MustString()
    // do stuff...
}
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchResult

type BatchResult struct {
	*simplejson.Json
	Err error
}

BatchResult defines the batch result.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client defines the open taobap client.

func NewClient

func NewClient(appKey, appSecret string, opts ...ClientOption) (*Client, error)

NewClient creates the `Client` according to the given options.

func (*Client) DoJson

func (c *Client) DoJson(ctx context.Context, method string, param Parameters) (res *simplejson.Json, err error)

DoJson executes the taobao api. https://open.taobao.com/doc.htm?docId=101617&docType=1

func (*Client) DoJsonBatch

func (c *Client) DoJsonBatch(ctx context.Context, params ...Parameters) ([]*BatchResult, error)

DoJsonBatch executes the taobao api in batch. https://open.taobao.com/doc.htm?docId=104350&docType=1

type ClientOption

type ClientOption func(opts *options)

ClientOption for building up `Client`.

func WithApiBatchURL

func WithApiBatchURL(batchURL string) ClientOption

WithApiBatchURL returns a `Option` that sets the batch api URL.

func WithApiURL

func WithApiURL(url string) ClientOption

WithApiURL returns a `Option` that sets the api URL.

func WithSession

func WithSession(session string) ClientOption

WithSession returns a `Option` that set the session key. https://open.taobao.com/doc.htm?docId=102635&docType=1

type ErrorResponse

type ErrorResponse struct {
	Code    int32  `json:"code"`
	Msg     string `json:"msg"`
	SubMsg  string `json:"sub_msg"`
	SubCode string `json:"sub_code"`
}

ErrorResponse defines the open taobao platform error response.

func (*ErrorResponse) Error

func (e *ErrorResponse) Error() string

type Parameters

type Parameters map[string]interface{}

Parameters defines the key value pair of parameters.

Jump to

Keyboard shortcuts

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