Skip to content

Commit

Permalink
feat: Product API
Browse files Browse the repository at this point in the history
  • Loading branch information
ipfans committed Apr 25, 2022
1 parent 5c84050 commit 06d9aa9
Show file tree
Hide file tree
Showing 16 changed files with 1,659 additions and 15 deletions.
21 changes: 21 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ func (c *Client) Post(ctx context.Context, path string, param url.Values, body i
return
}

// Put request for TikTok requests.
// Note: Timestamp, appkey and signature will auto-management by action.
func (c *Client) Put(ctx context.Context, path string, param url.Values, body interface{}, resp interface{}) (err error) {
param = c.prepareParam(path, param)
r := c.prepareBody(body)
err = c.request(ctx, http.MethodPut, APIBaseURL, path, param, r, resp)
return
}

// Delete request for TikTok requests. I don't known why there is body in delete request.
// Note: Timestamp, appkey and signature will auto-management by action.
func (c *Client) Delete(ctx context.Context, path string, param url.Values, body interface{}, resp interface{}) (err error) {
param = c.prepareParam(path, param)
var r io.Reader
if body != nil {
r = c.prepareBody(body)
}
err = c.request(ctx, http.MethodDelete, APIBaseURL, path, param, r, resp)
return
}

func (c *Client) prepareParam(path string, param url.Values) url.Values {
ak := safeGet(param, "access_token")
if ak != "" {
Expand Down
35 changes: 30 additions & 5 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ func setupMock(t *testing.T, tt TestRecord, args, want interface{}) {
if tt.Request.Query != "" {
require.Equal(t, tt.Request.Query, r.URL.RawQuery)
}
var b []byte
if r.Method == http.MethodPost || r.Method == http.MethodPut {
defer r.Body.Close()
b, _ = io.ReadAll(r.Body)
}

if len(tt.Request.Body) > 0 {
defer r.Body.Close()
b, _ := io.ReadAll(r.Body)
require.JSONEq(t, string(tt.Request.Body), string(b))
} else {
tt.Request.Body = []byte(`{}`)
Expand Down Expand Up @@ -85,3 +83,30 @@ func mockTime() func() {
}
}
}

func mockTests(t *testing.T, fn string, args, want interface{}, exec func() (interface{}, error)) {
t.Helper()

restore := mockTime()
defer restore()

tests := loadTestData(t, fn)
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
setupMock(t, tt, args, want)

got, err := exec()
if tt.WantErr {
require.Error(t, err)
return
} else {
require.NoError(t, err)
if want != nil {
require.Equal(t, want, got)
}
}
})
}
}
88 changes: 78 additions & 10 deletions product.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,110 @@ func (c *Client) UploadFile(ctx context.Context, p Param, fn string, body []byte
return
}

func (c *Client) CreateProduct(ctx context.Context, p Param) (err error) {
func (c *Client) CreateProduct(ctx context.Context, p Param, req CreateProductRequest) (product Product, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
if err = c.validate.Struct(&req); err != nil {
return
}
err = c.Post(ctx, "/api/products", param, req, &product)
return
}

func (c *Client) EditProduct(ctx context.Context, p Param) (err error) {
func (c *Client) EditProduct(ctx context.Context, p Param, req EditProductRequest) (product Product, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}

if err = c.validate.Struct(&req); err != nil {
return
}
err = c.Put(ctx, "/api/products", param, req, &product)
return
}

func (c *Client) GetProductList(ctx context.Context, p Param) (err error) {
func (c *Client) GetProductList(ctx context.Context, p Param, req ProductSearchRequest) (list ProductSearchList, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
err = c.Post(ctx, "/api/products/search", param, req, &list)
return
}

func (c *Client) GetProductDetail(ctx context.Context, p Param) (err error) {
func (c *Client) GetProductDetail(ctx context.Context, p Param, productID string) (product Product, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
param.Set("product_id", productID)
err = c.Get(ctx, "/api/products/details", param, &product)
return
}

func (c *Client) UpdatePrice(ctx context.Context, p Param) (err error) {
func (c *Client) UpdatePrice(ctx context.Context, p Param, req UpdatePriceRequest) (list UpdatePriceFailedSKU, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
err = c.Put(ctx, "/api/products/prices", param, req, &list)
return
}

func (c *Client) UpdateStock(ctx context.Context, p Param) (err error) {
func (c *Client) UpdateStock(ctx context.Context, p Param, req UpdateStockRequest) (list UpdateStockFailedSKU, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
err = c.Put(ctx, "/api/products/stocks", param, req, &list)
return
}

func (c *Client) DeactivateProducts(ctx context.Context, p Param) (err error) {
func (c *Client) DeactivateProducts(ctx context.Context, p Param, ids []string) (list FailedProductIDs, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
req := map[string][]string{
"product_ids": ids,
}
err = c.Post(ctx, "/api/products/inactivated_products", param, req, &list)
return
}

func (c *Client) DeleteProducts(ctx context.Context, p Param) (err error) {
func (c *Client) DeleteProducts(ctx context.Context, p Param, ids []string) (list FailedProductIDs, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
req := map[string][]string{
"product_ids": ids,
}
err = c.Delete(ctx, "/api/products", param, req, &list)
return
}

func (c *Client) RecoverProduct(ctx context.Context, p Param) (err error) {
func (c *Client) RecoverProduct(ctx context.Context, p Param, ids []string) (list FailedProductIDs, err error) {
param := url.Values{}
param.Set("access_token", p.AccessToken)
req := map[string][]string{
"product_ids": ids,
}
err = c.Post(ctx, "/api/products/recover", param, req, &list)
return
}

func (c *Client) ActivateProduct(ctx context.Context, p Param) (err error) {
func (c *Client) ActivateProduct(ctx context.Context, p Param, ids []string) (list FailedProductIDs, err error) {
var param url.Values
if param, err = c.params(p); err != nil {
return
}
req := map[string][]string{
"product_ids": ids,
}
err = c.Post(ctx, "/api/products/activate", param, req, &list)
return
}
Loading

0 comments on commit 06d9aa9

Please sign in to comment.