23#include <wx/translation.h>
26#include <boost/algorithm/string.hpp>
42 if( aTestConnectionNow )
59 res = curl->GetBuffer();
64 if(
res.length() == 0 )
66 m_lastError += wxString::Format(
_(
"KiCad received an empty response!" ) +
"\n" );
70 nlohmann::json response = nlohmann::json::parse(
res );
73 if( !response.at(
"categories" ).empty() && !response.at(
"parts" ).empty() )
77 catch(
const std::exception& e )
79 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
82 wxLogTrace(
traceHTTPLib, wxT(
"validateHttpLibraryEndpoints: Exception while testing API connection: %s" ),
99 wxLogTrace(
traceHTTPLib, wxT(
"syncCategories: without valid connection!" ) );
103 std::string
res =
"";
106 curl->SetURL(
m_source.root_url +
"categories.json" );
112 res = curl->GetBuffer();
117 nlohmann::json response = nlohmann::json::parse(
res );
120 for(
const auto& item : response.items() )
124 auto& value = item.value();
125 category.
id = value[
"id"].get<std::string>();
126 category.
name = value[
"name"].get<std::string>();
128 if( value.contains(
"description" ) )
130 category.
description = value[
"description"].get<std::string>();
137 catch(
const std::exception& e )
139 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
157 wxLogTrace(
traceHTTPLib, wxT(
"SelectOne: without valid connection!" ) );
165 if( std::difftime( std::time(
nullptr ),
m_cachedParts[aPartID].lastCached ) <
m_source.timeout_parts )
172 std::string
res =
"";
175 std::string url =
m_source.root_url + fmt::format(
"parts/{}.json", aPartID );
182 res = curl->GetBuffer();
187 nlohmann::ordered_json response = nlohmann::ordered_json::parse(
res );
188 std::string key =
"";
189 std::string value =
"";
193 aFetchedPart.
id = response.at(
"id" );
196 aFetchedPart.
lastCached = std::time(
nullptr );
199 if( response.contains(
"name" ) )
200 aFetchedPart.
name = response.at(
"name" );
202 aFetchedPart.
name = aFetchedPart.
id;
204 aFetchedPart.
symbolIdStr = response.at(
"symbolIdStr" );
209 if( response.contains(
"exclude_from_bom" ) )
212 exclude = response.at(
"exclude_from_bom" );
217 if( response.contains(
"exclude_from_board" ) )
220 exclude = response.at(
"exclude_from_board" );
225 if( response.contains(
"exclude_from_sim" ) )
228 exclude = response.at(
"exclude_from_sim" );
233 aFetchedPart.
fields.clear();
236 for(
const auto& field : response.at(
"fields" ).items() )
244 auto& properties = field.value();
246 value = properties.at(
"value" );
249 if( properties.contains(
"visible" ) )
251 std::string vis = properties.at(
"visible" );
256 aFetchedPart.
fields.push_back( std::make_pair( key, std::make_tuple( value, visible ) ) );
259 if( response.contains(
"description" ) )
260 aFetchedPart.
desc = response.at(
"description" );
262 if( response.contains(
"keywords" ) )
263 aFetchedPart.
keywords = response.at(
"keywords" );
265 if( response.contains(
"footprint_filters" ) )
267 nlohmann::json filters_json = response.at(
"footprint_filters" );
269 if( filters_json.is_array() )
271 for(
const auto& val : filters_json )
276 aFetchedPart.
fp_filters.push_back( filters_json );
280 catch(
const std::exception& e )
282 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
300 wxLogTrace(
traceHTTPLib, wxT(
"SelectAll: without valid connection!" ) );
304 std::string
res =
"";
308 curl->SetURL(
m_source.root_url + fmt::format(
"parts/category/{}.json", aCategory.
id ) );
314 res = curl->GetBuffer();
316 nlohmann::json response = nlohmann::json::parse(
res );
318 for( nlohmann::json& item : response )
323 part.
id = item.at(
"id" );
325 if( item.contains(
"description" ) )
328 part.
fields.push_back( std::make_pair(
"description",
329 std::make_tuple( item.at(
"description" ),
false ) ) );
333 if( item.contains(
"name" ) )
334 part.
name = item.at(
"name" );
341 aParts.emplace_back( std::move( part ) );
344 catch(
const std::exception& e )
346 m_lastError += wxString::Format(
_(
"Error: %s" ) +
"\n" +
_(
"API Response: %s" ) +
"\n",
360 int statusCode = aCurl->GetResponseStatusCode();
362 if( statusCode != 200 )
364 m_lastError += wxString::Format(
_(
"API responded with error code: %s" ) +
"\n",
377 wxString strval( std::any_cast<std::string>( aVal ).c_str(), wxConvUTF8 );
379 if( strval.IsEmpty() )
380 return aDefaultValue;
384 for(
const auto& trueVal : { wxS(
"true" ), wxS(
"yes" ), wxS(
"y" ), wxS(
"1" ) } )
386 if( strval.Matches( trueVal ) )
390 for(
const auto& falseVal : { wxS(
"false" ), wxS(
"no" ), wxS(
"n" ), wxS(
"0" ) } )
392 if( strval.Matches( falseVal ) )
396 catch(
const std::bad_any_cast& )
400 return aDefaultValue;
406 auto codeDescription =
407 []( uint16_t aCode ) -> wxString
411 case 100:
return wxS(
"Continue" );
412 case 101:
return wxS(
"Switching Protocols" );
413 case 102:
return wxS(
"Processing" );
414 case 103:
return wxS(
"Early Hints" );
416 case 200:
return wxS(
"OK" );
417 case 201:
return wxS(
"Created" );
418 case 203:
return wxS(
"Non-Authoritative Information" );
419 case 204:
return wxS(
"No Content" );
420 case 205:
return wxS(
"Reset Content" );
421 case 206:
return wxS(
"Partial Content" );
422 case 207:
return wxS(
"Multi-Status" );
423 case 208:
return wxS(
"Already Reported" );
424 case 226:
return wxS(
"IM Used" );
426 case 300:
return wxS(
"Multiple Choices" );
427 case 301:
return wxS(
"Moved Permanently" );
428 case 302:
return wxS(
"Found" );
429 case 303:
return wxS(
"See Other" );
430 case 304:
return wxS(
"Not Modified" );
431 case 305:
return wxS(
"Use Proxy (Deprecated)" );
432 case 306:
return wxS(
"Unused" );
433 case 307:
return wxS(
"Temporary Redirect" );
434 case 308:
return wxS(
"Permanent Redirect" );
436 case 400:
return wxS(
"Bad Request" );
437 case 401:
return wxS(
"Unauthorized" );
438 case 402:
return wxS(
"Payment Required (Experimental)" );
439 case 403:
return wxS(
"Forbidden" );
440 case 404:
return wxS(
"Not Found" );
441 case 405:
return wxS(
"Method Not Allowed" );
442 case 406:
return wxS(
"Not Acceptable" );
443 case 407:
return wxS(
"Proxy Authentication Required" );
444 case 408:
return wxS(
"Request Timeout" );
445 case 409:
return wxS(
"Conflict" );
446 case 410:
return wxS(
"Gone" );
447 case 411:
return wxS(
"Length Required" );
448 case 412:
return wxS(
"Payload Too Large" );
449 case 414:
return wxS(
"URI Too Long" );
450 case 415:
return wxS(
"Unsupported Media Type" );
451 case 416:
return wxS(
"Range Not Satisfiable" );
452 case 417:
return wxS(
"Expectation Failed" );
453 case 418:
return wxS(
"I'm a teapot" );
454 case 421:
return wxS(
"Misdirected Request" );
455 case 422:
return wxS(
"Unprocessable Content" );
456 case 423:
return wxS(
"Locked" );
457 case 424:
return wxS(
"Failed Dependency" );
458 case 425:
return wxS(
"Too Early (Experimental)" );
459 case 426:
return wxS(
"Upgrade Required" );
460 case 428:
return wxS(
"Precondition Required" );
461 case 429:
return wxS(
"Too Many Requests" );
462 case 431:
return wxS(
"Request Header Fields Too Large" );
463 case 451:
return wxS(
"Unavailable For Legal Reasons" );
465 case 500:
return wxS(
"Internal Server Error" );
466 case 501:
return wxS(
"Not Implemented" );
467 case 502:
return wxS(
"Bad Gateway" );
468 case 503:
return wxS(
"Service Unavailable" );
469 case 504:
return wxS(
"Gateway Timeout" );
470 case 505:
return wxS(
"HTTP Version Not Supported" );
471 case 506:
return wxS(
"Variant Also Negotiates" );
472 case 507:
return wxS(
"Insufficient Storage" );
473 case 508:
return wxS(
"Loop Detected" );
474 case 510:
return wxS(
"Not Extended" );
475 case 511:
return wxS(
"Network Authentication Required" );
476 default:
return wxS(
"Unknown" );
480 return wxString::Format( wxS(
"%d: %s" ), aHttpCode, codeDescription( aHttpCode ) );
bool checkServerResponse(std::unique_ptr< KICAD_CURL_EASY > &aCurl)
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::string > fp_filters
std::vector< std::pair< std::string, std::tuple< std::string, bool > > > fields