package client import ( "context" "encoding/json" "fmt" "io" "net/http" "git.espin.casa/albert/mercadona/pkg/types/mercadona" ) const ( BaseURL string = "https://tienda.mercadona.es" ) type IMercadona interface { // Get Mercadona products based on barcode scanned id GetMercadonaProduct(ctx context.Context, id string) (*mercadona.Product, error) } type client struct{ url string } // GetMercadonaProduct implements IMercadona. func (c client) GetMercadonaProduct(ctx context.Context, id string) (*mercadona.Product, error) { // product holder product := &mercadona.Product{} // crate url endpoint url := fmt.Sprintf("%s/api/products/%s/xselling/?lang=es&wh=bcn1&exclude=", BaseURL, id) // create http Get Request resp, err := http.Get(url) // check if error if err != nil { return nil, err } // close body response defer resp.Body.Close() // read body response body, err := io.ReadAll(resp.Body) // check if error on read body if err != nil { return nil, err } // unmarshal json body response if err := json.Unmarshal(body, product); err != nil { return nil, err } return product, nil } func NewClient() IMercadona { url := BaseURL return client{ url: url, } }