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
41
42 bool IsValidEndpoint() const;
43
51 bool SelectOne( const std::string& aPartID, HTTP_LIB_PART& aFetchedPart );
52
60 bool SelectAll( const HTTP_LIB_CATEGORY& aCategory, std::vector<HTTP_LIB_PART>& aParts );
61
62 std::string GetLastError() const { return m_lastError; }
63
64 std::vector<HTTP_LIB_CATEGORY> getCategories() const { return m_categories; }
65
66 std::string getCategoryDescription( const std::string& aCategoryName ) const
67 {
68 return m_categoryDescriptions.at( aCategoryName );
69 }
70
71 auto& getCachedParts() { return m_cache; }
72
73private:
74 // This is clunky but at the moment the only way to free the pointer after use without
75 // KiCad crashing. At this point we can't use smart pointers as there is a problem with
76 // the order of how things are deleted/freed
77 std::unique_ptr<KICAD_CURL_EASY> createCurlEasyObject()
78 {
79 std::unique_ptr<KICAD_CURL_EASY> aCurl( new KICAD_CURL_EASY() );
80
81 // prepare curl
82 aCurl->SetHeader( "Accept", "application/json" );
83 aCurl->SetHeader( "Authorization", "Token " + m_source.token );
84
85 return aCurl;
86 }
87
89
90 bool syncCategories();
91
92 bool checkServerResponse( std::unique_ptr<KICAD_CURL_EASY>& aCurl );
93
94 bool boolFromString( const std::any& aVal, bool aDefaultValue = false );
95
109 wxString httpErrorCodeDescription( uint16_t aHttpCode );
110
112
113 // part.id part
114 std::map<std::string, HTTP_LIB_PART> m_cachedParts;
115
116 // part.name part.id category.id
117 std::map<std::string, std::tuple<std::string, std::string>> m_cache;
118
119 bool m_endpointValid = false;
120
121 std::string m_lastError;
122
123 std::vector<HTTP_LIB_CATEGORY> m_categories;
124 std::map<std::string, std::string> m_categoryDescriptions;
125
126 std::map<std::string, std::string> m_parts;
127
128 const std::string http_endpoint_categories = "categories";
129 const std::string http_endpoint_parts = "parts";
130 const std::string http_endpoint_settings = "settings";
131 const std::string http_endpoint_auth = "authentication";
132};
133
134#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)
Retrieve 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)
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