#pragma once
#include <whiteout/common_types.h>
#include <whiteout/interfaces.h>
#include <whiteout/storages/casc/types.h>
#include "cdn_cache.h"
#include <atomic>
#include <functional>
#include <optional>
#include <string>
#include <vector>
namespace whiteout::storages::casc {
class CdnFetcher {
public:
using FetchCallback = std::function<void(std::optional<std::vector<u8>>)>;
CdnFetcher(interfaces::HttpHandler* http, std::vector<CdnServer> servers, CdnCache* cache);
std::optional<std::vector<u8>> fetch(const std::string& pathType, const std::string& keyHex);
std::optional<std::vector<u8>> fetchRange(const std::string& archiveKeyHex, u64 offset,
u32 size);
std::optional<std::vector<u8>> fetchUrl(const std::string& url);
void fetchAsync(const std::string& pathType, const std::string& keyHex, FetchCallback callback);
void fetchRangeAsync(const std::string& archiveKeyHex, u64 offset, u32 size,
FetchCallback callback);
bool supportsHttp2() const;
private:
interfaces::HttpHandler* m_http;
std::vector<CdnServer> m_servers;
CdnCache* m_cache; std::atomic<size_t> m_currentCdn{0};
std::string buildUrl(const CdnServer& server, const std::string& pathType,
const std::string& keyHex) const;
std::optional<std::vector<u8>> fetchWithFailover(const std::string& pathType,
const std::string& keyHex);
void getUrlAsync(const std::string& url,
std::function<void(interfaces::HttpResponse)> callback);
};
}