Allow to override fetcher link by using same name

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-11-29 16:52:51 +01:00
parent f1f5b405c4
commit 4a200e12e8
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D

49
api.go
View File

@ -33,6 +33,29 @@ func buildFullURL(u *url.URL, _ error) string {
}, "/") }, "/")
} }
func catalogEntryToAPICatalogEntry(ce database.CatalogEntry) (APICatalogEntry, error) {
cm, err := storage.Catalog.GetMeta(&ce)
if err != nil {
return APICatalogEntry{}, errors.Wrap(err, "fetching catalog meta")
}
for _, l := range fetcher.Get(ce.Fetcher).Links(&ce.FetcherConfig) {
var found bool
for _, el := range ce.Links {
if l.Name == el.Name {
found = true
break
}
}
if !found {
ce.Links = append(ce.Links, l)
}
}
return APICatalogEntry{CatalogEntry: ce, CatalogMeta: *cm}, nil
}
func handleCatalogGet(w http.ResponseWriter, r *http.Request) { func handleCatalogGet(w http.ResponseWriter, r *http.Request) {
var ( var (
vars = mux.Vars(r) vars = mux.Vars(r)
@ -45,20 +68,15 @@ func handleCatalogGet(w http.ResponseWriter, r *http.Request) {
return return
} }
cm, err := storage.Catalog.GetMeta(&ce) ae, err := catalogEntryToAPICatalogEntry(ce)
if err != nil { if err != nil {
log.WithError(err).Error("Unable to fetch catalog meta") log.WithError(err).Error("Unable to fetch catalog data")
http.Error(w, "Unable to fetch catalog meta", http.StatusInternalServerError) http.Error(w, "Unable to fetch catalog data", http.StatusInternalServerError)
return return
} }
ce.Links = append(
ce.Links,
fetcher.Get(ce.Fetcher).Links(&ce.FetcherConfig)...,
)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err = json.NewEncoder(w).Encode(APICatalogEntry{CatalogEntry: ce, CatalogMeta: *cm}); err != nil { if err = json.NewEncoder(w).Encode(ae); err != nil {
log.WithError(err).Error("Unable to encode catalog entry") log.WithError(err).Error("Unable to encode catalog entry")
http.Error(w, "Unable to encode catalog meta", http.StatusInternalServerError) http.Error(w, "Unable to encode catalog meta", http.StatusInternalServerError)
return return
@ -94,19 +112,14 @@ func handleCatalogList(w http.ResponseWriter, r *http.Request) {
for i := range configFile.Catalog { for i := range configFile.Catalog {
ce := configFile.Catalog[i] ce := configFile.Catalog[i]
cm, err := storage.Catalog.GetMeta(&ce) ae, err := catalogEntryToAPICatalogEntry(ce)
if err != nil { if err != nil {
log.WithError(err).Error("Unable to fetch catalog meta") log.WithError(err).Error("Unable to fetch catalog data")
http.Error(w, "Unable to fetch catalog meta", http.StatusInternalServerError) http.Error(w, "Unable to fetch catalog data", http.StatusInternalServerError)
return return
} }
ce.Links = append( out[i] = ae
ce.Links,
fetcher.Get(ce.Fetcher).Links(&ce.FetcherConfig)...,
)
out[i] = APICatalogEntry{CatalogEntry: ce, CatalogMeta: *cm}
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")