lazada

package
v0.0.0-...-5526aeb Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2018 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package lazada provides the client for interacting with the Lazada open platform

Index

Examples

Constants

View Source
const (
	SriLanka    Region = "lk"
	Philippines        = "ph"
	Bangladesh         = "bd"
	Thailand           = "th"
	Vietnam            = "vn"
	Pakistan           = "pk"
	Singapore          = "sg"
	Nepal              = "np"
	Indonesia          = "id"
	Myanmar            = "mm"
	Malaysia           = "my"
)

Variables

View Source
var DefaultListOptions = ListOptions{
	Limit:  100,
	Offset: 0,
}

Default list options

Functions

func SliceString

func SliceString(in []string) string

SliceString takes in a slice of strings and returns a string used in query parameters for the open platform

Types

type Attributes

type Attributes struct {
	XMLName xml.Name `xml:"Attributes,omitempty"`
	Attrs   StringMap
}

type AuthService

type AuthService service

The Auth Service deals with doing the OAuth flow and exchanging codes for tokens. It also lets you refresh tokens in order to get new credentials.

func (*AuthService) AuthURL

func (a *AuthService) AuthURL(redirect, state string) string

AuthURL returns the URL you should use to start the OAuth flow It takes in the URL that the user should be returned to as redirect and a state variable which should be a random string

func (*AuthService) Exchange

func (a *AuthService) Exchange(ctx context.Context, code string) (*Token, error)

Exchange sends the received oauth code to the open platform and returns a token

func (*AuthService) Refresh

func (a *AuthService) Refresh(ctx context.Context, token string) (*Token, error)

Refresh sends the refresh token and returns the new refreshed token

type Brand

type Brand struct {
	Name             string `json:"name"`
	BrandID          int    `json:"brand_id"`
	GlobalIdentifier string `json:"global_identifier"`
}

A brand object returned from the open platform

type CategoryAttributes

type CategoryAttributes struct {
	Label         string             `json:"label"`
	Name          string             `json:"name"`
	IsMandatory   int                `json:"is_mandatory"`
	AttributeType string             `json:"attribute_type"`
	InputType     string             `json:"input_type"`
	Options       []*CategoryOptions `json:"options"`
	IsSale        int                `json:"is_sale_prop"`
}

type CategoryOptions

type CategoryOptions struct {
	Name string
}

type CategoryTree

type CategoryTree struct {
	CategoryID int             `json:"category_id"`
	Children   []*CategoryTree `json:"children"`
	Var        bool            `json:"var"`
	Name       string          `json:"name"`
	Leaf       bool            `json:"leaf"`
}

type Client

type Client struct {
	BaseURL *url.URL

	// The product service used for making API calls related to products
	Products *ProductService

	// The auth service used for making API calls related to authorization or OAuth
	Auth *AuthService
	// contains filtered or unexported fields
}

func NewClient

func NewClient(appKey, secret string, region Region) *Client

NewClient takes in the application key, secret, and Lazada region and returns a client.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*LazadaResponse, error)

Do runs a http.Request adding in the various required query parameters if they weren't set by the body already. It will marshal the data returned into the provided interface.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)

NewRequest returns an http request conforming to the open platform Any body supplied will be encoded to XML

func (*Client) NewTokenClient

func (c *Client) NewTokenClient(token string) *Client

NewTokenClient takes a client access token and returns a copy of the client with the token set.

func (*Client) SetRegion

func (c *Client) SetRegion(region Region)

SetRegion changes the region on the client

func (*Client) Signature

func (c *Client) Signature(api string, val url.Values, body io.Reader) string

Signature calculates the signature for the query parameter needed with every request

type CreateProductResponse

type CreateProductResponse struct {
	ItemID  int64     `json:"item_id"`
	SKUList []SKUItem `json:"sku_list"`
}

type ErrorDetails

type ErrorDetails struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

type ErrorResponse

