MCPcopy Create free account
hub / github.com/dolanor/rip / handleCreate

Function handleCreate

handler.go:524–563  ·  view source on GitHub ↗
(method, urlPath string, f createFunc[Ent], options *RouteOptions)

Source from the content-addressed store, hash-verified

522}
523
524func handleCreate[Ent any](method, urlPath string, f createFunc[Ent], options *RouteOptions) http.HandlerFunc {
525 return func(w http.ResponseWriter, r *http.Request) {
526 accept, err := contentNegociateBestHeaderValue(r.Header, "Accept", options.codecs.OrderedMimeTypes)
527 if err != nil {
528 writeError(w, accept, fmt.Errorf("bad accept header format: %w", err), options)
529 return
530 }
531
532 if r.Method != method {
533 writeError(w, accept, fmt.Errorf("method not allowed: %s", r.Method), options)
534 return
535 }
536
537 contentType, err := contentNegociateBestHeaderValue(r.Header, "Content-Type", options.codecs.OrderedMimeTypes)
538 if err != nil {
539 writeError(w, accept, fmt.Errorf("bad content type header format: %w", err), options)
540 return
541 }
542
543 res, err := decode[Ent](r.Body, contentType, options)
544 if err != nil {
545 writeError(w, accept, fmt.Errorf("decode POST body: %w", err), options)
546 return
547 }
548
549 res, err = f(r.Context(), res)
550 if err != nil {
551 writeError(w, accept, fmt.Errorf("entity provider create: %w", err), options)
552 return
553 }
554
555 w.WriteHeader(http.StatusCreated)
556
557 err = encoding.AcceptEncoder(w, accept, encoding.EditOff, options.codecs).Encode(res)
558 if err != nil {
559 writeError(w, accept, fmt.Errorf("encode POST body: %w", err), options)
560 return
561 }
562 }
563}
564
565// start Handle OMIT
566

Callers 1

handleEntityWithPathFunction · 0.85

Calls 5

AcceptEncoderFunction · 0.92
writeErrorFunction · 0.85
fFunction · 0.85
EncodeMethod · 0.65

Tested by

no test coverage detected