Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Created July 2, 2022 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonymorast/c73470afb2f7a9bfee25f5ac89e9f7fc to your computer and use it in GitHub Desktop.
Save anthonymorast/c73470afb2f7a9bfee25f5ac89e9f7fc to your computer and use it in GitHub Desktop.
namespace FMP
{
enum CACHE_LENGTH { SHORT, LONG };
struct FMPCloudAPI
{
FMPCloudAPI(std::string apiKey) : _apiKey(apiKey)
{
/* TODO: Init cache expiration timers */
}
// ----------------- Endpoints -------------------
Document getStockQuote(const std::string& symbol) const; // no cache
Document getStockQuote(const std::vector<std::string>& symbols) const; // no cache
Document getAvailableCrypto(); // long cache
Document getCryptoPrices() const; // no cache
Document getAvailableForex(); // long cache
Document getAllForexPrices() const; // no cache
Document getAllForexTick() const; // no cache
// ----------------- Helper Functions ---------------
// on construction, create a timer to periodically expire the caches
// ideally, there would be 2 or 3 caches, short, medium, and long or short and long,
// which are all expired at different intervals.
void expireCache() { _expireLongCache(); };
void printDocument(const Document& jsonDoc, std::ostream& stream) const;
private:
std::string _apiKey;
std::string _baseUrl {"https://fmpcloud.io/api/"};
std::map<std::string, std::string> _longJsonContentsCache;
std::string _toCommaDelimited(const std::vector<std::string>& stringVector) const;
std::string _downloadAndGetJsonFile(const std::string& url, const std::string& filename) const; // download data, return as string
std::string _generateFilename(const std::string& endpoint) const;
Document _return(const std::string& contents) const;
Document _returnFromAndUpdateCache(std::string url, std::string cacheKey, std::string filenameKey, CACHE_LENGTH duration);
Document _getNoCache(std::string url, std::string filenameKey) const;
std::string _getCachedContents(std::string key, CACHE_LENGTH length) const;
void _cache(std::string key, std::string results, CACHE_LENGTH length);
void _expireLongCache() { _longJsonContentsCache.clear(); }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment