23#include <wx/translation.h>
26#include <boost/algorithm/string.hpp>
27#include <nlohmann/json.hpp>
42 if( aTestConnectionNow )
67 res = curl->GetBuffer();
72 if(
res.length() == 0 )
74 m_lastError += wxString::Format(
_(
"KiCad received an empty response!" ) +
"\n" );
78 nlohmann::json response = nlohmann::json::parse(
res );
88 catch(
const std::exception& e )
90 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
94 wxT(
"ValidateHTTPLibraryEndpoints: Exception occurred while testing the API "
120 wxLogTrace(
traceHTTPLib, wxT(
"syncCategories: without valid connection!" ) );
124 std::string
res =
"";
133 res = curl->GetBuffer();
140 nlohmann::json response = nlohmann::json::parse(
res );
143 for(
const auto& item : response.items() )
147 auto& value = item.value();
148 category.
id = value[
"id"].get<std::string>();
149 category.
name = value[
"name"].get<std::string>();
151 if( value.contains(
"description" ) )
153 category.
description = value[
"description"].get<std::string>();
160 catch(
const std::exception& e )
162 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
166 wxT(
"syncCategories: Exception occurred while syncing categories: %s" ),
182 wxLogTrace(
traceHTTPLib, wxT(
"SelectOne: without valid connection!" ) );
190 if( std::difftime( std::time(
nullptr ),
m_cachedParts[aPartID].lastCached )
198 std::string
res =
"";
208 res = curl->GetBuffer();
215 nlohmann::ordered_json response = nlohmann::ordered_json::parse(
res );
216 std::string key =
"";
217 std::string value =
"";
221 aFetchedPart.
id = response.at(
"id" );
224 aFetchedPart.
lastCached = std::time(
nullptr );
227 if( response.contains(
"name" ) )
229 aFetchedPart.
name = response.at(
"name" );
233 aFetchedPart.
name = aFetchedPart.
id;
236 aFetchedPart.
symbolIdStr = response.at(
"symbolIdStr" );
241 if( response.contains(
"exclude_from_bom" ) )
244 exclude = response.at(
"exclude_from_bom" );
249 if( response.contains(
"exclude_from_board" ) )
252 exclude = response.at(
"exclude_from_board" );
257 if( response.contains(
"exclude_from_sim" ) )
260 exclude = response.at(
"exclude_from_sim" );
265 aFetchedPart.
fields.clear();
268 for(
const auto& field : response.at(
"fields" ).items() )
276 auto& properties = field.value();
278 value = properties.at(
"value" );
281 if( properties.contains(
"visible" ) )
283 std::string vis = properties.at(
"visible" );
288 aFetchedPart.
fields.push_back(
289 std::make_pair( key, std::make_tuple( value, visible ) ) );
292 catch(
const std::exception& e )
294 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
298 wxT(
"SelectOne: Exception occurred while retrieving part from REST API: %s" ),
311 std::vector<HTTP_LIB_PART>& aParts )
315 wxLogTrace(
traceHTTPLib, wxT(
"SelectAll: without valid connection!" ) );
319 std::string
res =
"";
330 res = curl->GetBuffer();
332 nlohmann::json response = nlohmann::json::parse(
res );
334 for( nlohmann::json& item : response )
339 part.
id = item.at(
"id" );
341 if( item.contains(
"description" ) )
344 part.
fields.push_back( std::make_pair(
345 "description", std::make_tuple( item.at(
"description" ),
false ) ) );
349 if( item.contains(
"name" ) )
351 part.
name = item.at(
"name" );
361 aParts.emplace_back( std::move( part ) );
364 catch(
const std::exception& e )
366 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
369 wxLogTrace(
traceHTTPLib, wxT(
"Exception occurred while syncing parts from REST API: %s" ),
381 int statusCode = aCurl->GetResponseStatusCode();
382 if( statusCode != 200 )
384 m_lastError += wxString::Format(
_(
"API responded with error code: %s" ) +
"\n",
397 wxString strval( std::any_cast<std::string>( aVal ).c_str(), wxConvUTF8 );
399 if( strval.IsEmpty() )
400 return aDefaultValue;
404 for(
const auto& trueVal : { wxS(
"true" ), wxS(
"yes" ), wxS(
"y" ), wxS(
"1" ) } )
406 if( strval.Matches( trueVal ) )
410 for(
const auto& falseVal : { wxS(
"false" ), wxS(
"no" ), wxS(
"n" ), wxS(
"0" ) } )
412 if( strval.Matches( falseVal ) )
416 catch(
const std::bad_any_cast& )
420 return aDefaultValue;
426 auto codeDescription =
427 []( uint16_t aCode ) -> wxString
431 case 100:
return wxS(
"Continue" );
432 case 101:
return wxS(
"Switching Protocols" );
433 case 102:
return wxS(
"Processing" );
434 case 103:
return wxS(
"Early Hints" );
436 case 200:
return wxS(
"OK" );
437 case 201:
return wxS(
"Created" );
438 case 203:
return wxS(
"Non-Authoritative Information" );
439 case 204:
return wxS(
"No Content" );
440 case 205:
return wxS(
"Reset Content" );
441 case 206:
return wxS(
"Partial Content" );
442 case 207:
return wxS(
"Multi-Status" );
443 case 208:
return wxS(
"Already Reported" );
444 case 226:
return wxS(
"IM Used" );
446 case 300:
return wxS(
"Multiple Choices" );
447 case 301:
return wxS(
"Moved Permanently" );
448 case 302:
return wxS(
"Found" );
449 case 303:
return wxS(
"See Other" );
450 case 304:
return wxS(
"Not Modified" );
451 case 305:
return wxS(
"Use Proxy (Deprecated)" );
452 case 306:
return wxS(
"Unused" );
453 case 307:
return wxS(
"Temporary Redirect" );
454 case 308:
return wxS(
"Permanent Redirect" );
456 case 400:
return wxS(
"Bad Request" );
457 case 401:
return wxS(
"Unauthorized" );
458 case 402:
return wxS(
"Payment Required (Experimental)" );
459 case 403:
return wxS(
"Forbidden" );
460 case 404:
return wxS(
"Not Found" );
461 case 405:
return wxS(
"Method Not Allowed" );
462 case 406:
return wxS(
"Not Acceptable" );
463 case 407:
return wxS(
"Proxy Authentication Required" );
464 case 408:
return wxS(
"Request Timeout" );
465 case 409:
return wxS(
"Conflict" );
466 case 410:
return wxS(
"Gone" );
467 case 411:
return wxS(
"Length Required" );
468 case 412:
return wxS(
"Payload Too Large" );
469 case 414:
return wxS(
"URI Too Long" );
470 case 415:
return wxS(
"Unsupported Media Type" );
471 case 416:
return wxS(
"Range Not Satisfiable" );
472 case 417:
return wxS(
"Expectation Failed" );
473 case 418:
return wxS(
"I'm a teapot" );
474 case 421:
return wxS(
"Misdirected Request" );
475 case 422:
return wxS(
"Unprocessable Content" );
476 case 423:
return wxS(
"Locked" );
477 case 424:
return wxS(
"Failed Dependency" );
478 case 425:
return wxS(
"Too Early (Experimental)" );
479 case 426:
return wxS(
"Upgrade Required" );
480 case 428:
return wxS(
"Precondition Required" );
481 case 429:
return wxS(
"Too Many Requests" );
482 case 431:
return wxS(
"Request Header Fields Too Large" );
483 case 451:
return wxS(
"Unavailable For Legal Reasons" );
485 case 500:
return wxS(
"Internal Server Error" );
486 case 501:
return wxS(
"Not Implemented" );
487 case 502:
return wxS(
"Bad Gateway" );
488 case 503:
return wxS(
"Service Unavailable" );
489 case 504:
return wxS(
"Gateway Timeout" );
490 case 505:
return wxS(
"HTTP Version Not Supported" );
491 case 506:
return wxS(
"Variant Also Negotiates" );
492 case 507:
return wxS(
"Insufficient Storage" );
493 case 508:
return wxS(
"Loop Detected" );
494 case 510:
return wxS(
"Not Extended" );
495 case 511:
return wxS(
"Network Authentication Required" );
496 default:
return wxS(
"Unknown" );
500 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)
Retrieve 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)
Retrieve a single part with full details from the HTTP library.
wxString httpErrorCodeDescription(uint16_t aHttpCode)
HTTP response status codes indicate whether a specific HTTP request has been successfully completed.
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