{"modulePath":"github.com/oapi-codegen/oapi-codegen-exp/experimental","version":"v0.0.0-20260413235603-d24950bd28cc","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package treefarm\n\nPackage treefarm provides an example of how to handle OpenAPI callbacks. We create a server which plants trees. The client asks the server to plant a tree and requests a callback when the planting is done.\n\nThe server program will wait 1-5 seconds before notifying the client that a tree has been planted.\n\nYou can run the example by running these two commands in parallel go run ./server --port 8080 go run ./client --server [http://localhost:8080](http://localhost:8080)\n\n## Functions\n\n```go\nfunc GetOpenAPISpecJSON() ([]byte, error)\n```\nGetOpenAPISpecJSON returns the raw OpenAPI spec as JSON bytes.\n\n```go\nfunc Handler(si ServerInterface) http.Handler\n```\nHandler creates http.Handler with routing matching OpenAPI spec.\n\n```go\nfunc HandlerFromMux(si ServerInterface, m ServeMux) http.Handler\n```\nHandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.\n\n```go\nfunc HandlerFromMuxWithBaseURL(si ServerInterface, m ServeMux, baseURL string) http.Handler\n```\nHandlerFromMuxWithBaseURL creates http.Handler with routing and a base URL.\n\n```go\nfunc HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.Handler\n```\nHandlerWithOptions creates http.Handler with additional options.\n\n```go\nfunc NewTreePlantedCallbackRequest(targetURL string, body TreePlantedJSONRequestBody) (*http.Request, error)\n```\nNewTreePlantedCallbackRequest creates a POST request for the callback with application/json body\n\n```go\nfunc NewTreePlantedCallbackRequestWithBody(targetURL string, contentType string, body io.Reader) (*http.Request, error)\n```\nNewTreePlantedCallbackRequestWithBody creates a POST request for the callback with any body\n\n```go\nfunc TreePlantedCallbackHandler(si CallbackReceiverInterface, errHandler func(w http.ResponseWriter, r *http.Request, err error), middlewares ...CallbackReceiverMiddlewareFunc) http.Handler\n```\nTreePlantedCallbackHandler returns an http.Handler for the TreePlanted callback. The caller is responsible for registering this handler at the appropriate path.\n\n## Types\n\n```go\ntype CallbackHttpError[E any] struct {\n\tStatusCode int\n\tBody       E\n\tRawBody    []byte\n}\n```\nCallbackHttpError represents an HTTP error response from the callback. The type parameter E is the type of the parsed error body.\n\n```go\nfunc (e *CallbackHttpError[E]) Error() string\n```\n\n```go\ntype CallbackInitiator struct {\n\t// Doer for performing requests, typically a *http.Client with any\n\t// customized settings, such as certificate chains.\n\tClient HttpRequestDoer\n\n\t// A list of callbacks for modifying requests which are generated before sending over\n\t// the network.\n\tRequestEditors []RequestEditorFn\n}\n```\nCallbackInitiator sends callback requests to target URLs. Unlike Client, it has no stored base URL — the full target URL is provided per-call.\n\n```go\nfunc NewCallbackInitiator(opts ...CallbackInitiatorOption) (*CallbackInitiator, error)\n```\nNewCallbackInitiator creates a new CallbackInitiator with reasonable defaults.\n\n```go\nfunc (p *CallbackInitiator) TreePlanted(ctx context.Context, targetURL string, body TreePlantedJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)\n```\nTreePlanted sends a POST callback request with application/json body\n\n```go\nfunc (p *CallbackInitiator) TreePlantedWithBody(ctx context.Context, targetURL string, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error)\n```\nTreePlantedWithBody sends a POST callback request Tree planting result notification\n\n```go\ntype CallbackInitiatorInterface interface {\n\t// TreePlantedWithBody sends a POST callback request\n\tTreePlantedWithBody(ctx context.Context, targetURL string, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error)\n\tTreePlanted(ctx context.Context, targetURL string, body TreePlantedJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)\n}\n```\nCallbackInitiatorInterface is the interface specification for the callback initiator.\n\n```go\ntype CallbackInitiatorOption func(*CallbackInitiator) error\n```\nCallbackInitiatorOption allows setting custom parameters during construction.\n\n```go\nfunc WithCallbackHTTPClient(doer HttpRequestDoer) CallbackInitiatorOption\n```\nWithCallbackHTTPClient allows overriding the default Doer, which is automatically created using http.Client. This is useful for tests.\n\n```go\nfunc WithCallbackRequestEditorFn(fn RequestEditorFn) CallbackInitiatorOption\n```\nWithCallbackRequestEditorFn allows setting up a callback function, which will be called right before sending the request. This can be used to mutate the request.\n\n```go\ntype CallbackReceiverInterface interface {\n\t// Tree planting result notification\n\t// HandleTreePlantedCallback handles the POST callback request.\n\tHandleTreePlantedCallback(w http.ResponseWriter, r *http.Request)\n}\n```\nCallbackReceiverInterface represents handlers for receiving callback requests.\n\n```go\ntype CallbackReceiverMiddlewareFunc func(http.Handler) http.Handler\n```\nCallbackReceiverMiddlewareFunc is a middleware function for callback receiver handlers.\n\n```go\ntype Error struct {\n\t// Error code\n\tCode int32 `form:\"code\" json:\"code\"`\n\t// Error message\n\tMessage string `form:\"message\" json:\"message\"`\n}\n```\n\\#/components/schemas/Error\n\n```go\nfunc (s *Error) ApplyDefaults()\n```\nApplyDefaults sets default values for fields that are nil.\n\n```go\ntype HttpRequestDoer interface {\n\tDo(req *http.Request) (*http.Response, error)\n}\n```\nHttpRequestDoer performs HTTP requests. The standard http.Client implements this interface.\n\n```go\ntype InvalidParamFormatError struct {\n\tParamName string\n\tErr       error\n}\n```\nInvalidParamFormatError is returned when a parameter has an invalid format.\n\n```go\nfunc (e *InvalidParamFormatError) Error() string\n```\n\n```go\nfunc (e *InvalidParamFormatError) Unwrap() error\n```\n\n```go\ntype MiddlewareFunc func(http.Handler) http.Handler\n```\nMiddlewareFunc is a middleware function type.\n\n```go\ntype RequestEditorFn func(ctx context.Context, req *http.Request) error\n```\nRequestEditorFn is the function signature for the RequestEditor callback function. It may already be defined if client code is also generated; this is a compatible redeclaration.\n\n```go\ntype RequiredHeaderError struct {\n\tParamName string\n\tErr       error\n}\n```\nRequiredHeaderError is returned when a required header is missing.\n\n```go\nfunc (e *RequiredHeaderError) Error() string\n```\n\n```go\nfunc (e *RequiredHeaderError) Unwrap() error\n```\n\n```go\ntype RequiredParamError struct {\n\tParamName string\n}\n```\nRequiredParamError is returned when a required parameter is missing.\n\n```go\nfunc (e *RequiredParamError) Error() string\n```\n\n```go\ntype ServeMux interface {\n\tHandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))\n\tServeHTTP(w http.ResponseWriter, r *http.Request)\n}\n```\nServeMux is an abstraction of http.ServeMux.\n\n```go\ntype ServerInterface interface {\n\t// Request a tree planting\n\t// (POST /api/plant_tree)\n\tPlantTree(w http.ResponseWriter, r *http.Request)\n}\n```\nServerInterface represents all server handlers.\n\n```go\ntype ServerInterfaceWrapper struct {\n\tHandler            ServerInterface\n\tHandlerMiddlewares []MiddlewareFunc\n\tErrorHandlerFunc   func(w http.ResponseWriter, r *http.Request, err error)\n}\n```\nServerInterfaceWrapper converts HTTP requests to parameters.\n\n```go\nfunc (siw *ServerInterfaceWrapper) PlantTree(w http.ResponseWriter, r *http.Request)\n```\nPlantTree operation middleware\n\n```go\ntype SimpleCallbackInitiator struct {\n\t*CallbackInitiator\n}\n```\nSimpleCallbackInitiator wraps CallbackInitiator with typed responses for operations that have unambiguous response types. Methods return the success type directly, and HTTP errors are returned as \\*CallbackHttpError\\[E] where E is the error type.\n\n```go\nfunc NewSimpleCallbackInitiator(opts ...CallbackInitiatorOption) (*SimpleCallbackInitiator, error)\n```\nNewSimpleCallbackInitiator creates a new SimpleCallbackInitiator which wraps a CallbackInitiator.\n\n```go\ntype StdHTTPServerOptions struct {\n\tBaseURL          string\n\tBaseRouter       ServeMux\n\tMiddlewares      []MiddlewareFunc\n\tErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)\n}\n```\nStdHTTPServerOptions configures the StdHTTP server.\n\n```go\ntype TooManyValuesForParamError struct {\n\tParamName string\n\tCount     int\n}\n```\nTooManyValuesForParamError is returned when a parameter has too many values.\n\n```go\nfunc (e *TooManyValuesForParamError) Error() string\n```\n\n```go\ntype Tree struct {\n\t// Where to plant the tree (e.g. \"north meadow\")\n\tLocation string `form:\"location\" json:\"location\"`\n\t// What kind of tree to plant (e.g. \"oak\")\n\tKind string `form:\"kind\" json:\"kind\"`\n}\n```\n\\#/components/schemas/Tree A tree to be planted\n\n```go\nfunc (s *Tree) ApplyDefaults()\n```\nApplyDefaults sets default values for fields that are nil.\n\n```go\ntype TreePlantedJSONRequestBody = TreePlantingResult\n```\n\n```go\ntype TreePlantingRequest struct {\n\t// Where to plant the tree (e.g. \"north meadow\")\n\tLocation string `form:\"location\" json:\"location\"`\n\t// What kind of tree to plant (e.g. \"oak\")\n\tKind string `form:\"kind\" json:\"kind\"`\n\t// URL to receive the planting result callback\n\tCallbackURL string `form:\"callbackUrl\" json:\"callbackUrl\"`\n}\n```\n\\#/components/schemas/TreePlantingRequest A tree planting request, combining the tree details with a callback URL for completion notification.\n\n```go\nfunc (s *TreePlantingRequest) ApplyDefaults()\n```\nApplyDefaults sets default values for fields that are nil.\n\n```go\ntype TreePlantingResult struct {\n\t// Where to plant the tree (e.g. \"north meadow\")\n\tLocation string `form:\"location\" json:\"location\"`\n\t// What kind of tree to plant (e.g. \"oak\")\n\tKind string `form:\"kind\" json:\"kind\"`\n\t// Unique identifier for this planting request\n\tID UUID `form:\"id\" json:\"id\"`\n\t// Whether the tree was successfully planted\n\tSuccess bool `form:\"success\" json:\"success\"`\n}\n```\n\\#/components/schemas/TreePlantingResult The result of a tree planting operation, sent via callback\n\n```go\nfunc (s *TreePlantingResult) ApplyDefaults()\n```\nApplyDefaults sets default values for fields that are nil.\n\n```go\ntype TreeWithID struct {\n\t// Where to plant the tree (e.g. \"north meadow\")\n\tLocation string `form:\"location\" json:\"location\"`\n\t// What kind of tree to plant (e.g. \"oak\")\n\tKind string `form:\"kind\" json:\"kind\"`\n\t// Unique identifier for this planting request\n\tID UUID `form:\"id\" json:\"id\"`\n}\n```\n\\#/components/schemas/TreeWithId A tree with a server-assigned identifier\n\n```go\nfunc (s *TreeWithID) ApplyDefaults()\n```\nApplyDefaults sets default values for fields that are nil.\n\n```go\ntype UUID = uuid.UUID\n```\n\n```go\ntype UnescapedCookieParamError struct {\n\tParamName string\n\tErr       error\n}\n```\nUnescapedCookieParamError is returned when a cookie parameter cannot be unescaped.\n\n```go\nfunc (e *UnescapedCookieParamError) Error() string\n```\n\n```go\nfunc (e *UnescapedCookieParamError) Unwrap() error\n```\n\n```go\ntype UnmarshalingParamError struct {\n\tParamName string\n\tErr       error\n}\n```\nUnmarshalingParamError is returned when a parameter cannot be unmarshaled.\n\n```go\nfunc (e *UnmarshalingParamError) Error() string\n```\n\n```go\nfunc (e *UnmarshalingParamError) Unwrap() error\n```\n\n","path":"github.com/oapi-codegen/oapi-codegen-exp/experimental/examples/callback","name":"treefarm","synopsis":"Package treefarm provides an example of how to handle OpenAPI callbacks.","isRedistributable":true}
