hueplusplus  1.0.0
Public Member Functions | List of all members
hueplusplus::APICache Class Reference

Caches API GET requests and refreshes regularly. More...

#include <hueplusplus/APICache.h>

Public Member Functions

 APICache (std::shared_ptr< APICache > baseCache, const std::string &subEntry, std::chrono::steady_clock::duration refresh)
 Constructs APICache which forwards to a base cache. More...
 
 APICache (const std::string &path, const HueCommandAPI &commands, std::chrono::steady_clock::duration refresh, const nlohmann::json &initial)
 Constructs APICache with an own internal json cache. More...
 
void refresh ()
 Refresh cache now. More...
 
nlohmann::json & getValue ()
 Get cached value, refresh if necessary. More...
 
const nlohmann::json & getValue () const
 Get cached value, does not refresh. More...
 
void setRefreshDuration (std::chrono::steady_clock::duration refreshDuration)
 Set duration after which the cache is refreshed. More...
 
std::chrono::steady_clock::duration getRefreshDuration () const
 Get duration between refreshes. More...
 
HueCommandAPIgetCommandAPI ()
 Get HueCommandAPI used for requests. More...
 
const HueCommandAPIgetCommandAPI () const
 Get HueCommandAPI used for requests. More...
 
std::string getRequestPath () const
 Get path the cache is refreshed from. More...
 

Detailed Description

Caches API GET requests and refreshes regularly.

Constructor & Destructor Documentation

◆ APICache() [1/2]

hueplusplus::APICache::APICache ( std::shared_ptr< APICache baseCache,
const std::string &  subEntry,
std::chrono::steady_clock::duration  refresh 
)

Constructs APICache which forwards to a base cache.

Parameters
baseCacheBase cache providing a parent state, must not be nullptr
subEntryKey of the child to use in the base cache
refreshInterval between cache refreshing. May be 0 to always refresh. This is independent from the base cache refresh rate.

Refreshes only part of the base cache.

◆ APICache() [2/2]

hueplusplus::APICache::APICache ( const std::string &  path,
const HueCommandAPI commands,
std::chrono::steady_clock::duration  refresh,
const nlohmann::json &  initial 
)

Constructs APICache with an own internal json cache.

Parameters
pathURL appended after username, may be empty.
commandsHueCommandAPI for making API requests.
refreshInterval between cache refreshing. May be 0 to always refresh.
initialInitial value, may be null. If present, assumes the value is up to date.

Member Function Documentation

◆ getCommandAPI() [1/2]

HueCommandAPI & hueplusplus::APICache::getCommandAPI ( )

Get HueCommandAPI used for requests.

◆ getCommandAPI() [2/2]

const HueCommandAPI & hueplusplus::APICache::getCommandAPI ( ) const

Get HueCommandAPI used for requests.

◆ getRefreshDuration()

std::chrono::steady_clock::duration hueplusplus::APICache::getRefreshDuration ( ) const

Get duration between refreshes.

◆ getRequestPath()

std::string hueplusplus::APICache::getRequestPath ( ) const

Get path the cache is refreshed from.

Returns
Request path as passed to HueCommandAPI::GETRequest

◆ getValue() [1/2]

nlohmann::json & hueplusplus::APICache::getValue ( )

Get cached value, refresh if necessary.

Exceptions
std::system_errorwhen system or socket operations fail
HueExceptionwhen response contained no body
HueAPIResponseExceptionwhen response contains an error
nlohmann::json::parse_errorwhen response could not be parsed

◆ getValue() [2/2]

const nlohmann::json & hueplusplus::APICache::getValue ( ) const

Get cached value, does not refresh.

Exceptions
HueExceptionwhen no previous request was cached

◆ refresh()

void hueplusplus::APICache::refresh ( )

Refresh cache now.

Exceptions
std::system_errorwhen system or socket operations fail
HueExceptionwhen response contained no body
HueAPIResponseExceptionwhen response contains an error
nlohmann::json::parse_errorwhen response could not be parsed

If there is a base cache, refreshes only the used part of that cache.

◆ setRefreshDuration()

void hueplusplus::APICache::setRefreshDuration ( std::chrono::steady_clock::duration  refreshDuration)

Set duration after which the cache is refreshed.

Parameters
refreshDurationInterval between cache refreshing. May be 0 to always refresh, or c_refreshNever to never refresh.

If the new refresh duration is exceeded, does not refresh immediately. Instead, the next non-const getValue() call will refresh the value. This is to reduce the number of unneccessary requests.


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