MCPcopy
hub / github.com/danielgtaylor/huma / ExampleAdapter_handle

Function ExampleAdapter_handle

api_test.go:28–70  ·  view source on GitHub ↗

ExampleAdapter_handle demonstrates how to use the adapter directly instead of using the `huma.Register` convenience function to add a new operation and handler to the API. Note that you are responsible for defining all of the operation details, including the parameter and response definitions & sch

()

Source from the content-addressed store, hash-verified

26// Note that you are responsible for defining all of the operation details,
27// including the parameter and response definitions & schemas.
28func ExampleAdapter_handle() {
29 // Create an adapter for your chosen router.
30 adapter := NewExampleAdapter()
31
32 // Register an operation with a custom handler.
33 adapter.Handle(&huma.Operation{
34 OperationID: "example-operation",
35 Method: "GET",
36 Path: "/example/{name}",
37 Summary: "Example operation",
38 Parameters: []*huma.Param{
39 {
40 Name: "name",
41 In: "path",
42 Description: "Name to return",
43 Required: true,
44 Schema: &huma.Schema{
45 Type: "string",
46 },
47 },
48 },
49 Responses: map[string]*huma.Response{
50 "200": {
51 Description: "OK",
52 Content: map[string]*huma.MediaType{
53 "text/plain": {
54 Schema: &huma.Schema{
55 Type: "string",
56 },
57 },
58 },
59 },
60 },
61 }, func(ctx huma.Context) {
62 // Get the `name` path parameter.
63 name := ctx.Param("name")
64
65 // Set the response content type, status code, and body.
66 ctx.SetHeader("Content-Type", "text/plain; charset=utf-8")
67 ctx.SetStatus(http.StatusOK)
68 ctx.BodyWriter().Write([]byte("Hello, " + name))
69 })
70}
71
72func TestContextValue(t *testing.T) {
73 _, api := humatest.New(t)

Callers

nothing calls this directly

Calls 6

HandleMethod · 0.65
ParamMethod · 0.65
SetHeaderMethod · 0.65
SetStatusMethod · 0.65
BodyWriterMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…