type ErrorResponse struct {
	Response *http.Response

	Code      string          `json:"code"`
	Type      string          `json:"type"`
	Message   string          `json:"message"`
	RequestID string          `json:"request_id"`
	Detail    []*ErrorDetails `json:"detail,omitempty"`
}

Error response is used to return as much data as possible to the calling application to help with dealing with any API issues.

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type GetProduct

type GetProduct struct {
	ItemID          int               `json:"item_id"`
	PrimaryCategory int               `json:"primary_category"`
	Attributes      map[string]string `json:"attributes"`
	SKUs            []*ProductSKU     `json:"skus"`
}

type GetProductResponse

type GetProductResponse struct {
	TotalProducts int           `json:"total_products"`
	Products      []*GetProduct `json:"products"`
}

type ImageReq

type ImageReq struct {
	XMLName xml.Name `xml:"Request"`
	URL     string   `xml:"Image>Url"`
}

type ImageResponse

type ImageResponse struct {
	Image struct {
		HashCode string `json:"hash_code"`
		URL      string `json:"url"`
	}
}

type Images

type Images struct {
	XMLName xml.Name `xml:"Images,omitempty"`
	Image   []string `xml:"Image,omitempty"`
}

type LazadaResponse

type LazadaResponse struct {
	Code      string          `json:"code"`
	Data      json.RawMessage `json:"data"`
	RequestID string          `json:"request_id"`

	Type    string          `json:"type"`
	Message string          `json:"message"`
	Detail  []*ErrorDetails `json:"detail"`
}

func CheckResponse

func CheckResponse(r *http.Response) (*LazadaResponse, error)

CheckResponse makes sure we didn't receive an error from the platform and if we did it returns the error properly.

type ListOptions

type ListOptions struct {
	// Used to paginate through results
	Offset int `url:"offset"`

	// Limit how many items are returned
	Limit int `url:"limit"`
}

ListOptions used for various API calls

type Payload

type Payload struct {
	Payload string `url:"payload"`
}

type Product

type Product struct {
	XMLName         xml.Name    `xml:"Product,omitempty"`
	Attributes      *Attributes `xml:"Attributes,omitempty"`
	PrimaryCategory string      `xml:"PrimaryCategory,omitempty"`
	Skus            []*Sku      `xml:"Skus>Sku,omitempty"`
}

type ProductRequest

type ProductRequest struct {
	XMLName xml.Name `xml:"Request,omitempty"`
	Product *Product `xml:"Product,omitempty"`
}

type ProductSKU

type ProductSKU struct {
	Status          string          `json:"Status"`
	Quantity        int             `json:"quantity"`
	ProductWeight   string          `json:"product_weight"`
	Images          []string        `json:"Images"`
	SellerSKU       string          `json:"SellerSku"`
	ShopSKU         string          `json:"ShopSku"`
	URL             string          `json:"Url"`
	PackageWidth    string          `json:"package_width"`
	SpecialToTime   string          `json:"special_to_time"`
	SpecialFromTime string          `json:"special_from_time"`
	PackageHeight   string          `json:"package_height"`
	SpecialPrice    decimal.Decimal `json:"special_price"`
	Price           decimal.Decimal `json:"price"`
	PackageLength   string          `json:"package_length"`
	PackageWeight   string          `json:"package_weight"`
	Available       int             `json:"Available"`
	SkuID           int             `json:"SkuId"`
	SpecialToDate   string          `json:"special_to_date"`
}

type ProductService

type ProductService service

The Product Service deals with any methods under the "Product" category of the open platform

func (*ProductService) Brands

func (p *ProductService) Brands(ctx context.Context, opts *ListOptions) ([]*Brand, error)

Brands returns a list of brands in the region set If opts is nil then the default options are used

func (*ProductService) CategoryAttributes

func (p *ProductService) CategoryAttributes(ctx context.Context, id int) ([]*CategoryAttributes, error)

CategoryAttributes returns all the attributes related to the category id provided

func (*ProductService) CategoryTree

func (p *ProductService) CategoryTree(ctx context.Context) ([]*CategoryTree, error)

