Files
minstrel/internal/api/admin_provider_sources.go
T
bvandeusenandClaude Opus 5.5 17a9052acb
release / web (push) Successful in 1m16s
release / go (push) Successful in 1m49s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 3m56s
release / android (push) Successful in 4m8s
release / Build signed APK (push) Successful in 4m29s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 13s
release / Verify release artifacts (tag releases only) (push) Skipped
refactor(api): the cover-source and tag-source admin cards share their handlers (#8002)
admin_cover_sources.go and admin_tag_sources.go were the same list,
update and test handlers over two settings services, each with its own
copy of the response mapping. With both services now a
providers.Service, admin_provider_sources.go holds the handlers and wire
types once, and each family's routes pass their own service. The wire
shape is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 13:01:48 -05:00

133 lines
4.3 KiB
Go

package api
import (
"encoding/json"
"errors"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
)
// The admin cards for outside sources, Cover sources and Tag sources, run
// on these. Each family's handlers pass their own providers.Service, so the
// two cards answer in one shape and share no rows.
// providerSourceResp is one provider in a sources list.
type providerSourceResp struct {
ID string `json:"id"`
DisplayName string `json:"display_name"`
RequiresAPIKey bool `json:"requires_api_key"`
Supports []string `json:"supports"`
Enabled bool `json:"enabled"`
APIKeySet bool `json:"api_key_set"`
DisplayOrder int32 `json:"display_order"`
Testable bool `json:"testable"`
}
func providerSourceRespFrom(info providers.Info) providerSourceResp {
supports := info.Supports
if supports == nil {
supports = []string{}
}
return providerSourceResp{
ID: info.ID,
DisplayName: info.DisplayName,
RequiresAPIKey: info.RequiresAPIKey,
Supports: supports,
Enabled: info.Enabled,
APIKeySet: info.APIKeySet,
DisplayOrder: info.DisplayOrder,
Testable: info.Testable,
}
}
type providerSourcesListResp struct {
Providers []providerSourceResp `json:"providers"`
SourcesVersion int32 `json:"sources_version"`
}
// updateProviderSourceReq is the PATCH body. A nil field is left as it
// is; an empty api_key clears the key.
type updateProviderSourceReq struct {
Enabled *bool `json:"enabled,omitempty"`
APIKey *string `json:"api_key,omitempty"`
}
type updateProviderSourceResp struct {
providerSourceResp
VersionBumped bool `json:"version_bumped"`
}
// testProviderSourceResp is a Test-button result. It answers 200 whether
// or not the test passed: the result is data, not an error.
type testProviderSourceResp struct {
OK bool `json:"ok"`
DurationMs int64 `json:"duration_ms,omitempty"`
Error string `json:"error,omitempty"`
}
var errNoSuchProvider = apierror.New(http.StatusNotFound, "not_found", "no such provider")
// listProviderSources answers GET /api/admin/{family}-sources.
func listProviderSources(w http.ResponseWriter, svc *providers.Service) {
infos := svc.ListProviderInfo()
out := providerSourcesListResp{
Providers: make([]providerSourceResp, 0, len(infos)),
SourcesVersion: svc.CurrentVersion(),
}
for _, info := range infos {
out.Providers = append(out.Providers, providerSourceRespFrom(info))
}
writeJSON(w, http.StatusOK, out)
}
// updateProviderSource answers PATCH /api/admin/{family}-sources/{provider_id}
// with the provider as it now stands.
func (h *handlers) updateProviderSource(w http.ResponseWriter, r *http.Request, svc *providers.Service) {
id := chi.URLParam(r, "provider_id")
var body updateProviderSourceReq
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, apierror.BadRequest("bad_body", "invalid JSON"))
return
}
bumped, err := svc.UpdateProvider(r.Context(), id, providers.UpdatePatch{Enabled: body.Enabled, APIKey: body.APIKey})
if err != nil {
if errors.Is(err, providers.ErrProviderNotFound) {
writeErr(w, errNoSuchProvider)
return
}
h.logger.Error("admin: update provider source", "id", id, "err", err)
writeErr(w, apierror.InternalMsg("update failed", err))
return
}
var found providerSourceResp
for _, info := range svc.ListProviderInfo() {
if info.ID == id {
found = providerSourceRespFrom(info)
break
}
}
writeJSON(w, http.StatusOK, updateProviderSourceResp{providerSourceResp: found, VersionBumped: bumped})
}
// testProviderSource answers POST /api/admin/{family}-sources/{provider_id}/test.
func testProviderSource(w http.ResponseWriter, r *http.Request, svc *providers.Service) {
id := chi.URLParam(r, "provider_id")
start := time.Now()
err := svc.TestProvider(r.Context(), id)
duration := time.Since(start).Milliseconds()
switch {
case err == nil:
writeJSON(w, http.StatusOK, testProviderSourceResp{OK: true, DurationMs: duration})
case errors.Is(err, providers.ErrProviderNotFound):
writeErr(w, errNoSuchProvider)
default:
// Not testable, or the test failed.
writeJSON(w, http.StatusOK, testProviderSourceResp{OK: false, DurationMs: duration, Error: err.Error()})
}
}