Cogs.Core
OGC3DTilesAccessParser.cpp
1#include "OGC3DTilesAccessParser.h"
2
3#include "Context.h"
4#include "Engine.h"
5
6#include <Serialization/JsonParser.h>
7
8#include <Foundation/Logging/Logger.h>
9
10#include <cassert>
11
12namespace {
13 const std::string ACCESS_FILE_NAME = "/access.json";
14 Cogs::Logging::Log logger = Cogs::Logging::getLogger("OGC3DTilesAccessParser");
15}
16
17using namespace Cogs::Core;
18
19bool
20parseJSON(std::string_view json, OGC3DTilesAccessParser::TilesAccess* accessData)
21{
22 Document doc = parseJson(json, JsonParseFlags::None);
23 accessData->type = doc["type"].GetString();
24 accessData->url = doc["url"].GetString();
25 accessData->attributionsJSON = "FIXME"; // FIXME: Parse this when we actually need it (2-10-2023 handegard)
26 // Use the name bearerToken to avoid confusion with 'accessToken' in OGC3DTilesComponent
27 accessData->bearerToken = doc["accessToken"].GetString();
28
29 // FIXME: Add proper error handling here (18-10-2023 handegar)
30 return true;
31}
32
37Cogs::Core::DataFetcherManager::FetchId
38OGC3DTilesAccessParser::fetch(Context* context, const std::string& baseURL, OGC3DTilesAccessParser::FetchCallback callback)
39{
40 std::string url = baseURL + ACCESS_FILE_NAME;
41
42 Cogs::Core::DataFetcherManager::FetchId fetchId = DataFetcherManager::fetchAsync(context, url,
43 [context, callback, url](std::unique_ptr<Cogs::FileContents> data) {
44 TilesAccess* accessData = nullptr;
45 if (data.get()) {
46 accessData = new TilesAccess;
47 Memory::MemoryBuffer contentsBuff = data->take();
48 std::string_view jsonStr = std::string_view(static_cast<const char*>(contentsBuff.data()), contentsBuff.size());
49 bool success = parseJSON(jsonStr, accessData);
50 if (!success) {
51 delete accessData;
52 accessData = nullptr;
53 }
54 }
55 else {
56 LOG_ERROR(logger, "Failed fetching TileAccess data from '%s'", url.c_str());
57 }
58
59 auto finishTask = [callback, accessData]() {
60 callback(accessData);
61 };
62
63#if defined(EMSCRIPTEN)
64 (void)context; // Silence unused capture warning.
65 // Cogs.js support - no threading
66 finishTask();
67#else
68 context->engine->runTaskInMainThread(std::move(finishTask));
69#endif
70 });
71
72 return fetchId;
73}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
std::unique_ptr< class Engine > engine
Engine instance.
Definition: Context.h:222
Log implementation class.
Definition: LogManager.h:139
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180