KiCad PCB EDA Suite
Loading...
Searching...
No Matches
http_lib_connection.h
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 Andre F. K. Iwers <[email protected]>
5 * Copyright The 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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <any>
24#include <boost/algorithm/string.hpp>
25#include <json_common.h>
26
29
30extern const char* const traceHTTPLib;
31
32
42bool setPartExtendedData( const nlohmann::json& aPartJson, HTTP_LIB_PART& aPart );
43
44
46{
47public:
48 static const long DEFAULT_TIMEOUT = 10;
49
50 HTTP_LIB_CONNECTION( const HTTP_LIB_SOURCE& aSource, bool aTestConnectionNow );
51 virtual ~HTTP_LIB_CONNECTION() = default;
52
53 bool IsValidEndpoint() const { return m_endpointValid; }
54
62 bool SelectOne( const std::string& aPartID, HTTP_LIB_PART& aFetchedPart );
63
71 bool SelectAll( const HTTP_LIB_CATEGORY& aCategory, std::vector<HTTP_LIB_PART>& aParts );
72
73 std::string GetLastError() const { return m_lastError; }
74
75 std::vector<HTTP_LIB_CATEGORY> getCategories() const { return m_categories; }
76
77 std::string getCategoryDescription( const std::string& aCategoryName ) const
78 {
79 if( m_categoryDescriptions.contains( aCategoryName ) )
80 return m_categoryDescriptions.at( aCategoryName );
81 else
82 return "";
83 }
84
85 auto& GetCachedParts() { return m_cache; }
86
87private:
88 // This is clunky but at the moment the only way to free the pointer after use without
89 // KiCad crashing. At this point we can't use smart pointers as there is a problem with
90 // the order of how things are deleted/freed
91 std::unique_ptr<KICAD_CURL_EASY> createCurlEasyObject()
92 {
93 std::unique_ptr<KICAD_CURL_EASY> aCurl( new KICAD_CURL_EASY() );
94
95 // prepare curl
96 aCurl->SetHeader( "Accept", "application/json" );
97 aCurl->SetHeader( "Authorization", "Token " + m_source.token );
98 aCurl->SetFollowRedirects( true );
99
100 return aCurl;
101 }
102
104
105 bool syncCategories();
106
107 bool checkServerResponse( std::unique_ptr<KICAD_CURL_EASY>& aCurl );
108
122 wxString httpErrorCodeDescription( uint16_t aHttpCode );
123
124private:
126 bool m_endpointValid = false;
127 std::string m_lastError;
128
129 std::vector<HTTP_LIB_CATEGORY> m_categories;
130 std::map<std::string, std::string> m_categoryDescriptions;
131 std::map<std::string, std::string> m_parts;
132
133 // part.id part
134 std::map<std::string, HTTP_LIB_PART> m_cachedParts;
135
136 // part.name part.id category.id
137 std::map<std::string, std::tuple<std::string, std::string>> m_cache;
138};
bool checkServerResponse(std::unique_ptr< KICAD_CURL_EASY > &aCurl)
std::map< std::string, std::string > m_parts
static const long DEFAULT_TIMEOUT
std::vector< HTTP_LIB_CATEGORY > getCategories() const
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 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
virtual ~HTTP_LIB_CONNECTION()=default
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.
std::string GetLastError() const
std::string getCategoryDescription(const std::string &aCategoryName) const
const char *const traceHTTPLib
bool setPartExtendedData(const nlohmann::json &aPartJson, HTTP_LIB_PART &aPart)
Parse the exclusion flags and field content from a part's JSON record into aPart.