KiCad PCB EDA Suite
Loading...
Searching...
No Matches
HTTP_LIB_CONNECTION Class Reference

#include <http_lib_connection.h>

Public Types

using RETRIEVER
 Allows replacing CURL fetches with a mock for testing.
 

Public Member Functions

 HTTP_LIB_CONNECTION (const HTTP_LIB_SOURCE &aSource, bool aTestConnectionNow)
 
 HTTP_LIB_CONNECTION (const HTTP_LIB_SOURCE &aSource, bool aTestConnectionNow, RETRIEVER aRetriever)
 
virtual ~HTTP_LIB_CONNECTION ()=default
 
bool IsValidEndpoint () const
 
bool SelectOne (const std::string &aPartID, HTTP_LIB_PART &aFetchedPart)
 Retrieve a single part with full details from the HTTP library.
 
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::string GetLastError () const
 
std::vector< HTTP_LIB_CATEGORYgetCategories () const
 
std::string getCategoryDescription (const std::string &aCategoryName) const
 
bool HasCachedParts () const
 
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.
 
void ClearPartCache ()
 

Private Member Functions

bool validateHttpLibraryEndpoints ()
 
bool syncCategories ()
 
bool retrieve (const std::string &aUrl, int &aStatusCode, std::string &aBody)
 Fetch aUrl through the transport.
 
bool checkServerResponse (int aStatusCode)
 
wxString httpErrorCodeDescription (uint16_t aHttpCode)
 HTTP response status codes indicate whether a specific HTTP request has been successfully completed.
 

Private Attributes

std::mutex m_queryMutex
 
HTTP_LIB_SOURCE m_source
 
RETRIEVER m_retriever
 
bool m_endpointValid = false
 
std::string m_lastError
 
std::vector< HTTP_LIB_CATEGORYm_categories
 
std::map< std::string, std::string > m_categoryDescriptions
 
std::map< std::string, std::string > m_parts
 
std::map< std::string, HTTP_LIB_PARTm_cachedParts
 
std::map< std::string, std::tuple< std::string, std::string > > m_cache
 

Detailed Description

Definition at line 47 of file http_lib_connection.h.

Member Typedef Documentation

◆ RETRIEVER

Initial value:
std::function<bool( const std::string& aUrl, int& aStatusCode,
std::string& aBody, std::string& aError )>

Allows replacing CURL fetches with a mock for testing.

Parameters
aUrlthe URL to retrieve
aStatusCodereceives the HTTP status (0 when no response was received)
aBodyreceives the response body
aErrorreceives a failure reason when the request could not be completed
Returns
true when a response was obtained (regardless of status code); false when the request could not be completed at the transport level

Definition at line 60 of file http_lib_connection.h.

Constructor & Destructor Documentation

◆ HTTP_LIB_CONNECTION() [1/2]

HTTP_LIB_CONNECTION::HTTP_LIB_CONNECTION ( const HTTP_LIB_SOURCE & aSource,
bool aTestConnectionNow )

Definition at line 67 of file http_lib_connection.cpp.

References HTTP_LIB_CONNECTION(), and makeDefaultRetriever().

Referenced by HTTP_LIB_CONNECTION().

◆ HTTP_LIB_CONNECTION() [2/2]

HTTP_LIB_CONNECTION::HTTP_LIB_CONNECTION ( const HTTP_LIB_SOURCE & aSource,
bool aTestConnectionNow,
RETRIEVER aRetriever )

Definition at line 73 of file http_lib_connection.cpp.

References m_retriever, m_source, move, and validateHttpLibraryEndpoints().

◆ ~HTTP_LIB_CONNECTION()

virtual HTTP_LIB_CONNECTION::~HTTP_LIB_CONNECTION ( )
virtualdefault

Member Function Documentation

◆ checkServerResponse()

bool HTTP_LIB_CONNECTION::checkServerResponse ( int aStatusCode)
private

◆ ClearPartCache()

void HTTP_LIB_CONNECTION::ClearPartCache ( )
inline

Definition at line 139 of file http_lib_connection.h.

References m_cache, m_cachedParts, and m_queryMutex.

◆ GetCachedPartRelation()

bool HTTP_LIB_CONNECTION::GetCachedPartRelation ( const std::string & aPartName,
std::string & aPartId,
std::string & aCategoryId ) const
inline

Resolve a cached part name to its part ID and category ID.

Parameters
aPartNamethe name to look up
aPartIdreceives the part ID on a hit
aCategoryIdreceives the category ID on a hit
Returns
true if the name was found; false otherwise

Definition at line 125 of file http_lib_connection.h.

References m_cache, and m_queryMutex.

Referenced by BOOST_AUTO_TEST_CASE().

◆ getCategories()

std::vector< HTTP_LIB_CATEGORY > HTTP_LIB_CONNECTION::getCategories ( ) const
inline

Definition at line 95 of file http_lib_connection.h.

References m_categories, and m_queryMutex.

Referenced by BOOST_AUTO_TEST_CASE().

◆ getCategoryDescription()

std::string HTTP_LIB_CONNECTION::getCategoryDescription ( const std::string & aCategoryName) const
inline

Definition at line 101 of file http_lib_connection.h.

References m_categoryDescriptions, and m_queryMutex.

Referenced by BOOST_AUTO_TEST_CASE().

◆ GetLastError()

std::string HTTP_LIB_CONNECTION::GetLastError ( ) const
inline

◆ HasCachedParts()

bool HTTP_LIB_CONNECTION::HasCachedParts ( ) const
inline

Definition at line 111 of file http_lib_connection.h.

References m_cache, and m_queryMutex.

◆ httpErrorCodeDescription()

wxString HTTP_LIB_CONNECTION::httpErrorCodeDescription ( uint16_t aHttpCode)
private

HTTP response status codes indicate whether a specific HTTP request has been successfully completed.

Responses are grouped in five classes:

Definition at line 468 of file http_lib_connection.cpp.

Referenced by checkServerResponse().

◆ IsValidEndpoint()

bool HTTP_LIB_CONNECTION::IsValidEndpoint ( ) const
inline

◆ retrieve()

bool HTTP_LIB_CONNECTION::retrieve ( const std::string & aUrl,
int & aStatusCode,
std::string & aBody )
private

Fetch aUrl through the transport.

Returns
false (with m_lastError populated) when the transport could not complete the request; true otherwise, even when aStatusCode is not 200.

Definition at line 441 of file http_lib_connection.cpp.

References m_lastError, and m_retriever.

Referenced by SelectAll(), SelectOne(), syncCategories(), and validateHttpLibraryEndpoints().

◆ SelectAll()

bool HTTP_LIB_CONNECTION::SelectAll ( const HTTP_LIB_CATEGORY & aCategory,
std::vector< HTTP_LIB_PART > & aParts )

Retrieve all parts from a specific category from the HTTP library.

Parameters
aCategoryis the category to fetch parts from
aPartswill be filled with all parts in that category
Returns
true if the query succeeded and at least one part was found, false otherwise

Definition at line 384 of file http_lib_connection.cpp.

References _, HTTP_LIB_PART::detailsLoaded, HTTP_LIB_CATEGORY::id, HTTP_LIB_PART::id, IsValidEndpoint(), HTTP_LIB_PART::lastCached, m_cache, m_cachedParts, m_lastError, m_queryMutex, m_source, HTTP_LIB_PART::name, res, retrieve(), setPartExtendedData(), setPartIdNameAndMetadata(), and traceHTTPLib.

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), and fetchCategoryParts().

◆ SelectOne()

bool HTTP_LIB_CONNECTION::SelectOne ( const std::string & aPartID,
HTTP_LIB_PART & aFetchedPart )

◆ syncCategories()

◆ validateHttpLibraryEndpoints()

bool HTTP_LIB_CONNECTION::validateHttpLibraryEndpoints ( )
private

Member Data Documentation

◆ m_cache

std::map<std::string, std::tuple<std::string, std::string> > HTTP_LIB_CONNECTION::m_cache
private

◆ m_cachedParts

std::map<std::string, HTTP_LIB_PART> HTTP_LIB_CONNECTION::m_cachedParts
private

Definition at line 189 of file http_lib_connection.h.

Referenced by ClearPartCache(), SelectAll(), and SelectOne().

◆ m_categories

std::vector<HTTP_LIB_CATEGORY> HTTP_LIB_CONNECTION::m_categories
private

Definition at line 184 of file http_lib_connection.h.

Referenced by getCategories(), and syncCategories().

◆ m_categoryDescriptions

std::map<std::string, std::string> HTTP_LIB_CONNECTION::m_categoryDescriptions
private

Definition at line 185 of file http_lib_connection.h.

Referenced by getCategoryDescription(), and syncCategories().

◆ m_endpointValid

bool HTTP_LIB_CONNECTION::m_endpointValid = false
private

Definition at line 181 of file http_lib_connection.h.

Referenced by IsValidEndpoint(), and validateHttpLibraryEndpoints().

◆ m_lastError

std::string HTTP_LIB_CONNECTION::m_lastError
private

◆ m_parts

std::map<std::string, std::string> HTTP_LIB_CONNECTION::m_parts
private

Definition at line 186 of file http_lib_connection.h.

◆ m_queryMutex

std::mutex HTTP_LIB_CONNECTION::m_queryMutex
mutableprivate

◆ m_retriever

RETRIEVER HTTP_LIB_CONNECTION::m_retriever
private

Definition at line 180 of file http_lib_connection.h.

Referenced by HTTP_LIB_CONNECTION(), and retrieve().

◆ m_source

HTTP_LIB_SOURCE HTTP_LIB_CONNECTION::m_source
private

The documentation for this class was generated from the following files: