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 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef KICAD_HTTP_LIB_CONNECTION_H
21#define KICAD_HTTP_LIB_CONNECTION_H
22
23#include <any>
24#include <boost/algorithm/string.hpp>
25
28
29extern const char* const traceHTTPLib;
30
31
33{
34public:
35 static const long DEFAULT_TIMEOUT = 10;
36
37 HTTP_LIB_CONNECTION( const HTTP_LIB_SOURCE& aSource, bool aTestConnectionNow );
38
40
41 bool IsValidEndpoint() const;
42
49 bool SelectOne( const std::string& aPartID, HTTP_LIB_PART& aFetchedPart );
50
57 bool SelectAll( const HTTP_LIB_CATEGORY& aCategory, std::vector<HTTP_LIB_PART>& aParts );
58
59 std::string GetLastError() const { return m_lastError; }
60
61 std::vector<HTTP_LIB_CATEGORY> getCategories() const { return m_categories; }
62
63 std::string getCategoryDescription( const std::string& aCategoryName ) const
64 {
65 return m_categoryDescriptions.at( aCategoryName );
66 }
67
68 auto getCachedParts() { return m_cache; }
69
70private:
71 // This is clunky but at the moment the only way to free the pointer after use without KiCad crashing.
72 // at this point we can't use smart pointers as there is a problem with the order of how things are deleted/freed
73 std::unique_ptr<KICAD_CURL_EASY> createCurlEasyObject()
74 {
75 std::unique_ptr<KICAD_CURL_EASY> aCurl( new KICAD_CURL_EASY() );
76
77 // prepare curl
78 aCurl->SetHeader( "Accept", "application/json" );
79 aCurl->SetHeader( "Authorization", "Token " + m_source.token );
80
81 return aCurl;
82 }
83
85
86 bool syncCategories();
87
88 bool checkServerResponse( std::unique_ptr<KICAD_CURL_EASY>& aCurl );
89
90 bool boolFromString( const std::any& aVal, bool aDefaultValue = false );
91
92 wxString httpErrorCodeDescription( uint16_t aHttpCode );
93
95
96 // part.id part
97 std::map<std::string, HTTP_LIB_PART> m_cachedParts;
98
99 // part.name part.id category.id
100 std::map<std::string, std::tuple<std::string, std::string>> m_cache;
101
102 bool m_endpointValid = false;
103
104 std::string m_lastError;
105
106 std::vector<HTTP_LIB_CATEGORY> m_categories;
107 std::map<std::string, std::string> m_categoryDescriptions;
108
109 std::map<std::string, std::string> m_parts;
110
111 const std::string http_endpoint_categories = "categories";
112 const std::string http_endpoint_parts = "parts";
113 const std::string http_endpoint_settings = "settings";
114 const std::string http_endpoint_auth = "authentication";
115};
116
117#endif //KICAD_HTTP_LIB_CONNECTION_H
const std::string http_endpoint_parts
bool checkServerResponse(std::unique_ptr< KICAD_CURL_EASY > &aCurl)
const std::string http_endpoint_categories
std::map< std::string, std::string > m_parts
static const long DEFAULT_TIMEOUT
const std::string http_endpoint_settings
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)
Retrieves all parts from a specific category from the HTTP library.
std::map< std::string, std::tuple< std::string, std::string > > m_cache
std::vector< HTTP_LIB_CATEGORY > m_categories
const std::string http_endpoint_auth
HTTP_LIB_SOURCE m_source
bool SelectOne(const std::string &aPartID, HTTP_LIB_PART &aFetchedPart)
Retrieves a single part with full details from the HTTP library.
wxString httpErrorCodeDescription(uint16_t aHttpCode)
std::string GetLastError() const
std::string getCategoryDescription(const std::string &aCategoryName) const
const char *const traceHTTPLib