You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
6 months ago
|
package api
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
|
||
|
"git.cheetah.cat/chee-erp/go-flink-inator/common"
|
||
|
"github.com/go-resty/resty/v2"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
APIClient struct {
|
||
|
apiClient *resty.Client
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func NewAPIClient() (apiClient *APIClient) {
|
||
|
return &APIClient{
|
||
|
apiClient: resty.New().
|
||
|
SetJSONMarshaler(json.Marshal).
|
||
|
SetJSONUnmarshaler(json.Unmarshal),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (ac *APIClient) GetHub(lat, lng float64) (res HubResult, err error) {
|
||
|
resp, err := ac.apiClient.R().
|
||
|
SetResult(&res).
|
||
|
SetQueryParams(map[string]string{
|
||
|
"lat": fmt.Sprint(lat),
|
||
|
"long": fmt.Sprint(lng),
|
||
|
}).
|
||
|
SetHeader("Accept", "application/json").
|
||
|
//SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F").
|
||
|
Get(common.UrlLocationsHubV1)
|
||
|
if err != nil {
|
||
|
return res, err
|
||
|
}
|
||
|
|
||
|
if resp.IsSuccess() {
|
||
|
res = *(resp.Result().(*HubResult))
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|
||
|
|
||
|
func (ac *APIClient) DiscoverCategories(hubSlug string) (res DiscoverCategoriesResult, err error) {
|
||
|
resp, err := ac.apiClient.R().
|
||
|
SetResult(&res).
|
||
|
SetHeader("Hub-Slug", hubSlug).
|
||
|
SetHeader("Accept", "application/json").
|
||
|
Get(common.UrlDiscoverV2)
|
||
|
if err != nil {
|
||
|
return res, err
|
||
|
}
|
||
|
if resp.IsSuccess() {
|
||
|
res = *(resp.Result().(*DiscoverCategoriesResult))
|
||
|
} else {
|
||
|
return res, errors.New(string(resp.Body()))
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|
||
|
func (ac *APIClient) DiscoverCategoriesSpecific(hubSlug string, catSlug string) (res DiscoverCategorySubEntry, err error) {
|
||
|
resp, err := ac.apiClient.R().
|
||
|
SetResult(&res).
|
||
|
SetHeader("Hub-Slug", hubSlug).
|
||
|
SetHeader("Accept", "application/json").
|
||
|
Get(fmt.Sprintf(common.UrlDiscoverV2Specific, catSlug))
|
||
|
if err != nil {
|
||
|
return res, err
|
||
|
}
|
||
|
if resp.IsSuccess() {
|
||
|
res = *(resp.Result().(*DiscoverCategorySubEntry))
|
||
|
} else {
|
||
|
return res, errors.New(string(resp.Body()))
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|
||
|
|
||
|
func (ac *APIClient) DiscoverProduct(hubSlug string, skus []string) (res DiscoverSubProductList, err error) {
|
||
|
//
|
||
|
resp, err := ac.apiClient.R().
|
||
|
SetResult(&res).
|
||
|
SetBody(map[string][]string{
|
||
|
"skus": skus,
|
||
|
}).
|
||
|
SetHeader("Hub-Slug", hubSlug).
|
||
|
SetHeader("Accept", "application/json").
|
||
|
Get(common.UrlDiscoverV3Product)
|
||
|
if err != nil {
|
||
|
return res, err
|
||
|
}
|
||
|
if resp.IsSuccess() {
|
||
|
res = *(resp.Result().(*DiscoverSubProductList))
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|