21#ifndef KICAD_API_HANDLER_H
22#define KICAD_API_HANDLER_H
27#include <fmt/format.h>
28#include <tl/expected.hpp>
33#include <google/protobuf/message.h>
36#include <api/common/envelope.pb.h>
41using kiapi::common::ApiRequest, kiapi::common::ApiResponse;
42using kiapi::common::ApiResponseStatus, kiapi::common::ApiStatusCode;
44typedef tl::expected<ApiResponse, ApiResponseStatus>
API_RESULT;
50template <
typename RequestMessageType>
73 bool CanHandle(
const std::string& aTypeName )
const
84 return it !=
m_handlers.end() && it->second.AllowedDuringTask;
135 template <
class RequestType,
class ResponseType,
class HandlerType>
139 std::string typeName { RequestType().GetTypeName() };
141 wxASSERT_MSG( !
m_handlers.contains( typeName ),
142 wxString::Format(
"Duplicate API handler for type %s", typeName ) );
148 ApiResponse envelope;
153 ctx.
ClientName = aRequest.header().client_name();
156 std::invoke( aHandler,
static_cast<HandlerType*
>(
this ), ctx );
158 if( response.has_value() )
160 if( !envelope.mutable_message()->PackFrom( *response ) )
162 wxFAIL_MSG( wxString::Format(
"Failed to pack '%s'", typeName ) );
164 ApiResponseStatus status;
165 status.set_status( ApiStatusCode::AS_INTERNAL_ERROR );
166 status.set_error_message(
"the response could not be serialized" );
167 return tl::unexpected( status );
170 envelope.mutable_status()->set_status( ApiStatusCode::AS_OK );
175 return tl::unexpected( response.error() );
177 }, aAllowedDuringTask };
191 template<
typename MessageType>
192 bool tryUnpack( ApiRequest& aRequest, ApiResponse& aReply, MessageType& aDest )
194 if( !aRequest.message().UnpackTo( &aDest ) )
196 std::string msg = fmt::format(
"could not unpack message of type {} from request",
197 aDest.GetTypeName() );
198 aReply.mutable_status()->set_status( ApiStatusCode::AS_BAD_REQUEST );
199 aReply.mutable_status()->set_error_message( msg );
tl::expected< ApiResponse, ApiResponseStatus > API_RESULT
tl::expected< T, ApiResponseStatus > HANDLER_RESULT
std::map< std::string, HANDLER_ENTRY > m_handlers
Maps type name (without the URL prefix) to a handler method.
static const wxString m_defaultCommitMessage
bool AllowsDuringTask(const std::string &aTypeName) const
KICAD_API_SERVER * m_apiServer
virtual void onNetSettingsChanged()
Called on all registered handlers after project net settings (netclasses or netclass assignments) hav...
KICAD_API_SERVER * apiServer() const
void registerHandler(HANDLER_RESULT< ResponseType >(HandlerType::*aHandler)(const HANDLER_CONTEXT< RequestType > &), bool aAllowedDuringTask=false)
Registers an API command handler for the given message types.
void requestNetSettingsNotification()
bool clearNetSettingsNotification()
Returns true if the notification had been requested at the time of the call.
bool tryUnpack(ApiRequest &aRequest, ApiResponse &aReply, MessageType &aDest)
bool CanHandle(const std::string &aTypeName) const
std::function< HANDLER_RESULT< ApiResponse >(ApiRequest &)> REQUEST_HANDLER
A handler for outer messages (envelopes) that will unpack to inner messages and call a specific handl...
void setApiServer(KICAD_API_SERVER *aServer)
Set by KICAD_API_SERVER::RegisterHandler; null when the handler is not registered.
Handle(KICAD3D_INFO) KICAD3D_INFO
RequestMessageType Request