22#include <wx/translation.h>
25#include <boost/algorithm/string.hpp>
26#include <nlohmann/json.hpp>
41 if( aTestConnectionNow )
66 res = curl->GetBuffer();
71 if(
res.length() == 0 )
73 m_lastError += wxString::Format(
_(
"KiCad received an empty response!" ) +
"\n" );
77 nlohmann::json response = nlohmann::json::parse(
res );
87 catch(
const std::exception& e )
89 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
93 wxT(
"ValidateHTTPLibraryEndpoints: Exception occurred while testing the API "
119 wxLogTrace(
traceHTTPLib, wxT(
"syncCategories: without valid connection!" ) );
123 std::string
res =
"";
132 res = curl->GetBuffer();
139 nlohmann::json response = nlohmann::json::parse(
res );
142 for(
const auto& item : response.items() )
146 auto& value = item.value();
147 category.
id = value[
"id"].get<std::string>();
148 category.
name = value[
"name"].get<std::string>();
150 if( value.contains(
"description" ) )
152 category.
description = value[
"description"].get<std::string>();
159 catch(
const std::exception& e )
161 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
165 wxT(
"syncCategories: Exception occurred while syncing categories: %s" ),
181 wxLogTrace(
traceHTTPLib, wxT(
"SelectOne: without valid connection!" ) );
189 if( std::difftime( std::time(
nullptr ),
m_cachedParts[aPartID].lastCached )
197 std::string
res =
"";
207 res = curl->GetBuffer();
214 nlohmann::ordered_json response = nlohmann::ordered_json::parse(
res );
215 std::string key =
"";
216 std::string value =
"";
220 aFetchedPart.
id = response.at(
"id" );
223 aFetchedPart.
lastCached = std::time(
nullptr );
226 if( response.contains(
"name" ) )
228 aFetchedPart.
name = response.at(
"name" );
232 aFetchedPart.
name = aFetchedPart.
id;
235 aFetchedPart.
symbolIdStr = response.at(
"symbolIdStr" );
240 if( response.contains(
"exclude_from_bom" ) )
243 exclude = response.at(
"exclude_from_bom" );
248 if( response.contains(
"exclude_from_board" ) )
251 exclude = response.at(
"exclude_from_board" );
256 if( response.contains(
"exclude_from_sim" ) )
259 exclude = response.at(
"exclude_from_sim" );
264 aFetchedPart.
fields.clear();
267 for(
const auto& field : response.at(
"fields" ).items() )
275 auto& properties = field.value();
277 value = properties.at(
"value" );
280 if( properties.contains(
"visible" ) )
282 std::string vis = properties.at(
"visible" );
287 aFetchedPart.
fields.push_back(
288 std::make_pair( key, std::make_tuple( value, visible ) ) );
291 catch(
const std::exception& e )
293 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
297 wxT(
"SelectOne: Exception occurred while retrieving part from REST API: %s" ),
310 std::vector<HTTP_LIB_PART>& aParts )
314 wxLogTrace(
traceHTTPLib, wxT(
"SelectAll: without valid connection!" ) );
318 std::string
res =
"";
329 res = curl->GetBuffer();
331 nlohmann::json response = nlohmann::json::parse(
res );
333 for( nlohmann::json& item : response )
338 part.
id = item.at(
"id" );
340 if( item.contains(
"description" ) )
343 part.
fields.push_back( std::make_pair(
344 "description", std::make_tuple( item.at(
"description" ),
false ) ) );
348 if( item.contains(
"name" ) )
350 part.
name = item.at(
"name" );
360 aParts.emplace_back( std::move( part ) );
363 catch(
const std::exception& e )
365 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
368 wxLogTrace(
traceHTTPLib, wxT(
"Exception occurred while syncing parts from REST API: %s" ),
380 int statusCode = aCurl->GetResponseStatusCode();
381 if( statusCode != 200 )
383 m_lastError += wxString::Format(
_(
"API responded with error code: %s" ) +
"\n",
396 wxString strval( std::any_cast<std::string>( aVal ).c_str(), wxConvUTF8 );
398 if( strval.IsEmpty() )
399 return aDefaultValue;
403 for(
const auto& trueVal : { wxS(
"true" ), wxS(
"yes" ), wxS(
"y" ), wxS(
"1" ) } )
405 if( strval.Matches( trueVal ) )
409 for(
const auto& falseVal : { wxS(
"false" ), wxS(
"no" ), wxS(
"n" ), wxS(
"0" ) } )
411 if( strval.Matches( falseVal ) )
415 catch(
const std::bad_any_cast& )
419 return aDefaultValue;
435 auto codeDescription =
436 []( uint16_t aCode ) -> wxString
440 case 100:
return wxS(
"Continue" );
441 case 101:
return wxS(
"Switching Protocols" );
442 case 102:
return wxS(
"Processing" );
443 case 103:
return wxS(
"Early Hints" );
445 case 200:
return wxS(
"OK" );
446 case 201:
return wxS(
"Created" );
447 case 203:
return wxS(
"Non-Authoritative Information" );
448 case 204:
return wxS(
"No Content" );
449 case 205:
return wxS(
"Reset Content" );
450 case 206:
return wxS(
"Partial Content" );
451 case 207:
return wxS(
"Multi-Status" );
452 case 208:
return wxS(
"Already Reporte" );
453 case 226:
return wxS(
"IM Used" );
455 case 300:
return wxS(
"Multiple Choices" );
456 case 301:
return wxS(
"Moved Permanently" );
457 case 302:
return wxS(
"Found" );
458 case 303:
return wxS(
"See Other" );
459 case 304:
return wxS(
"Not Modified" );
460 case 305:
return wxS(
"Use Proxy (Deprecated)" );
461 case 306:
return wxS(
"Unused" );
462 case 307:
return wxS(
"Temporary Redirect" );
463 case 308:
return wxS(
"Permanent Redirect" );
465 case 400:
return wxS(
"Bad Request" );
466 case 401:
return wxS(
"Unauthorized" );
467 case 402:
return wxS(
"Payment Required (Experimental)" );
468 case 403:
return wxS(
"Forbidden" );
469 case 404:
return wxS(
"Not Found" );
470 case 405:
return wxS(
"Method Not Allowed" );
471 case 406:
return wxS(
"Not Acceptable" );
472 case 407:
return wxS(
"Proxy Authentication Required" );
473 case 408:
return wxS(
"Request Timeout" );
474 case 409:
return wxS(
"Conflict" );
475 case 410:
return wxS(
"Gone" );
476 case 411:
return wxS(
"Length Required" );
477 case 412:
return wxS(
"Payload Too Large" );
478 case 414:
return wxS(
"URI Too Long" );
479 case 415:
return wxS(
"Unsupported Media Type" );
480 case 416:
return wxS(
"Range Not Satisfiable" );
481 case 417:
return wxS(
"Expectation Failed" );
482 case 418:
return wxS(
"I'm a teapot" );
483 case 421:
return wxS(
"Misdirected Request" );
484 case 422:
return wxS(
"Unprocessable Conten" );
485 case 423:
return wxS(
"Locked" );
486 case 424:
return wxS(
"Failed Dependency" );
487 case 425:
return wxS(
"Too Early (Experimental)" );
488 case 426:
return wxS(
"Upgrade Required" );
489 case 428:
return wxS(
"Precondition Required" );
490 case 429:
return wxS(
"Too Many Requests" );
491 case 431:
return wxS(
"Request Header Fields Too Large" );
492 case 451:
return wxS(
"Unavailable For Legal Reasons" );
494 case 500:
return wxS(
"Internal Server Error" );
495 case 501:
return wxS(
"Not Implemented" );
496 case 502:
return wxS(
"Bad Gateway" );
497 case 503:
return wxS(
"Service Unavailable" );
498 case 504:
return wxS(
"Gateway Timeout" );
499 case 505:
return wxS(
"HTTP Version Not Supported" );
500 case 506:
return wxS(
"Variant Also Negotiates" );
501 case 507:
return wxS(
"Insufficient Storag" );
502 case 508:
return wxS(
"Loop Detecte" );
503 case 510:
return wxS(
"Not Extended" );
504 case 511:
return wxS(
"Network Authentication Required" );
505 default:
return wxS(
"Unknown" );
509 return wxString::Format( wxS(
"%d: %s" ), aHttpCode, codeDescription( aHttpCode ) );
const std::string http_endpoint_parts
bool checkServerResponse(std::unique_ptr< KICAD_CURL_EASY > &aCurl)
const std::string http_endpoint_categories
bool ValidateHTTPLibraryEndpoints()
std::map< std::string, HTTP_LIB_PART > m_cachedParts
std::unique_ptr< KICAD_CURL_EASY > createCurlEasyObject()
std::map< std::string, std::string > m_categoryDescriptions
bool boolFromString(const std::any &aVal, bool aDefaultValue=false)
bool SelectAll(const HTTP_LIB_CATEGORY &aCategory, std::vector< HTTP_LIB_PART > &aParts)
Retrieves all parts from a specific category from the HTTP library.
HTTP_LIB_CONNECTION(const HTTP_LIB_SOURCE &aSource, bool aTestConnectionNow)
std::map< std::string, std::tuple< std::string, std::string > > m_cache
std::vector< HTTP_LIB_CATEGORY > m_categories
bool IsValidEndpoint() const
bool SelectOne(const std::string &aPartID, HTTP_LIB_PART &aFetchedPart)
Retrieves a single part with full details from the HTTP library.
wxString httpErrorCodeDescription(uint16_t aHttpCode)
const char *const traceHTTPLib
const char *const traceHTTPLib
std::string id
id of category
std::string name
name of category
std::string description
description of category
std::vector< std::pair< std::string, std::tuple< std::string, bool > > > fields
additional generic fields