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

#include <rpcService.hpp>

Collaboration diagram for anch::rest::RpcService:

Public Member Functions

 RpcService (std::string_view name)
template<anch::codec::ProtobufMessage Req, anch::codec::ProtobufMessage Res, typename F>
RpcService & unary (std::string_view method, F &&handler)
std::string call (std::string_view method, std::string_view request, anch::rest::RpcFormat format, anch::rest::RpcContext &context) const
bool has (std::string_view method) const
std::vector< std::string > getMethods () const
const std::string & getName () const noexcept
std::size_t getMaxMessageSize () const noexcept
void setMaxMessageSize (std::size_t size) noexcept

Detailed Description

RPC service: unary methods of a Protocol Buffers service, served by installRpc (Connect and gRPC-Web in Api , HTTP/1.1) and by GrpcServer (gRPC, HTTP/2) with the same handlers.

anch::rest::RpcService service("pets.PetStore");
service.unary<pets::GetPetRequest, pets::Pet>("GetPet", [](const pets::GetPetRequest& request, anch::rest::RpcContext&) {
if(request.id() != 1) {
throw anch::rest::RpcError(anch::rest::RpcCode::NOT_FOUND, "No pet"); // Connect, gRPC-Web and gRPC status
}
return findPet(request.id());
});
anch::rest::installRpc(api, service); // POST /pets.PetStore/GetPet (Connect, gRPC-Web): rest/rpcApi.hpp

Handler errors: RpcError is sent with its code and message, HttpException with the code of its status ( 400 is INVALID_ARGUMENT , 404 is NOT_FOUND , ...), other exceptions are INTERNAL without message.

Methods have to be declared before calls; calls can then be made from several threads.

Author
Vincent Lachenal
Since
0.2

Constructor & Destructor Documentation

◆ RpcService()

anch::rest::RpcService::RpcService ( std::string_view name)
explicit

RpcService constructor

Parameters
namethe service full name ( package.Service )
Exceptions
std::invalid_argumentwhen name is empty or contains /

Member Function Documentation

◆ call()

std::string anch::rest::RpcService::call ( std::string_view method,
std::string_view request,
anch::rest::RpcFormat format,
anch::rest::RpcContext & context ) const

Call method

Parameters
methodthe method name
requestthe request message
formatthe messages encoding
contextthe call context
Returns
the response message
Exceptions
RpcErroron error ( UNIMPLEMENTED for unknown method, INVALID_ARGUMENT for invalid request, RESOURCE_EXHAUSTED for request larger than maximum message size, handler errors)

◆ getMaxMessageSize()

std::size_t anch::rest::RpcService::getMaxMessageSize ( ) const
noexcept

Get maximum request message size (default: 4 MiB, as gRPC)

Returns
the size in bytes
Here is the call graph for this function:

◆ getMethods()

std::vector< std::string > anch::rest::RpcService::getMethods ( ) const

Get methods names

Returns
the methods names, sorted

◆ getName()

const std::string & anch::rest::RpcService::getName ( ) const
noexcept

Get service full name

Returns
the name

◆ has()

bool anch::rest::RpcService::has ( std::string_view method) const

Check if service has a method

Parameters
methodthe method name
Returns
true when method is declared

◆ setMaxMessageSize()

void anch::rest::RpcService::setMaxMessageSize ( std::size_t size)
noexcept

Set maximum request message size

Parameters
sizethe size in bytes
Here is the call graph for this function:

◆ unary()

RpcService & anch::rest::RpcService::unary ( std::string_view method,
F && handler )

Declare unary method

Template Parameters
Reqthe request message type
Resthe response message type
Parameters
methodthe method name ( GetPet )
handlerthe handler: Res(const Req&, RpcContext&) or Res(const Req&)
Returns
this
Exceptions
std::invalid_argumentwhen method name is invalid or already declared
Here is the call graph for this function:

The documentation for this class was generated from the following file: