Add function to send horizons requests without Qt

This commit is contained in:
Malin E
2022-05-19 14:23:49 +02:00
parent c18c554c2b
commit 73b40e6e1b
2 changed files with 40 additions and 0 deletions

View File

@@ -24,6 +24,7 @@
#include <modules/space/horizonsfile.h>
#include <openspace/util/httprequest.h>
#include <openspace/util/spicemanager.h>
#include <openspace/util/time.h>
#include <ghoul/filesystem/filesystem.h>
@@ -167,6 +168,44 @@ std::string constructHorizonsUrl(HorizonsType type, const std::string& target,
return url;
}
json sendHorizonsRequest(const std::string& url, std::filesystem::path filePath) {
// Set up HTTP request and download result
auto download = std::make_unique<HttpFileDownload>(
url,
filePath,
HttpFileDownload::Overwrite::Yes
);
HttpFileDownload* dl = download.get();
dl->start();
bool failed = false;
dl->wait();
if (!dl->hasSucceeded()) {
LERROR(fmt::format("Error downloading horizons file with URL {}", dl->url()));
failed = true;
}
if (failed) {
dl->cancel();
}
// Read the entire file into a string
constexpr auto read_size = std::size_t(4096);
std::ifstream stream = std::ifstream(filePath.string().data());
stream.exceptions(std::ios_base::badbit);
std::string answer;
std::string buf = std::string(read_size, '\0');
while (stream.read(&buf[0], read_size)) {
answer.append(buf, 0, stream.gcount());
}
answer.append(buf, 0, stream.gcount());
// convert to a json object
return json::parse(answer);
}
HorizonsResultCode isValidHorizonsAnswer(const json& answer) {
// Signature, source and version
if (auto signature = answer.find("signature"); signature != answer.end()) {

View File

@@ -127,6 +127,7 @@ std::string constructHorizonsUrl(HorizonsType type, const std::string& target,
const std::string& observer, const std::string& startTime,
const std::string& stopTime, const std::string& stepSize,
const std::string& unit);
nlohmann::json sendHorizonsRequest(const std::string& url, std::filesystem::path filePath);
HorizonsResultCode isValidHorizonsAnswer(const nlohmann::json& answer);
HorizonsResultCode isValidHorizonsFile(std::filesystem::path file);
HorizonsResult readHorizonsFile(std::filesystem::path file);