SlHelpers
Loading...
Searching...
No Matches
Curl.h
1// SPDX-License-Identifier: GPL-2.0-only
2
3#pragma once
4
5#include <chrono>
6#include <filesystem>
7#include <optional>
8#include <ostream>
9#include <string>
10
11typedef void CURL;
12
13namespace SlCurl {
14
18class LibCurl {
19public:
26 ~LibCurl();
27
35 bool downloadToStream(const std::string &url, const std::ostream &stream,
36 unsigned *HTTPErrorCode = nullptr);
37
45 bool downloadToFile(const std::string &url, const std::filesystem::path &file,
46 unsigned *HTTPErrorCode = nullptr);
47
48
55 std::optional<std::string> download(const std::string &url,
56 unsigned *HTTPErrorCode = nullptr);
57
64 static std::optional<std::string> singleDownload(const std::string &url,
65 unsigned *HTTPErrorCode = nullptr);
66
74 static bool singleDownloadToFile(const std::string &url,
75 const std::filesystem::path &file,
76 unsigned *HTTPErrorCode = nullptr);
77
86 static bool isDownloadNeeded(const std::filesystem::path &filePath, bool &fileAlreadyExists,
87 bool forceRefresh, const std::chrono::hours &hours);
88
98 static std::filesystem::path fetchFileIfNeeded(const std::filesystem::path &filePath,
99 const std::string &url,
100 bool forceRefresh, bool ignoreErrors,
101 const std::chrono::hours &hours);
102
104 static const std::string &lastError() { return m_lastError; }
105private:
106 CURL *handle;
107 static thread_local std::string m_lastError;
108};
109
110}
static std::optional< std::string > singleDownload(const std::string &url, unsigned *HTTPErrorCode=nullptr)
Download url to a string (and create LibCurl temporarily).
static const std::string & lastError()
Return the last error string if some.
Definition Curl.h:104
LibCurl()
Construct LibCurl.
static std::filesystem::path fetchFileIfNeeded(const std::filesystem::path &filePath, const std::string &url, bool forceRefresh, bool ignoreErrors, const std::chrono::hours &hours)
Fetch url and store into filePath.
bool downloadToFile(const std::string &url, const std::filesystem::path &file, unsigned *HTTPErrorCode=nullptr)
Download url to file.
std::optional< std::string > download(const std::string &url, unsigned *HTTPErrorCode=nullptr)
Download url to a string.
static bool isDownloadNeeded(const std::filesystem::path &filePath, bool &fileAlreadyExists, bool forceRefresh, const std::chrono::hours &hours)
Check if filePath is old enough that it should be downloaded.
bool downloadToStream(const std::string &url, const std::ostream &stream, unsigned *HTTPErrorCode=nullptr)
Download url to stream.
static bool singleDownloadToFile(const std::string &url, const std::filesystem::path &file, unsigned *HTTPErrorCode=nullptr)
Download url to file (and create LibCurl temporarily).