CategoryTree returns all the categories available in the region set

func (*ProductService) Create

Create lets you create a new product on the open platform.

Requires a client access token

Example
client := NewClient("12345", "example", Singapore)

userClient := client.NewTokenClient("usertoken") // Set the a token obtained through oauth
userClient.SetRegion(Malaysia)                   // Change the region to Malaysia

product := &Product{
	PrimaryCategory: "10001958",
	Attributes: &Attributes{
		Attrs: StringMap{
			"name":               "test product creation",
			"short_description":  "test product highlights",
			"description":        "test product description",
			"brand":              "Kid Basix",
			"model":              "test model",
			"recommended_gender": "Men",
			"material":           "Cotton",
			"waterproof":         "waterproof",
			"warranty_type":      "Local (Singapore) manufacturer warranty",
			"warranty":           "1 month",
			"Hazmat":             "Battery, Flammable",
		}},
	Skus: []*Sku{
		{
			SellerSku: "test-product-creation-for-api",
			Images:    &Images{Image: []string{"https://sg-live.slatic.net/original/b731a8098df7d606ab2e56efc650afcb.jpg"}},
			SkuAttrs: StringMap{
				"quantity":        "1",
				"color_family":    "Black",
				"special_price":   "0.0",
				"price":           "23.0",
				"package_length":  "1",
				"package_weight":  "1",
				"package_content": "test whats in the box",
				"package_width":   "1",
				"package_height":  "1",
			},
		},
	},
}

userClient.Products.Create(context.Background(), product)
Output:

func (*ProductService) Get

Get lets you retrieve all products in a specific region

func (*ProductService) MigrateImage

func (p *ProductService) MigrateImage(ctx context.Context, imgURL string) (*ImageResponse, error)

MigrateImage lets you move any publicly accessible image into the Lazada platform Requires a client access token

func (*ProductService) Update

func (p *ProductService) Update(ctx context.Context, pReq *Product) error

Update lets you update an existing product on the open platform Requires a client access token

type Region

type Region string

type SKUItem

type SKUItem struct {
	ShopSKU   string `json:"shop_sku"`
	SellerSKU string `json:"seller_sku"`
	SKUID     string `json:"sku_id"`
}

type SearchOptions

type SearchOptions struct {
	// Filter the product status
	Filter string `url:"filter"`

	// Search for products with this name or seller sku
	Search *string `url:"search,omitempty"`

	// Set to "1" to get more stock information
	Options *string `url:"options,omitempty"`

	// Return only products that have this seller sku
	SKUSellerList *string `url:"sku_seller_list,omitempty"`

	// Offset the results by
	Offset int `url:"offset"`

	// Limit the amount of returned results
	Limit int `url:"limit"`
}

type Sku

type Sku struct {
	XMLName   xml.Name `xml:"Sku,omitempty"`
	Images    *Images  `xml:"Images,omitempty"`
	SellerSku string   `xml:"SellerSku"`
	SkuAttrs  StringMap
}

type StringMap

type StringMap map[string]string

func (StringMap) MarshalXML

func (s StringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error

StringMap marshals a map into XML.

type Token

type Token struct {
	AccountID       string `json:"account_id"`
	Account         string `json:"account"`
	Country         string `json:"country"`
	AccountPlatform string `json:"account_platform"`

	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`

	ExpiresIn        int `json:"expires_in"`
	RefreshExpiresIn int `json:"refresh_expires_in"`
	// contains filtered or unexported fields
}

Token is the data returned when doing an Oauth Flow through the open platform

func (*Token) ExpiresAt

func (t *Token) ExpiresAt() time.Time

ExpiresAt tells you the point in time when this token will expire

func (*Token) RefreshExpiresAt

func (t *Token) RefreshExpiresAt() time.Time

RefreshExpiresAt tells you the point in time when the refresh token will expire

func (*Token) Valid

func (t *Token) Valid() bool

Valid tells you if the token is valid right now

Jump to

Keyboard shortcuts

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