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 along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef KICAD_HTTP_LIB_CONNECTION_H
22#define KICAD_HTTP_LIB_CONNECTION_H
23
24#include <any>
25#include <boost/algorithm/string.hpp>
26
29
30extern const char* const traceHTTPLib;
31
32
34{
35public:
36 static const long DEFAULT_TIMEOUT = 10;
37
38 HTTP_LIB_CONNECTION( const HTTP_LIB_SOURCE& aSource, bool aTestConnectionNow );
39 virtual ~HTTP_LIB_CONNECTION() = default;
40
41 bool IsValidEndpoint() const { return m_endpointValid; }
42
50 bool SelectOne( const std::string& aPartID, HTTP_LIB_PART& aFetchedPart );
51
59 bool SelectAll( const HTTP_LIB_CATEGORY& aCategory, std::vector<HTTP_LIB_PART>& aParts );
60
61 std::string GetLastError() const { return m_lastError; }
62
63 std::vector<HTTP_LIB_CATEGORY> getCategories() const { return m_categories; }
64
65 std::string getCategoryDescription( const std::string& aCategoryName ) const
66 {
67 if( m_categoryDescriptions.contains( aCategoryName ) )
68 return m_categoryDescriptions.at( aCategoryName );
69 else
70 return "";
71 }
72
73 auto& GetCachedParts() { return m_cache; }
74
75private:
76 // This is clunky but at the moment the only way to free the pointer after use without
77 // KiCad crashing. At this point we can't use smart pointers as there is a problem with
78 // the order of how things are deleted/freed
79 std::unique_ptr<KICAD_CURL_EASY> createCurlEasyObject()
80 {
81 std::unique_ptr<KICAD_CURL_EASY> aCurl( new KICAD_CURL_EASY() );
82
83 // prepare curl
84 aCurl->SetHeader( "Accept", "application/json" );
85 aCurl->SetHeader( "Authorization", "Token " + m_source.token );
86 aCurl->SetFollowRedirects( true );
87
88 return aCurl;
89 }
90
92
93 bool syncCategories();
94
95 bool checkServerResponse( std::unique_ptr<KICAD_CURL_EASY>& aCurl );
96
97 bool boolFromString( const std::any& aVal, bool aDefaultValue = false );
98
112 wxString httpErrorCodeDescription( uint16_t aHttpCode );
113
114private:
116 bool m_endpointValid = false;
117 std::string m_lastError;
118
119 std::vector<HTTP_LIB_CATEGORY> m_categories;
120 std::map<std::string, std::string> m_categoryDescriptions;
121 std::map<std::string, std::string> m_parts;
122
123 // part.id part
124 std::map<std::string, HTTP_LIB_PART> m_cachedParts;
125
126 // part.name part.id category.id
127 std::map<std::string, std::tuple<std::string, std::string>> m_cache;
128};
129
130#endif //KICAD_HTTP_LIB_CONNECTION_H
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 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
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