feat(indexers): test API from settings (#829)

* refactor(indexers): test api clients

* feat(indexers): test api connection

* fix(indexers): api client tests

* refactor: indexer api clients

* feat: add Toasts for indexer api tests

* fix: failing red tests
This commit is contained in:
ze0s 2023-04-15 23:34:27 +02:00 committed by GitHub
parent fb9dcc23a0
commit f3cfeed8cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 475 additions and 191 deletions

View file

@ -18,6 +18,7 @@ type indexerService interface {
GetAll() ([]*domain.IndexerDefinition, error)
GetTemplates() ([]domain.IndexerDefinition, error)
Delete(ctx context.Context, id int) error
TestApi(ctx context.Context, req domain.IndexerTestApiRequest) error
}
type indexerHandler struct {
@ -41,6 +42,7 @@ func (h indexerHandler) Routes(r chi.Router) {
r.Get("/", h.getAll)
r.Get("/options", h.list)
r.Delete("/{indexerID}", h.delete)
r.Post("/{id}/api/test", h.testApi)
}
func (h indexerHandler) getSchema(w http.ResponseWriter, r *http.Request) {
@ -62,6 +64,7 @@ func (h indexerHandler) store(w http.ResponseWriter, r *http.Request) {
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
@ -81,6 +84,7 @@ func (h indexerHandler) update(w http.ResponseWriter, r *http.Request) {
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
@ -132,3 +136,39 @@ func (h indexerHandler) list(w http.ResponseWriter, r *http.Request) {
h.encoder.StatusResponse(ctx, w, indexers, http.StatusOK)
}
func (h indexerHandler) testApi(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
idParam = chi.URLParam(r, "id")
req domain.IndexerTestApiRequest
)
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
h.encoder.Error(w, err)
return
}
id, err := strconv.Atoi(idParam)
if err != nil {
h.encoder.Error(w, err)
return
}
if req.IndexerId == 0 {
req.IndexerId = id
}
if err := h.service.TestApi(ctx, req); err != nil {
h.encoder.Error(w, err)
return
}
res := struct {
Message string `json:"message"`
}{
Message: "Indexer api test OK",
}
h.encoder.StatusResponse(ctx, w, res, http.StatusOK)
}