| 379 | |
| 380 | |
| 381 | bool OpenEI::QueryUtilityRates(const wxString &name, std::vector<RateInfo> &rates, wxString *err) |
| 382 | { |
| 383 | rates.clear(); // reset rates list |
| 384 | |
| 385 | wxString utlnm = name; |
| 386 | utlnm.Replace("&", "%26"); |
| 387 | |
| 388 | size_t max_limit = 500; |
| 389 | size_t offset; |
| 390 | size_t old_offset; |
| 391 | size_t count; |
| 392 | wxString url; |
| 393 | wxString json_data; |
| 394 | |
| 395 | |
| 396 | // initial query to get first 500 rates |
| 397 | // OpenEI International Utility Rate Database https://openei.org/services/doc/rest/util_rates/?version=7 |
| 398 | offset = 0; |
| 399 | url = SamApp::WebApi("urdb_rates"); |
| 400 | url.Replace("<LIMIT>", wxString::Format("%d", (int)max_limit)); |
| 401 | url.Replace("<DETAIL>", "minimal"); // don't need rate details for this call |
| 402 | url.Replace("&offset=<OFFSET>", wxString::Format("&offset=%d", (int)offset)); |
| 403 | url.Replace("<UTILITYNAME>", utlnm); |
| 404 | url.Replace("<GUID>", ""); |
| 405 | url.Replace("<APIKEY>", wxString(sam_api_key)); |
| 406 | |
| 407 | |
| 408 | |
| 409 | // change from UTF8 to UTF16 encoding to address unicode characters per SAM GitHub issue 1848 |
| 410 | rapidjson::GenericDocument < rapidjson::UTF16<> > reader; |
| 411 | bool retrieve_rates = true; |
| 412 | bool ret = true; |
| 413 | // up to maxlimit rates added to rates vector |
| 414 | |
| 415 | while (retrieve_rates && ret) { |
| 416 | |
| 417 | json_data = MyGet(url); |
| 418 | if (json_data.IsEmpty()) { |
| 419 | if (err) *err = "Web API call to urdb_rates returned empty JSON for utility company name = " + name; // do not report url because it has private API key |
| 420 | ret = false; |
| 421 | } |
| 422 | else { |
| 423 | reader.Parse(json_data.c_str()); |
| 424 | |
| 425 | if (reader.HasParseError()) { |
| 426 | if (err) *err = wxString::Format("Could not parse JSON for utility company name = %s.\nRapidJSON ParseErrorCode = %d", name, reader.GetParseError()); |
| 427 | ret = false; |
| 428 | } |
| 429 | else if (!reader.HasMember(L"items")) { |
| 430 | if (err) *err = "No rates found for utility company name = " + name; |
| 431 | ret = false; |
| 432 | } |
| 433 | else if (!reader[L"items"].IsArray()) { |
| 434 | if (err) *err = "No rates found for utility company name = " + name; |
| 435 | ret = false; |
| 436 | } |
| 437 | else { |
| 438 | auto item_list = reader[L"items"].GetArray(); |