mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
A path segment is a VALUE. Which router matched the route is not, and 140 handler bodies named gorilla/mux to read one — so the router was braided into every piece of business logic that addresses a resource by id. coretypes now owns the read (routevalue.go): Param for a path segment, SetParams for the test that has no router, RoutePath for the route TEMPLATE the audit record must be keyed on. One reader, one writer, one place that knows who filled them. PathParam — the authz extractor — reads through the same Param, so the gate and the handler cannot disagree about which segment they mean. Converted end to end, both ends of one resource plus the shared plumbing: llmpricingrule's handler, errortracking's handler AND its test injector, the SelfAccess gate's id, and audit's route template. Nine tests, and they are not vacuous: stubbing Param to "" turns eight of them red. Param answers for a PATH segment only. A query value stays r.URL.Query().Get — conflating the two is how a route starts reading the wrong half of the URL, and TestParamIsNotAQueryRead pins it. RoutePath answers "" with no matched route where the old mux.CurrentRoute(req).GetPathTemplate() dereferenced nil. Route table unchanged: 367 method+path pairs before and after (353 typed ops, 11 hatches, 3 probes). No registration was touched — that flip is atomic across the whole tree and is the next commit, not this one. Co-authored-by: Hanzo Dev <[email protected]>
158 lines
3.9 KiB
Go
158 lines
3.9 KiB
Go
package impllmpricingrule
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"github.com/hanzoai/o11y/pkg/http/binding"
|
|
"github.com/hanzoai/o11y/pkg/http/render"
|
|
"github.com/hanzoai/o11y/pkg/modules/llmpricingrule"
|
|
"github.com/hanzoai/o11y/pkg/types/authtypes"
|
|
"github.com/hanzoai/o11y/pkg/types/coretypes"
|
|
"github.com/hanzoai/o11y/pkg/types/llmpricingruletypes"
|
|
"github.com/hanzoai/o11y/pkg/valuer"
|
|
)
|
|
|
|
const maxLimit = 100
|
|
|
|
type handler struct {
|
|
module llmpricingrule.Module
|
|
}
|
|
|
|
func NewHandler(module llmpricingrule.Module) llmpricingrule.Handler {
|
|
return &handler{module: module}
|
|
}
|
|
|
|
// List handles GET /api/v1/llm_pricing_rules.
|
|
func (h *handler) List(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
orgID := valuer.MustNewUUID(claims.OrgID)
|
|
|
|
var q llmpricingruletypes.ListPricingRulesQuery
|
|
if err := binding.Query.BindQuery(r.URL.Query(), &q); err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
if q.Limit <= 0 {
|
|
q.Limit = 20
|
|
} else if q.Limit > maxLimit {
|
|
q.Limit = maxLimit
|
|
}
|
|
if q.Offset < 0 {
|
|
render.Error(rw, errors.Newf(errors.TypeInvalidInput, llmpricingruletypes.ErrCodePricingRuleInvalidInput, "offset must be a non-negative integer"))
|
|
return
|
|
}
|
|
|
|
rules, total, err := h.module.List(ctx, orgID, q.Offset, q.Limit, q.Search, q.IsOverride)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusOK, llmpricingruletypes.NewGettableLLMPricingRulesFromLLMPricingRules(rules, total, q.Offset, q.Limit))
|
|
}
|
|
|
|
// Get handles GET /api/v1/llm_pricing_rules/{id}.
|
|
func (h *handler) Get(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
orgID := valuer.MustNewUUID(claims.OrgID)
|
|
|
|
id, err := ruleIDFromPath(r)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
rule, err := h.module.Get(ctx, orgID, id)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusOK, rule)
|
|
}
|
|
|
|
func (h *handler) CreateOrUpdate(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
orgID := valuer.MustNewUUID(claims.OrgID)
|
|
|
|
req := new(llmpricingruletypes.UpdatableLLMPricingRules)
|
|
if err := binding.JSON.BindBody(r.Body, req); err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
err = h.module.CreateOrUpdate(ctx, orgID, claims.Email, req.Rules)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusNoContent, nil)
|
|
}
|
|
|
|
// Delete handles DELETE /api/v1/llm_pricing_rules/{id}.
|
|
func (h *handler) Delete(rw http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
claims, err := authtypes.ClaimsFromContext(ctx)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
orgID := valuer.MustNewUUID(claims.OrgID)
|
|
|
|
id, err := ruleIDFromPath(r)
|
|
if err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
if err := h.module.Delete(ctx, orgID, id); err != nil {
|
|
render.Error(rw, err)
|
|
return
|
|
}
|
|
|
|
render.Success(rw, http.StatusNoContent, nil)
|
|
}
|
|
|
|
// ruleIDFromPath extracts and validates the {id} path segment. The read goes
|
|
// through coretypes.Param — the ONE route-value reader — so this handler names
|
|
// the value it needs and not the router that matched it.
|
|
func ruleIDFromPath(r *http.Request) (valuer.UUID, error) {
|
|
id, err := valuer.NewUUID(coretypes.Param(r, "id"))
|
|
if err != nil {
|
|
return valuer.UUID{}, errors.Wrapf(err, errors.TypeInvalidInput, llmpricingruletypes.ErrCodePricingRuleInvalidInput, "id is not a valid uuid")
|
|
}
|
|
return id, nil
|
|
}
|