2
0

ApResolve.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "ApResolve.h"
  2. #include <initializer_list> // for initializer_list
  3. #include <map> // for operator!=, operator==
  4. #include <memory> // for allocator, unique_ptr
  5. #include <string_view> // for string_view
  6. #include <vector> // for vector
  7. #include "HTTPClient.h" // for HTTPClient, HTTPClient::Response
  8. #ifdef BELL_ONLY_CJSON
  9. #include "cJSON.h"
  10. #else
  11. #include "nlohmann/json.hpp" // for basic_json<>::object_t, basic_json
  12. #include "nlohmann/json_fwd.hpp" // for json
  13. #endif
  14. using namespace cspot;
  15. ApResolve::ApResolve(std::string apOverride) {
  16. this->apOverride = apOverride;
  17. }
  18. std::string ApResolve::fetchFirstApAddress() {
  19. if (apOverride != "") {
  20. return apOverride;
  21. }
  22. auto request = bell::HTTPClient::get("https://apresolve.spotify.com/");
  23. std::string_view responseStr = request->body();
  24. // parse json with nlohmann
  25. #ifdef BELL_ONLY_CJSON
  26. cJSON* json = cJSON_Parse(responseStr.data());
  27. auto ap_string = std::string(
  28. cJSON_GetArrayItem(cJSON_GetObjectItem(json, "ap_list"), 0)->valuestring);
  29. cJSON_Delete(json);
  30. return ap_string;
  31. #else
  32. auto json = nlohmann::json::parse(responseStr);
  33. return json["ap_list"][0];
  34. #endif
  35. }