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 auto getCachedParts() { return m_cache; }
64
65private:
66
67 // This is clunky but at the moment the only way to free the pointer after use without KiCad crashing.
68 // at this point we can't use smart pointers as there is a problem with the order of how things are deleted/freed
69 std::unique_ptr<KICAD_CURL_EASY> createCurlEasyObject()
70 {
71
72 std::unique_ptr<KICAD_CURL_EASY> aCurl( new KICAD_CURL_EASY() );
73
74 // prepare curl
75 aCurl->SetHeader( "Accept", "application/json" );
76 aCurl->SetHeader( "Authorization", "Token " + m_source.token );
77
78 return aCurl;
79 }
80
82
83 bool syncCategories();
84
85 bool checkServerResponse( std::unique_ptr<KICAD_CURL_EASY>& aCurl );
86
87 bool boolFromString( const std::any& aVal, bool aDefaultValue = false );
88
89 wxString httpErrorCodeDescription( uint16_t aHttpCode );
90
92
93 // part.id part
94 std::map<std::string, HTTP_LIB_PART> m_cachedParts;
95
96 // part.name part.id category.id
97 std::map<std::string, std::tuple<std::string, std::string>> m_cache;
98
99 bool m_endpointValid = false;
100
101 std::string m_lastError;
102
103 std::vector<HTTP_LIB_CATEGORY> m_categories;
104
105 std::map<std::string, std::string> m_parts;
106
107 const std::string http_endpoint_categories = "categories";
108 const std::string http_endpoint_parts = "parts";
109 const std::string http_endpoint_settings = "settings";
110 const std::string http_endpoint_auth = "authentication";
111
112};
113
114#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()
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
const char *const traceHTTPLib