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#pragma once
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 virtual ~HTTP_LIB_CONNECTION() = default;
39
40 bool IsValidEndpoint() const { return m_endpointValid; }
41
49 bool SelectOne( const std::string& aPartID, HTTP_LIB_PART& aFetchedPart );
50
58 bool SelectAll( const HTTP_LIB_CATEGORY& aCategory, std::vector<HTTP_LIB_PART>& aParts );
59
60 std::string GetLastError() const { return m_lastError; }
61
62 std::vector<HTTP_LIB_CATEGORY> getCategories() const { return m_categories; }
63
64 std::string getCategoryDescription( const std::string& aCategoryName ) const
65 {
66 if( m_categoryDescriptions.contains( aCategoryName ) )
67 return m_categoryDescriptions.at( aCategoryName );
68 else
69 return "";
70 }
71
72 auto& GetCachedParts() { return m_cache; }
73
74private:
75 // This is clunky but at the moment the only way to free the pointer after use without
76 // KiCad crashing. At this point we can't use smart pointers as there is a problem with
77 // the order of how things are deleted/freed
78 std::unique_ptr<KICAD_CURL_EASY> createCurlEasyObject()
79 {
80 std::unique_ptr<KICAD_CURL_EASY> aCurl( new KICAD_CURL_EASY() );
81
82 // prepare curl
83 aCurl->SetHeader( "Accept", "application/json" );
84 aCurl->SetHeader( "Authorization", "Token " + m_source.token );
85 aCurl->SetFollowRedirects( true );
86
87 return aCurl;
88 }
89
91
92 bool syncCategories();
93
94 bool checkServerResponse( std::unique_ptr<KICAD_CURL_EASY>& aCurl );
95
109 wxString httpErrorCodeDescription( uint16_t aHttpCode );
110
111private:
113 bool m_endpointValid = false;
114 std::string m_lastError;
115
116 std::vector<HTTP_LIB_CATEGORY> m_categories;
117 std::map<std::string, std::string> m_categoryDescriptions;
118 std::map<std::string, std::string> m_parts;
119
120 // part.id part
121 std::map<std::string, HTTP_LIB_PART> m_cachedParts;
122
123 // part.name part.id category.id
124 std::map<std::string, std::tuple<std::string, std::string>> m_cache;
125};
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