package api import ( "net/http" "strconv" "strings" "forge.cadoles.com/Cadoles/emissary/internal/datastore" "github.com/go-chi/chi/v5" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/api" "gitlab.com/wpetit/goweb/logger" ) const ( ErrCodeUnknownError api.ErrorCode = "unknown-error" ErrCodeNotFound api.ErrorCode = "not-found" ErrCodeInvalidSignature api.ErrorCode = "invalid-signature" ErrCodeConflict api.ErrorCode = "conflict" ErrCodeMultipleResults api.ErrorCode = "multiple-results" ErrCodeAlreadyClaimed api.ErrorCode = "already-claimed" ) func getAgentID(w http.ResponseWriter, r *http.Request) (datastore.AgentID, bool) { rawAgentID := chi.URLParam(r, "agentID") agentID, err := strconv.ParseInt(rawAgentID, 10, 64) if err != nil { logger.Error(r.Context(), "could not parse agent id", logger.CapturedE(errors.WithStack(err))) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return 0, false } return datastore.AgentID(agentID), true } func getSpecID(w http.ResponseWriter, r *http.Request) (datastore.SpecID, bool) { rawSpecID := chi.URLParam(r, "specID") specID, err := strconv.ParseInt(rawSpecID, 10, 64) if err != nil { err = errors.WithStack(err) logger.Error(r.Context(), "could not parse spec id", logger.CapturedE(err)) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return 0, false } return datastore.SpecID(specID), true } func getTenantID(w http.ResponseWriter, r *http.Request) (datastore.TenantID, bool) { rawTenantID := chi.URLParam(r, "tenantID") tenantID, err := datastore.ParseTenantID(rawTenantID) if err != nil { err = errors.WithStack(err) logger.Error(r.Context(), "could not parse tenant id", logger.CapturedE(err)) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return "", false } return tenantID, true } func getIntQueryParam(w http.ResponseWriter, r *http.Request, param string, defaultValue int64) (int64, bool) { rawValue := r.URL.Query().Get(param) if rawValue != "" { value, err := strconv.ParseInt(rawValue, 10, 64) if err != nil { err = errors.WithStack(err) logger.Error(r.Context(), "could not parse int param", logger.F("param", param), logger.CapturedE(err)) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return 0, false } return value, true } return defaultValue, true } func getStringSliceValues(w http.ResponseWriter, r *http.Request, param string, defaultValue []string) ([]string, bool) { rawValue := r.URL.Query().Get(param) if rawValue != "" { values := strings.Split(rawValue, ",") return values, true } return defaultValue, true } func getIntSliceValues(w http.ResponseWriter, r *http.Request, param string, defaultValue []int64) ([]int64, bool) { rawValue := r.URL.Query().Get(param) if rawValue != "" { rawValues := strings.Split(rawValue, ",") values := make([]int64, 0, len(rawValues)) for _, rv := range rawValues { value, err := strconv.ParseInt(rv, 10, 64) if err != nil { err = errors.WithStack(err) logger.Error(r.Context(), "could not parse int slice param", logger.F("param", param), logger.CapturedE(err)) api.ErrorResponse(w, http.StatusBadRequest, api.ErrCodeMalformedRequest, nil) return nil, false } values = append(values, value) } return values, true } return defaultValue, true }