7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-02 18:36:55 +00:00
kicad/common/api/api_handler.cpp
Jon Evans f613cd1cb4 ADDED: A new IPC API based on protobuf and nng
Details, documentation, and language bindings are works in
progress and will be evolving over the course of KiCad 9
development.
2024-04-02 19:34:36 -04:00

75 lines
2.4 KiB
C++

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Jon Evans <jon@craftyjon.com>
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <api/api_handler.h>
using kiapi::common::ApiRequest, kiapi::common::ApiResponse, kiapi::common::ApiResponseStatus;
API_RESULT API_HANDLER::Handle( ApiRequest& aMsg )
{
ApiResponseStatus status;
if( !aMsg.has_message() )
{
status.set_status( ApiStatusCode::AS_BAD_REQUEST );
status.set_error_message( "request has no inner message" );
return tl::unexpected( status );
}
std::string typeName;
if( !google::protobuf::Any::ParseAnyTypeUrl( aMsg.message().type_url(), &typeName ) )
{
status.set_status( ApiStatusCode::AS_BAD_REQUEST );
status.set_error_message( "could not parse inner message type" );
return tl::unexpected( status );
}
auto it = m_handlers.find( typeName );
if( it != m_handlers.end() )
{
REQUEST_HANDLER& handler = it->second;
return handler( aMsg );
}
status.set_status( ApiStatusCode::AS_UNHANDLED );
// This response is used internally; no need for an error message
return tl::unexpected( status );
}
std::optional<KICAD_T> API_HANDLER::TypeNameFromAny( const google::protobuf::Any& aMessage )
{
static const std::map<std::string, KICAD_T> s_types = {
{ "type.googleapis.com/kiapi.board.types.Track", PCB_TRACE_T },
{ "type.googleapis.com/kiapi.board.types.Arc", PCB_ARC_T },
{ "type.googleapis.com/kiapi.board.types.Via", PCB_VIA_T },
};
auto it = s_types.find( aMessage.type_url() );
if( it != s_types.end() )
return it->second;
return std::nullopt;
}