KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_handler.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Jon Evans <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <api/api_handler.h>
22#include <wx/wx.h>
23
24using kiapi::common::ApiRequest, kiapi::common::ApiResponse, kiapi::common::ApiResponseStatus;
25
26
27const wxString API_HANDLER::m_defaultCommitMessage = _( "Modification from API" );
28
29
31{
32 ApiResponseStatus status;
33
34 if( !aMsg.has_message() )
35 {
36 status.set_status( ApiStatusCode::AS_BAD_REQUEST );
37 status.set_error_message( "request has no inner message" );
38 return tl::unexpected( status );
39 }
40
41 std::string typeName;
42
43 if( !google::protobuf::Any::ParseAnyTypeUrl( aMsg.message().type_url(), &typeName ) )
44 {
45 status.set_status( ApiStatusCode::AS_BAD_REQUEST );
46 status.set_error_message( "could not parse inner message type" );
47 return tl::unexpected( status );
48 }
49
50 auto it = m_handlers.find( typeName );
51
52 if( it != m_handlers.end() )
53 {
54 REQUEST_HANDLER& handler = it->second;
55 return handler( aMsg );
56 }
57
58 status.set_status( ApiStatusCode::AS_UNHANDLED );
59 // This response is used internally; no need for an error message
60 return tl::unexpected( status );
61}
tl::expected< ApiResponse, ApiResponseStatus > API_RESULT
Definition: api_handler.h:42
static const wxString m_defaultCommitMessage
Definition: api_handler.h:131
std::map< std::string, REQUEST_HANDLER > m_handlers
Maps type name (without the URL prefix) to a handler method.
Definition: api_handler.h:129
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...
Definition: api_handler.h:76
API_RESULT Handle(ApiRequest &aMsg)
Attempt to handle the given API request, if a handler exists in this class for the message.
Definition: api_handler.cpp:30
#define _(s)