#include <api.hpp>
Public Member Functions | |
| Api () | |
| Api (const Api &)=delete | |
| Api (Api &&other) noexcept | |
| virtual | ~Api () |
| template<typename F> | |
| anch::rest::Endpoint< std::decay_t< F > > & | route (anch::rest::Method method, std::string_view pattern, F &&function) |
| template<typename F> | |
| anch::rest::Endpoint< std::decay_t< F > > & | get (std::string_view pattern, F &&function) |
| template<typename F> | |
| anch::rest::Endpoint< std::decay_t< F > > & | post (std::string_view pattern, F &&function) |
| template<typename F> | |
| anch::rest::Endpoint< std::decay_t< F > > & | put (std::string_view pattern, F &&function) |
| template<typename F> | |
| anch::rest::Endpoint< std::decay_t< F > > & | patch (std::string_view pattern, F &&function) |
| template<typename F> | |
| anch::rest::Endpoint< std::decay_t< F > > & | del (std::string_view pattern, F &&function) |
| Api & | filter (anch::rest::Filter filter) |
| Api & | cors (anch::rest::CorsOptions options) |
| void | validate () const |
| anch::rest::Response | handle (anch::rest::Request &request) noexcept |
| Api & | operator= (const Api &)=delete |
| Api & | operator= (Api &&other) noexcept |
REST API: endpoints declaration and routing, independent of the HTTP server.
Endpoints are declared with a method and a path pattern where path parameters are named between braces (/pets/{id} ). Empty path segments are ignored (/pets/ is /pets ). On routing, a literal segment has priority over a parameter.
Endpoint arguments and results are described in Endpoint .
handle never throws: an HttpException is answered with its status, any other exception with 500 . No route for path is 404 , no route for method is 405 (with Allow header). HEAD requests are answered by GET endpoints without body. OPTIONS requests without OPTIONS endpoint are answered with 204 and the Allow header ( OPTIONS * with 204 ).
Cross-Origin Resource Sharing is enabled with cors : preflight requests are answered before filters (they have no credentials), and CORS headers are added to every response, errors included.
Routes and filters have to be declared before handling requests; handle can then be called from several threads.
| anch::rest::Api::Api | ( | ) |
Api constructor
|
delete |
|
noexcept |
|
virtual |
Api destructor
| Api & anch::rest::Api::cors | ( | anch::rest::CorsOptions | options | ) |
Enable Cross-Origin Resource Sharing (CORS), see CorsOptions .
Preflight requests ( OPTIONS with Origin and Access-Control-Request-Method headers) are answered without executing filters: 204 with Access-Control-Allow-* headers, 403 when origin, method or a requested header is not allowed, 404 when path is unknown. Other responses get Access-Control-Allow-Origin (and Access-Control-Expose-Headers , Access-Control-Allow-Credentials ) when their origin is allowed.
| options | the CORS options |
this | std::invalid_argument | when there is no origin or when "*" origin is used with credentials |
| anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::del | ( | std::string_view | pattern, |
| F && | function ) |
Declare DELETE endpoint (see route )
| pattern | the path pattern |
| function | the endpoint function |
| Api & anch::rest::Api::filter | ( | anch::rest::Filter | filter | ) |
Add filter. Filters are executed in declaration order around endpoints (and around routing errors).
| filter | the filter |
this | anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::get | ( | std::string_view | pattern, |
| F && | function ) |
Declare GET endpoint (see route )
| pattern | the path pattern |
| function | the endpoint function |
|
noexcept |
Handle request
| request | the request |
Api move assignment
| other | the API to move |
this | anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::patch | ( | std::string_view | pattern, |
| F && | function ) |
Declare PATCH endpoint (see route )
| pattern | the path pattern |
| function | the endpoint function |
| anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::post | ( | std::string_view | pattern, |
| F && | function ) |
Declare POST endpoint (see route )
| pattern | the path pattern |
| function | the endpoint function |
| anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::put | ( | std::string_view | pattern, |
| F && | function ) |
Declare PUT endpoint (see route )
| pattern | the path pattern |
| function | the endpoint function |
| anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::route | ( | anch::rest::Method | method, |
| std::string_view | pattern, | ||
| F && | function ) |
Declare endpoint
| method | the method |
| pattern | the path pattern |
| function | the endpoint function (see Endpoint ) |
| std::invalid_argument | when pattern is invalid, when the number of path parameters does not match function arguments or when route already exists |
| void anch::rest::Api::validate | ( | ) | const |
Check every endpoint configuration (codecs). Server adapters call it at startup.
| std::logic_error | listing invalid endpoints |