mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 21:33:02 +01:00
ProductBarcode: define Barcodespider API key using env variables
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
"github.com/hay-kot/httpkit/errchain"
|
"github.com/hay-kot/httpkit/errchain"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/sysadminsmedia/homebox/backend/internal/data/repo"
|
"github.com/sysadminsmedia/homebox/backend/internal/data/repo"
|
||||||
|
"github.com/sysadminsmedia/homebox/backend/internal/sys/config"
|
||||||
"github.com/sysadminsmedia/homebox/backend/internal/web/adapters"
|
"github.com/sysadminsmedia/homebox/backend/internal/web/adapters"
|
||||||
"github.com/yeqown/go-qrcode/v2"
|
"github.com/yeqown/go-qrcode/v2"
|
||||||
"github.com/yeqown/go-qrcode/writer/standard"
|
"github.com/yeqown/go-qrcode/writer/standard"
|
||||||
@@ -192,7 +194,7 @@ type BARCODESPIDER_COMResponse struct {
|
|||||||
// @Success 200 {object} repo.ItemCreate
|
// @Success 200 {object} repo.ItemCreate
|
||||||
// @Router /v1/getproductfromean [GET]
|
// @Router /v1/getproductfromean [GET]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
func (ctrl *V1Controller) HandleProductSearchEAN() errchain.HandlerFunc {
|
func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) errchain.HandlerFunc {
|
||||||
type query struct {
|
type query struct {
|
||||||
// 4,296 characters is the maximum length of a QR code
|
// 4,296 characters is the maximum length of a QR code
|
||||||
EAN string `schema:"productEAN" validate:"required,max=4296"`
|
EAN string `schema:"productEAN" validate:"required,max=4296"`
|
||||||
@@ -267,8 +269,13 @@ func (ctrl *V1Controller) HandleProductSearchEAN() errchain.HandlerFunc {
|
|||||||
log.Error().Msg("Can not retrieve product from upcitemdb.com" + err.Error())
|
log.Error().Msg("Can not retrieve product from upcitemdb.com" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Barcode spider: Freetoken: 43866b1fa5d558a2bd12
|
// Barcode spider implementation
|
||||||
barcodespider := func(iEan string) ([]BarcodeProduct, error) {
|
barcodespider := func(tokenAPI string, iEan string) ([]BarcodeProduct, error) {
|
||||||
|
|
||||||
|
if len(tokenAPI) == 0 {
|
||||||
|
return nil, errors.New("no api token configured for barcodespider")
|
||||||
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(
|
req, err := http.NewRequest(
|
||||||
"GET", "https://api.barcodespider.com/v1/lookup?upc="+iEan, nil)
|
"GET", "https://api.barcodespider.com/v1/lookup?upc="+iEan, nil)
|
||||||
|
|
||||||
@@ -276,7 +283,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN() errchain.HandlerFunc {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("token", "43866b1fa5d558a2bd12")
|
req.Header.Add("token", tokenAPI)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
@@ -315,9 +322,9 @@ func (ctrl *V1Controller) HandleProductSearchEAN() errchain.HandlerFunc {
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ps2, err := barcodespider(q.EAN)
|
ps2, err := barcodespider(conf.TokenBarcodespider, q.EAN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg("Can not retrieve product from barcodespider.com" + err.Error())
|
log.Error().Msg("Can not retrieve product from barcodespider.com: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge everything.
|
// Merge everything.
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ func (a *app) mountRoutes(r *chi.Mux, chain *errchain.ErrChain, repos *repo.AllR
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Get("/qrcode", chain.ToHandlerFunc(v1Ctrl.HandleGenerateQRCode(), assetMW...))
|
r.Get("/qrcode", chain.ToHandlerFunc(v1Ctrl.HandleGenerateQRCode(), assetMW...))
|
||||||
r.Get("/getproductfromean", chain.ToHandlerFunc(v1Ctrl.HandleProductSearchEAN(), userMW...))
|
r.Get("/getproductfromean", chain.ToHandlerFunc(v1Ctrl.HandleProductSearchEAN(a.conf.Barcode), userMW...))
|
||||||
r.Get(
|
r.Get(
|
||||||
"/items/{id}/attachments/{attachment_id}",
|
"/items/{id}/attachments/{attachment_id}",
|
||||||
chain.ToHandlerFunc(v1Ctrl.HandleItemAttachmentGet(), assetMW...),
|
chain.ToHandlerFunc(v1Ctrl.HandleItemAttachmentGet(), assetMW...),
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ type Config struct {
|
|||||||
Options Options `yaml:"options"`
|
Options Options `yaml:"options"`
|
||||||
LabelMaker LabelMakerConf `yaml:"labelmaker"`
|
LabelMaker LabelMakerConf `yaml:"labelmaker"`
|
||||||
Thumbnail Thumbnail `yaml:"thumbnail"`
|
Thumbnail Thumbnail `yaml:"thumbnail"`
|
||||||
|
Barcode BarcodeAPIConf `yaml:"barcode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
@@ -70,6 +71,10 @@ type LabelMakerConf struct {
|
|||||||
DynamicLength bool `yaml:"bool" conf:"default:true"`
|
DynamicLength bool `yaml:"bool" conf:"default:true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BarcodeAPIConf struct {
|
||||||
|
TokenBarcodespider string `yaml:"token_barcodespider"`
|
||||||
|
}
|
||||||
|
|
||||||
// New parses the CLI/Config file and returns a Config struct. If the file argument is an empty string, the
|
// New parses the CLI/Config file and returns a Config struct. If the file argument is an empty string, the
|
||||||
// file is not read. If the file is not empty, the file is read and the Config struct is returned.
|
// file is not read. If the file is not empty, the file is read and the Config struct is returned.
|
||||||
func New(buildstr string, description string) (*Config, error) {
|
func New(buildstr string, description string) (*Config, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user