{"modulePath":"github.com/ModelRocket/hiro","version":"v0.1.5","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package api\n\nPackage api is the hiro api helper library\n\n## Constants\n\n```go\nconst (\n\t// PrincipalTypeUser should be returned if the principal is a user\n\tPrincipalTypeUser PrincipalType = \"user\"\n\n\t// PrincipalTypeApplication should be used if the principal is an application\n\tPrincipalTypeApplication PrincipalType = \"application\"\n\n\t// CredentialTypeBasic is http 401 Basic auth\n\tCredentialTypeBasic CredentialType = \"Basic\"\n\n\t// CredentialTypeBearer is HTTP Bearer token authentication\n\tCredentialTypeBearer CredentialType = \"Bearer\"\n\n\t// CredentialTypeAPIKey is used for other api key based authentications\n\tCredentialTypeAPIKey CredentialType = \"ApiKey\"\n)\n```\n\n## Variables\n\n```go\nvar (\n\t// ErrBadRequest should be returned for a bad request or invalid parameters\n\tErrBadRequest = Errorf(\"bad request\").WithStatus(http.StatusBadRequest)\n\n\t// ErrUnauthorized is returned when authentication has failed or is necessary\n\tErrUnauthorized = Errorf(\"access denied\").WithStatus(http.StatusUnauthorized)\n\n\t// ErrForbidden should be returned when an client is authenticated but not allowed\n\tErrForbidden = Errorf(\"forbidden\").WithStatus(http.StatusForbidden)\n\n\t// ErrNotFound is returned when an object is not found\n\tErrNotFound = Errorf(\"not found\").WithStatus(http.StatusNotFound)\n\n\t// ErrConflict should be returned when there is a conflict with resources\n\tErrConflict = Errorf(\"conflict\").WithStatus(http.StatusConflict)\n\n\t// ErrServerError should be returned for internal errors\n\tErrServerError = Errorf(\"server error\").WithStatus(http.StatusInternalServerError)\n\n\t// ErrAuthUnacceptable should be returned when an authorizer could not find data to decode\n\tErrAuthUnacceptable = Errorf(\"no acceptable authorization data found\").WithCode(\"unacceptable\").WithStatus(http.StatusUnauthorized)\n\n\t// ErrNotImplemented is returned when a method is not implemented\n\tErrNotImplemented = Errorf(\"operation is not implemented\").WithCode(\"not_implemented\").WithStatus(http.StatusNotImplemented)\n\n\t// ErrTimeout is returned when a request or operation times out\n\tErrTimeout = Errorf(\"timeout\").WithStatus(http.StatusRequestTimeout)\n)\n```\n\n## Functions\n\n```go\nfunc AuthPrincipal(ctx context.Context, target interface{}) bool\n```\nAuthPrincipal the first auth principal that matches the target\n\n```go\nfunc Context(ctx context.Context) interface{}\n```\nContext returns the request context object\n\n```go\nfunc IsRequest(ctx context.Context) bool\n```\nIsRequest returns true if the context is an http.Request\n\n```go\nfunc Log(ctx context.Context) log.Interface\n```\nLog returns the logger\n\n```go\nfunc Request(ctx context.Context) (*http.Request, http.ResponseWriter)\n```\nRequest gets the reqest and response objects from the context\n\n```go\nfunc RequestHost(ctx context.Context) string\n```\nRequestHost returns the host from the current request\n\n```go\nfunc RequestVersion(r *http.Request) string\n```\nRequestVersion returns the request version in the context\n\n```go\nfunc RequirePrincipal(ctx context.Context, target interface{}, t ...PrincipalType)\n```\nRequirePrincipal panics with api.ErrUnauthorized on fail\n\n```go\nfunc SessionManager(ctx context.Context) *session.Manager\n```\nSessionManager returns the session store from the context\n\n```go\nfunc Unauthorized(r *http.Request) (interface{}, error)\n```\nUnauthorized fails auth and can used by routes in the authorizer chain as an explicit deny, this is useful when there are authorizers with optional components\n\n```go\nfunc Version(ctx context.Context) string\n```\nVersion returns the request version from the context\n\n```go\nfunc VersionMiddleware(v Versioner, header ...string) func(http.Handler) http.Handler\n```\nVersionMiddleware enforces versioning in the request path\n\n```go\nfunc Write(w http.ResponseWriter, status int, payload interface{}, headers ...http.Header) error\n```\nWrite writes the raw payload out expeting it to be bytes\n\n```go\nfunc WriteJSON(w http.ResponseWriter, status int, payload interface{}, headers ...http.Header) error\n```\nWriteJSON writes json objects\n\n```go\nfunc WriteXML(w http.ResponseWriter, status int, payload interface{}, headers ...http.Header) error\n```\nWriteXML writes an  object out as XML\n\n## Types\n\n```go\ntype AuthorizedRoute interface {\n\tRoute\n\n\t// RequireAuth returns the credential types this route requires\n\tRequireAuth() []CredentialType\n}\n```\nAuthorizedRoute is used but authorizers to signify checking authorizers\n\n```go\ntype Authorizer interface {\n\tAuthorize(r *http.Request, rt Route) (Principal, error)\n\tCredentialType() CredentialType\n}\n```\nAuthorizer is the api authorizer interface\n\n```go\ntype AuthorizerFunc func(r *http.Request, rt Route) (Principal, error)\n```\nAuthorizerFunc performs an authorization and returns a context or error on failure\n\n```go\ntype CachedRoute interface {\n\tRoute\n\n\tTimeout() time.Duration\n}\n```\nCachedRoute is a cached route interface\n\n```go\ntype Claims interface {\n\t// Set sets a value in the claims\n\tSet(key string, value interface{})\n\n\t// Get gets a value in the claims\n\tGet(key string) interface{}\n\n\t// All should return all of the claims as a map\n\tAll() map[string]interface{}\n}\n```\nClaims is a basic claims interface\n\n```go\ntype CredentialType string\n```\nCredentialType is the credential type\n\n```go\ntype Encoder interface {\n\tEncode(w io.Writer) error\n}\n```\nEncoder is a response encoder\n\n```go\ntype ErrorResponse interface {\n\terror\n\tResponder\n\n\t// Code returns the error code\n\tCode() string\n\n\t// Detail returns the detail\n\tDetail() []string\n\n\t// With error overrides the existing error if the status is greater, or sets the detail\n\tWithError(err error) ErrorResponse\n\n\t// WithMessage sets the message for the error\n\tWithMessage(format string, args ...interface{}) ErrorResponse\n\n\t// WithStatus sets the status code for the error\n\tWithStatus(status int) ErrorResponse\n\n\t// WithCode sets the error code for the response, the default is the http status\n\tWithCode(code string) ErrorResponse\n\n\t// WithDetail adds detail to the error\n\tWithDetail(detail ...interface{}) ErrorResponse\n}\n```\nErrorResponse is response with an error\n\n```go\nfunc Error(err error) ErrorResponse\n```\nError returns a new api error from the error\n\n```go\nfunc Errorf(format string, args ...interface{}) ErrorResponse\n```\nErrorf returns a new api error from the string\n\n```go\ntype HandlerFunc func(http.ResponseWriter, *http.Request) Responder\n```\nHandlerFunc is a generic handler server operations\n\n```go\ntype Option func(*Server)\n```\nOption provides the server options, these will override th defaults and any hiro instance values.\n\n```go\nfunc WithAddr(addr string) Option\n```\nWithAddr sets the listen address for the server\n\n```go\nfunc WithBasepath(base string) Option\n```\nWithBasepath sets the router basepath for the api\n\n```go\nfunc WithCORS(origin ...string) Option\n```\nWithCORS sets the cors origin and enables cors on the router\n\n```go\nfunc WithCache(ttl time.Duration) Option\n```\nWithCache enables content caching for the route\n\n```go\nfunc WithListener(l net.Listener) Option\n```\nWithListener sets the net listener for the server\n\n```go\nfunc WithLog(l log.Interface) Option\n```\nWithLog specifies a new logger\n\n```go\nfunc WithRouter(router *mux.Router) Option\n```\nWithRouter specifies the router to use\n\n```go\nfunc WithServerName(name string) Option\n```\nWithServerName specifies the server name\n\n```go\nfunc WithTracing(t bool) Option\n```\nWithTracing enables http tracing\n\n```go\nfunc WithVersion(v string) Option\n```\nWithVersion sets the specific version for the server\n\n```go\ntype Principal interface {\n\t// Type should return the principal type\n\tType() PrincipalType\n\n\t// CredentialType should return the credential type, i.e. Basic or Bearer\n\tCredentialType() CredentialType\n\n\t// Credentials should return the raw principal credentials\n\tCredentials() string\n\n\t// Returns the principal's authorization claims\n\tAuthClaims() Claims\n}\n```\nPrincipal is an authorization principal\n\n```go\ntype PrincipalType string\n```\nPrincipalType defines a principal type\n\n```go\ntype Redirector struct {\n\t*Response\n\t// contains filtered or unexported fields\n}\n```\nRedirector defines an api redirect\n\n```go\nfunc Redirect(u *url.URL) *Redirector\n```\nRedirect will set the proper redirect headers and http.StatusFound\n\n```go\nfunc (r *Redirector) WithError(err error) *Redirector\n```\n\n```go\nfunc (r *Redirector) WithQuery(vals *url.Values) *Redirector\n```\n\n```go\ntype Responder interface {\n\t// Status returns the http status\n\tStatus() int\n\n\t// Payload is the payload\n\tPayload() interface{}\n\n\t// Write writes out the payload\n\tWrite(w http.ResponseWriter) error\n}\n```\nResponder is an api response interface\n\n```go\nfunc RedirectErrIf(cond bool, u *url.URL, err error) Responder\n```\nRedirectErrIf does a conditional redirect with the error\n\n```go\nfunc RedirectIf(cond bool, u *url.URL, val ...interface{}) Responder\n```\nRedirectIf does a conditional redirect\n\n```go\ntype Response struct {\n\t// contains filtered or unexported fields\n}\n```\nResponse is the common response type\n\n```go\nfunc NewResponse(payload ...interface{}) *Response\n```\nNewResponse returns a response with defaults\n\n```go\nfunc (r *Response) Payload() interface{}\n```\nPayload returns the payload\n\n```go\nfunc (r *Response) Status() int\n```\nStatus returns the status\n\n```go\nfunc (r *Response) WithHeader(key string, value string) *Response\n```\nWithHeader adds headers to the request\n\n```go\nfunc (r *Response) WithStatus(status int) *Response\n```\nWithStatus sets the status\n\n```go\nfunc (r *Response) WithWriter(w WriterFunc) *Response\n```\nWithWriter sets the writer\n\n```go\nfunc (r *Response) Write(w http.ResponseWriter) error\n```\nWrite writes the response to the writer\n\n```go\ntype Route interface {\n\tName() string\n\tMethods() []string\n\tPath() string\n}\n```\nRoute is the common route interface that should be implemented by handler functions\n\n```go\ntype RouteHook func(r *http.Request, rt Route) error\n```\nRouteHook methods are called before the handler to allow for added context\n\n```go\ntype Router struct {\n\t*Server\n\tMux *mux.Router\n\t// contains filtered or unexported fields\n}\n```\nRouter is an api Router\n\n```go\nfunc (r *Router) AddRoutes(routes ...Route) *Router\n```\nAddRoutes adds a routes to the router\n\n```go\nfunc (r Router) Name() string\n```\nName implements the Versioner interface\n\n```go\nfunc (r Router) RequireVersion() bool\n```\nRequireVersion implements the Versioner interface\n\n```go\nfunc (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)\n```\nServeHTTP implements the http.Handler interface\n\n```go\nfunc (r Router) Version() string\n```\nVersion implements the Versioner interface\n\n```go\nfunc (r *Router) WithName(name string) RouterOption\n```\nWithName enables versioning that will enforce a versioned path\n\n```go\ntype RouterOption func(r *Router)\n```\nRouterOption are router options\n\n```go\nfunc WithAuthorizers(auth ...Authorizer) RouterOption\n```\nWithAuthorizers sets the authorizers for the router\n\n```go\nfunc WithContext(ctx func(context.Context) interface{}) RouterOption\n```\nWithContext adds context to the router\n\n```go\nfunc WithHooks(hooks ...RouteHook) RouterOption\n```\nWithHooks sets the hooks for the router\n\n```go\nfunc WithSessionManager(store *session.Manager) RouterOption\n```\nWithSessionManager adds the session store to the router\n\n```go\nfunc WithVersioning(version ...string) RouterOption\n```\nWithVersioning enables versioning that will enforce a versioned path\n\n```go\ntype Server struct {\n\t// contains filtered or unexported fields\n}\n```\nServer is an http server that provides basic REST funtionality\n\n```go\nfunc NewServer(opts ...Option) *Server\n```\nNewServer creates a new server object\n\n```go\nfunc (s *Server) Log() log.Interface\n```\nLog returns the server log\n\n```go\nfunc (s *Server) LogMiddleware() func(http.Handler) http.Handler\n```\nLogMiddleware is simple logging middleware handler\n\n```go\nfunc (s *Server) Name() string\n```\nName implements the Versioner interface\n\n```go\nfunc (s *Server) RequireVersion() bool\n```\nRequireVersion implements the Versioner interface\n\n```go\nfunc (s *Server) Router(basePath string, opts ...RouterOption) *Router\n```\nRouter returns an api router at the specified base path\n\n```go\nfunc (s *Server) Serve() error\n```\nServe starts the http server\n\n```go\nfunc (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)\n```\nServeHTTP implements the http.Handler interface\n\n```go\nfunc (s *Server) Shutdown(ctx context.Context) error\n```\nShutdown shuts down the http server with the context\n\n```go\nfunc (s *Server) Version() string\n```\nVersion implements the Versioner interface\n\n```go\nfunc (s *Server) WriteError(w http.ResponseWriter, status int, err error)\n```\nWriteError writes an error object\n\n```go\nfunc (s *Server) WriteJSON(w http.ResponseWriter, status int, v interface{}, pretty ...bool)\n```\nWriteJSON writes out json\n\n```go\ntype StatusError struct {\n\t// contains filtered or unexported fields\n}\n```\nStatusError is a basic status error others can sub\n\n```go\nfunc (e StatusError) Code() string\n```\nCode returns the error code\n\n```go\nfunc (e StatusError) Detail() []string\n```\nDetail returns the error detail\n\n```go\nfunc (e StatusError) Error() string\n```\n\n```go\nfunc (e *StatusError) Is(target error) bool\n```\nIs implements the errors.Is interface\n\n```go\nfunc (e StatusError) Payload() interface{}\n```\nPayload implements the api.Responder interface\n\n```go\nfunc (e StatusError) Status() int\n```\nStatus implements the api.Responder interface\n\n```go\nfunc (e StatusError) WithCode(code string) ErrorResponse\n```\nWithCode sets the error code for the response, the default is the http status\n\n```go\nfunc (e StatusError) WithDetail(detail ...interface{}) ErrorResponse\n```\nWithDetail returns the error with detail\n\n```go\nfunc (e StatusError) WithError(err error) ErrorResponse\n```\nWithError returns the error with an underlying error\n\n```go\nfunc (e StatusError) WithMessage(format string, args ...interface{}) ErrorResponse\n```\nWithMessage returns the error with a message\n\n```go\nfunc (e StatusError) WithStatus(status int) ErrorResponse\n```\nWithStatus returns the error with status\n\n```go\nfunc (e StatusError) Write(w http.ResponseWriter) error\n```\nWrite implements the api.Responder interface\n\n```go\ntype Versioner interface {\n\tName() string\n\tVersion() string\n\tRequireVersion() bool\n}\n```\nVersioner provides a version\n\n```go\ntype WriterFunc func(w http.ResponseWriter, status int, payload interface{}, headers ...http.Header) error\n```\nWriterFunc is a response writer\n\n","path":"github.com/ModelRocket/hiro/pkg/api","name":"api","synopsis":"Package api is the hiro api helper library","isRedistributable":true}
