AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::rest::Api Class Reference

#include <api.hpp>

Collaboration diagram for anch::rest::Api:

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

Detailed Description

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.

api.get("/pets/{id}", [](uint64_t id) -> std::optional<Pet> { return findPet(id); }) // 404 when empty
.produces<anch::codec::Json>();
api.post("/pets", [](anch::rest::Body<Pet> pet) {
Pet saved = savePet(*pet);
return anch::rest::Response(anch::rest::Status::CREATED)
.header(anch::rest::header::LOCATION, "/pets/" + std::to_string(saved.id));
})
.consumes<anch::codec::Json>();
api.filter([](anch::rest::Request& request, const anch::rest::Next& next) {
return anch::rest::Response(anch::rest::Status::UNAUTHORIZED);
}
return next(request);
});
api.validate(); // every endpoint has its codecs
anch::rest::Response response = api.handle(request); // done by HTTP server adapter

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.

Author
Vincent Lachenal
Since
0.2

Constructor & Destructor Documentation

◆ Api() [1/3]

anch::rest::Api::Api ( )

Api constructor

◆ Api() [2/3]

anch::rest::Api::Api ( const Api & )
delete

Prohibit Api copy

Here is the call graph for this function:

◆ Api() [3/3]

anch::rest::Api::Api ( Api && other)
noexcept

Api move constructor

Parameters
otherthe API to move
Here is the call graph for this function:

◆ ~Api()

virtual anch::rest::Api::~Api ( )
virtual

Api destructor

Member Function Documentation

◆ cors()

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.

Parameters
optionsthe CORS options
Returns
this
Exceptions
std::invalid_argumentwhen there is no origin or when "*" origin is used with credentials
Here is the call graph for this function:

◆ del()

template<typename F>
anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::del ( std::string_view pattern,
F && function )

Declare DELETE endpoint (see route )

Parameters
patternthe path pattern
functionthe endpoint function
Returns
the endpoint

◆ filter()

Api & anch::rest::Api::filter ( anch::rest::Filter filter)

Add filter. Filters are executed in declaration order around endpoints (and around routing errors).

Parameters
filterthe filter
Returns
this
Here is the call graph for this function:

◆ get()

template<typename F>
anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::get ( std::string_view pattern,
F && function )

Declare GET endpoint (see route )

Parameters
patternthe path pattern
functionthe endpoint function
Returns
the endpoint

◆ handle()

anch::rest::Response anch::rest::Api::handle ( anch::rest::Request & request)
noexcept

Handle request

Parameters
requestthe request
Returns
the response

◆ operator=() [1/2]

Api & anch::rest::Api::operator= ( Api && other)
noexcept

Api move assignment

Parameters
otherthe API to move
Returns
this
Here is the call graph for this function:

◆ operator=() [2/2]

Api & anch::rest::Api::operator= ( const Api & )
delete

Prohibit Api copy

Here is the call graph for this function:

◆ patch()

template<typename F>
anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::patch ( std::string_view pattern,
F && function )

Declare PATCH endpoint (see route )

Parameters
patternthe path pattern
functionthe endpoint function
Returns
the endpoint

◆ post()

template<typename F>
anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::post ( std::string_view pattern,
F && function )

Declare POST endpoint (see route )

Parameters
patternthe path pattern
functionthe endpoint function
Returns
the endpoint

◆ put()

template<typename F>
anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::put ( std::string_view pattern,
F && function )

Declare PUT endpoint (see route )

Parameters
patternthe path pattern
functionthe endpoint function
Returns
the endpoint

◆ route()

template<typename F>
anch::rest::Endpoint< std::decay_t< F > > & anch::rest::Api::route ( anch::rest::Method method,
std::string_view pattern,
F && function )

Declare endpoint

Parameters
methodthe method
patternthe path pattern
functionthe endpoint function (see Endpoint )
Returns
the endpoint, to declare its codecs
Exceptions
std::invalid_argumentwhen pattern is invalid, when the number of path parameters does not match function arguments or when route already exists

◆ validate()

void anch::rest::Api::validate ( ) const

Check every endpoint configuration (codecs). Server adapters call it at startup.

Exceptions
std::logic_errorlisting invalid endpoints

The documentation for this class was generated from the following file:
  • anch-rest/server/inc/rest/api.hpp