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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <any>
24#include <boost/algorithm/string.hpp>
25#include <functional>
26#include <json_common.h>
27#include <mutex>
28
31
32extern const char* const traceHTTPLib;
33
34
44bool setPartExtendedData( const nlohmann::json& aPartJson, HTTP_LIB_PART& aPart );
45
46
48{
49public:
60 using RETRIEVER = std::function<bool( const std::string& aUrl, int& aStatusCode,
61 std::string& aBody, std::string& aError )>;
62
63 HTTP_LIB_CONNECTION( const HTTP_LIB_SOURCE& aSource, bool aTestConnectionNow );
64
65 HTTP_LIB_CONNECTION( const HTTP_LIB_SOURCE& aSource, bool aTestConnectionNow, RETRIEVER aRetriever );
66
67 virtual ~HTTP_LIB_CONNECTION() = default;
68
69 bool IsValidEndpoint() const { return m_endpointValid; }
70
78 bool SelectOne( const std::string& aPartID, HTTP_LIB_PART& aFetchedPart );
79
87 bool SelectAll( const HTTP_LIB_CATEGORY& aCategory, std::vector<HTTP_LIB_PART>& aParts );
88
89 std::string GetLastError() const
90 {
91 std::lock_guard lock( m_queryMutex );
92 return m_lastError;
93 }
94
95 std::vector<HTTP_LIB_CATEGORY> getCategories() const
96 {
97 std::lock_guard lock( m_queryMutex );
98 return m_categories;
99 }
100
101 std::string getCategoryDescription( const std::string& aCategoryName ) const
102 {
103 std::lock_guard lock( m_queryMutex );
104
105 if( m_categoryDescriptions.contains( aCategoryName ) )
106 return m_categoryDescriptions.at( aCategoryName );
107
108 return "";
109 }
110
111 bool HasCachedParts() const
112 {
113 std::lock_guard lock( m_queryMutex );
114 return !m_cache.empty();
115 }
116
125 bool GetCachedPartRelation( const std::string& aPartName, std::string& aPartId, std::string& aCategoryId ) const
126 {
127 std::lock_guard lock( m_queryMutex );
128
129 auto it = m_cache.find( aPartName );
130
131 if( it == m_cache.end() )
132 return false;
133
134 aPartId = std::get<0>( it->second );
135 aCategoryId = std::get<1>( it->second );
136 return true;
137 }
138
140 {
141 std::lock_guard lock( m_queryMutex );
142 m_cachedParts.clear();
143 m_cache.clear();
144 }
145
146private:
148
149 bool syncCategories();
150
157 bool retrieve( const std::string& aUrl, int& aStatusCode, std::string& aBody );
158
159 bool checkServerResponse( int aStatusCode );
160
174 wxString httpErrorCodeDescription( uint16_t aHttpCode );
175
176private:
177 mutable std::mutex m_queryMutex;
178
181 bool m_endpointValid = false;
182 std::string m_lastError;
183
184 std::vector<HTTP_LIB_CATEGORY> m_categories;
185 std::map<std::string, std::string> m_categoryDescriptions;
186 std::map<std::string, std::string> m_parts;
187
188 // part.id part
189 std::map<std::string, HTTP_LIB_PART> m_cachedParts;
190
191 // part.name part.id category.id
192 std::map<std::string, std::tuple<std::string, std::string>> m_cache;
193};
bool GetCachedPartRelation(const std::string &aPartName, std::string &aPartId, std::string &aCategoryId) const
Resolve a cached part name to its part ID and category ID.
std::map< std::string, std::string > m_parts
std::vector< HTTP_LIB_CATEGORY > getCategories() const
std::map< std::string, HTTP_LIB_PART > m_cachedParts
std::map< std::string, std::string > m_categoryDescriptions
bool checkServerResponse(int aStatusCode)
bool retrieve(const std::string &aUrl, int &aStatusCode, std::string &aBody)
Fetch aUrl through the transport.
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
std::function< bool(const std::string &aUrl, int &aStatusCode, std::string &aBody, std::string &aError)> RETRIEVER
Allows replacing CURL fetches with a mock for testing.
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
bool setPartExtendedData(const nlohmann::json &aPartJson, HTTP_LIB_PART &aPart)
Parse the exclusion flags and field content from a part's JSON record into aPart.
Connection parameters for one HTTP library.