- weimob wsc api
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
@@ -20,8 +21,23 @@ const (
|
||||
|
||||
const (
|
||||
prodURL = "https://dopen.weimob.com/api/1_0/ec"
|
||||
mediaURL = "https://dopen.weimob.com/media/1_0/ec"
|
||||
authURL = "https://dopen.weimob.com/fuwu/b"
|
||||
accessTokenAPI = "oauth2/token"
|
||||
uploadImgAPI = "goodsImage/uploadImg"
|
||||
)
|
||||
|
||||
const (
|
||||
// sku
|
||||
KeySkuID = "skuId"
|
||||
KeyOuterSkuCode = "outerSkuCode"
|
||||
KeyImageURL = "imageUrl"
|
||||
KeySalePrice = "salePrice"
|
||||
KeyOriginalPrice = "originalPrice"
|
||||
KeyCostPrice = "costPrice"
|
||||
KeyEditStockNum = "editStockNum"
|
||||
KeyB2cSku = "b2cSku"
|
||||
KeySkuAttrMap = "skuAttrMap"
|
||||
)
|
||||
|
||||
type TokenInfo struct {
|
||||
@@ -100,20 +116,37 @@ func (a *API) AccessAPI(apiStr string, apiParams map[string]interface{}) (retVal
|
||||
}))
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, nil)
|
||||
} else {
|
||||
fullURL := utils.GenerateGetURL(prodURL, apiStr, utils.Params2Map("accesstoken", a.MustGetToken().AccessToken))
|
||||
var body io.Reader
|
||||
if apiParams != nil {
|
||||
apiParamsBytes, err := json.Marshal(apiParams)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Error when marshal %v, error:%v", apiParams, err))
|
||||
if apiStr == uploadImgAPI {
|
||||
fullURL := utils.GenerateGetURL(mediaURL, apiStr, utils.Params2Map("accesstoken", a.MustGetToken().AccessToken))
|
||||
var b bytes.Buffer
|
||||
w := multipart.NewWriter(&b)
|
||||
imgData := apiParams["file"].([]byte)
|
||||
imgName, _ := apiParams["name"].(string)
|
||||
if fw, err := w.CreateFormFile("file", imgName); err != nil {
|
||||
panic(err.Error())
|
||||
} else {
|
||||
fw.Write(imgData)
|
||||
}
|
||||
body = bytes.NewReader(apiParamsBytes)
|
||||
w.Close()
|
||||
// b.WriteString(utils.Map2URLValues(params).Encode())
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, &b)
|
||||
request.Header.Set("Content-Type", w.FormDataContentType())
|
||||
} else {
|
||||
fullURL := utils.GenerateGetURL(prodURL, apiStr, utils.Params2Map("accesstoken", a.MustGetToken().AccessToken))
|
||||
var body io.Reader
|
||||
if apiParams != nil {
|
||||
apiParamsBytes, err := json.Marshal(apiParams)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Error when marshal %v, error:%v", apiParams, err))
|
||||
}
|
||||
body = bytes.NewReader(apiParamsBytes)
|
||||
}
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, body)
|
||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
request.Header.Set("Content-Encoding", "gzip, deflate")
|
||||
request.Header.Set("User-Agent", "weimob-golang-api")
|
||||
// request.Close = true //todo 为了性能考虑还是不要关闭
|
||||
}
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, body)
|
||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
request.Header.Set("Content-Encoding", "gzip, deflate")
|
||||
request.Header.Set("User-Agent", "weimob-golang-api")
|
||||
// request.Close = true //todo 为了性能考虑还是不要关闭
|
||||
}
|
||||
return request
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user