2023-09-19 01:09:21 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 Andre F. K. Iwers <iwers11@gmail.com>
|
2025-01-01 13:30:11 -08:00
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2023-09-19 01:09:21 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef KICAD_HTTP_LIB_CONNECTION_H
|
|
|
|
#define KICAD_HTTP_LIB_CONNECTION_H
|
|
|
|
|
|
|
|
#include <any>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include "http_lib/http_lib_settings.h"
|
|
|
|
#include <kicad_curl/kicad_curl_easy.h>
|
|
|
|
|
|
|
|
extern const char* const traceHTTPLib;
|
|
|
|
|
|
|
|
|
|
|
|
class HTTP_LIB_CONNECTION
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const long DEFAULT_TIMEOUT = 10;
|
|
|
|
|
|
|
|
HTTP_LIB_CONNECTION( const HTTP_LIB_SOURCE& aSource, bool aTestConnectionNow );
|
|
|
|
|
|
|
|
~HTTP_LIB_CONNECTION();
|
|
|
|
|
2023-09-18 21:37:34 -04:00
|
|
|
bool IsValidEndpoint() const;
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-13 13:05:29 -05:00
|
|
|
* Retrieve a single part with full details from the HTTP library.
|
|
|
|
*
|
2023-09-19 01:09:21 +00:00
|
|
|
* @param aPk is the primary key of the part
|
2025-01-13 13:05:29 -05:00
|
|
|
* @param aResult will contain the part if one was found
|
2023-09-19 01:09:21 +00:00
|
|
|
* @return true if aResult was filled; false otherwise
|
|
|
|
*/
|
2023-09-18 21:27:39 -04:00
|
|
|
bool SelectOne( const std::string& aPartID, HTTP_LIB_PART& aFetchedPart );
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-13 13:05:29 -05:00
|
|
|
* Retrieve all parts from a specific category from the HTTP library.
|
|
|
|
*
|
2023-09-19 01:09:21 +00:00
|
|
|
* @param aPk is the primary key of the category
|
|
|
|
* @param aResults will be filled with all parts in that category
|
|
|
|
* @return true if the query succeeded and at least one part was found, false otherwise
|
|
|
|
*/
|
|
|
|
bool SelectAll( const HTTP_LIB_CATEGORY& aCategory, std::vector<HTTP_LIB_PART>& aParts );
|
|
|
|
|
|
|
|
std::string GetLastError() const { return m_lastError; }
|
|
|
|
|
|
|
|
std::vector<HTTP_LIB_CATEGORY> getCategories() const { return m_categories; }
|
|
|
|
|
2024-04-30 11:57:00 +00:00
|
|
|
std::string getCategoryDescription( const std::string& aCategoryName ) const
|
|
|
|
{
|
2025-02-15 19:32:21 +00:00
|
|
|
if( m_categoryDescriptions.contains( aCategoryName ) )
|
|
|
|
return m_categoryDescriptions.at( aCategoryName );
|
|
|
|
else
|
|
|
|
return "";
|
2024-04-30 11:57:00 +00:00
|
|
|
}
|
|
|
|
|
2024-12-31 00:03:37 +00:00
|
|
|
auto& getCachedParts() { return m_cache; }
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
private:
|
2025-01-04 09:21:11 -05:00
|
|
|
// This is clunky but at the moment the only way to free the pointer after use without
|
|
|
|
// KiCad crashing. At this point we can't use smart pointers as there is a problem with
|
|
|
|
// the order of how things are deleted/freed
|
2023-09-19 01:09:21 +00:00
|
|
|
std::unique_ptr<KICAD_CURL_EASY> createCurlEasyObject()
|
|
|
|
{
|
|
|
|
std::unique_ptr<KICAD_CURL_EASY> aCurl( new KICAD_CURL_EASY() );
|
|
|
|
|
|
|
|
// prepare curl
|
|
|
|
aCurl->SetHeader( "Accept", "application/json" );
|
2024-01-23 12:36:10 +11:00
|
|
|
aCurl->SetHeader( "Authorization", "Token " + m_source.token );
|
2025-02-09 10:27:25 -05:00
|
|
|
aCurl->SetFollowRedirects( true );
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
return aCurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidateHTTPLibraryEndpoints();
|
|
|
|
|
|
|
|
bool syncCategories();
|
|
|
|
|
2023-09-18 21:27:39 -04:00
|
|
|
bool checkServerResponse( std::unique_ptr<KICAD_CURL_EASY>& aCurl );
|
2023-09-19 01:09:21 +00:00
|
|
|
|
2023-09-24 13:37:01 +00:00
|
|
|
bool boolFromString( const std::any& aVal, bool aDefaultValue = false );
|
2023-09-19 01:09:21 +00:00
|
|
|
|
2025-01-13 13:05:29 -05:00
|
|
|
/**
|
|
|
|
* HTTP response status codes indicate whether a specific HTTP request has been
|
|
|
|
* successfully completed.
|
|
|
|
*
|
|
|
|
* Responses are grouped in five classes:
|
|
|
|
* - Informational responses (100 ? 199)
|
|
|
|
* - Successful responses (200 ? 299)
|
|
|
|
* - Redirection messages (300 ? 399)
|
|
|
|
* - Client error responses (400 ? 499)
|
|
|
|
* - Server error responses (500 ? 599)
|
|
|
|
*
|
|
|
|
* see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
|
|
|
|
*/
|
2023-09-18 21:27:39 -04:00
|
|
|
wxString httpErrorCodeDescription( uint16_t aHttpCode );
|
2023-09-19 01:09:21 +00:00
|
|
|
|
2024-04-30 11:57:00 +00:00
|
|
|
HTTP_LIB_SOURCE m_source;
|
2023-09-19 01:09:21 +00:00
|
|
|
|
2024-01-18 02:33:16 +00:00
|
|
|
// part.id part
|
|
|
|
std::map<std::string, HTTP_LIB_PART> m_cachedParts;
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
// part.name part.id category.id
|
|
|
|
std::map<std::string, std::tuple<std::string, std::string>> m_cache;
|
|
|
|
|
2023-09-18 21:27:39 -04:00
|
|
|
bool m_endpointValid = false;
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
std::string m_lastError;
|
|
|
|
|
2024-04-30 11:57:00 +00:00
|
|
|
std::vector<HTTP_LIB_CATEGORY> m_categories;
|
|
|
|
std::map<std::string, std::string> m_categoryDescriptions;
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
std::map<std::string, std::string> m_parts;
|
|
|
|
|
|
|
|
const std::string http_endpoint_categories = "categories";
|
|
|
|
const std::string http_endpoint_parts = "parts";
|
|
|
|
const std::string http_endpoint_settings = "settings";
|
|
|
|
const std::string http_endpoint_auth = "authentication";
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //KICAD_HTTP_LIB_CONNECTION_H
|