first commit

master
cheetah 6 months ago
commit faaae240fa

@ -0,0 +1,96 @@
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
}

@ -0,0 +1,78 @@
package api
type (
SlugIdBase struct {
Slug *string `json:"slug,omitempty"`
Id *string `json:"id,omitempty"`
}
MonetaryValue struct {
Currency string `json:"currency"`
Amount float64 `json:"amount"`
}
UnitAmount struct {
Unit string `json:"currency"`
Amount float64 `json:"amount"`
}
HubResult struct {
SlugIdBase
Address string `json:"address"`
City string `json:"city"`
Details HubDetails `json:"details"`
Country string `json:"country"`
DeliversToRequestedLocation bool `json:"delivers_to_requested_location"`
}
HubClosure struct {
Message string
Title string
Case string
}
HubDetails struct {
Closure HubClosure `json:"closure"`
MinimumorderValue MonetaryValue `json:"minimum_order_value"`
ShippingFee MonetaryValue `json:"shipping_fee"`
}
// ConsumerBackend Discovery API v2
DiscoverCategoriesResult struct {
SlugIdBase
Categories DiscoverCategoriesList `json:"categories"`
Title *string `json:"title,omitempty"`
}
DiscoverCategoriesList struct {
Categories []DiscoverCategoryEntry `json:"categories"`
}
DiscoverCategoryEntry struct {
SlugIdBase
BackgroundImage *string `json:"backgroundImage,omitempty"`
Icon *string `json:"icon,omitempty"`
HREF *string `json:"href,omitempty"`
Name string `json:"name"`
SubCategories *[]DiscoverCategorySubEntry `json:"subCategories,omitempty"`
Products *DiscoverSubProductList `json:"products,omitempty"`
}
// first layer
DiscoverCategorySubEntry struct {
Name string `json:"name"`
Categories []DiscoverCategoryEntry `json:"categories"`
Title *string `json:"title,omitempty"`
SlugIdBase
}
//second layer with products
DiscoverSubProductList struct {
Products *[]DiscoverSubProductEntry `json:"products"`
}
DiscoverSubProductEntry struct {
SlugIdBase
BasePrice MonetaryValue `json:"base_price"`
BaseUnit UnitAmount `json:"base_unit"`
MaxSingleOrderquantity int64 `json:"max_single_order_quantity"`
Name string `json:"name"`
Price MonetaryValue `json:"price"`
Quantity float64 `json:"quantity"`
SKU string `json:"sku"`
Thumbnail string `json:"thumbnail"`
}
//
)

@ -0,0 +1,10 @@
package common
const (
UrlLocationsHubV1 = "https://consumer-api.goflink.com/v1/locations/hub" // locations/hub?lat=52.1303067&long=11.6097666
ConsumerDiscoveryBackendUrl = "https://api.goflink.com/consumer-backend/discovery/v2/"
UrlDiscoverV2 = "https://api.goflink.com/consumer-backend/discovery/v2/categories"
UrlDiscoverV2Specific = "https://api.goflink.com/consumer-backend/discovery/v2/categories/%s"
UrlDiscoverV3Product = "https://api.goflink.com/consumer-backend/discovery/v3/products"
)

@ -0,0 +1,8 @@
module git.cheetah.cat/chee-erp/go-flink-inator
go 1.21.4
require (
github.com/go-resty/resty/v2 v2.13.1 // indirect
golang.org/x/net v0.25.0 // indirect
)

@ -0,0 +1,48 @@
github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g=
github.com/go-resty/resty/v2 v2.13.1/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -0,0 +1 @@
package main

File diff suppressed because it is too large Load Diff

@ -0,0 +1,40 @@
package main
import (
"fmt"
"git.cheetah.cat/chee-erp/go-flink-inator/api"
)
func main() {
apiClient := api.NewAPIClient()
magdeburgHub, err := apiClient.GetHub(52.1303067, 11.6097666)
if err != nil {
panic(err)
}
fmt.Println(magdeburgHub)
categories, err := apiClient.DiscoverCategories(*magdeburgHub.Slug)
if err != nil {
panic(err)
}
for _, cat := range categories.Categories.Categories {
fmt.Println(*cat.Id, *cat.Slug, cat.Name)
if cat.SubCategories != nil {
productCats, err := api.NewAPIClient().DiscoverCategoriesSpecific(*magdeburgHub.Slug, *cat.Slug)
if err != nil {
panic(err)
}
for _, subcat := range productCats.Categories {
fmt.Println("\t", *subcat.Id, *subcat.Slug, subcat.Name)
for _, product := range *subcat.Products.Products {
fmt.Println("\t\t", *product.Id, product.Name, product.Price, product.Quantity, product.BasePrice, product.SKU, product.Thumbnail)
}
}
}
}
}
Loading…
Cancel
Save