| 207 | } |
| 208 | |
| 209 | bool OpenEI::QueryUtilityCompanies(wxArrayString &names, wxString *err) |
| 210 | { |
| 211 | |
| 212 | // OpenEI service returns list of utility companies and their aliases https://openei.org/services/doc/rest/util_cos/?version=3 |
| 213 | wxString url = SamApp::WebApi("urdb_companies_all"); |
| 214 | url.Replace("<SCOPE>", "international"); |
| 215 | |
| 216 | wxString json_data = MyGet(url); |
| 217 | if (json_data.IsEmpty() ) |
| 218 | { |
| 219 | if (err) *err = "Could not retrieve JSON data for utility rate companies."; |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | rapidjson::GenericDocument < rapidjson::UTF16<> > reader; |
| 224 | reader.Parse(json_data.c_str()); |
| 225 | |
| 226 | if (reader.HasParseError()) |
| 227 | { |
| 228 | if (err) *err = wxString::Format("Could not process returned JSON data for utility rate companies.\nRapidJSON ParseErrorCode = %d", reader.GetParseError()); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | if ( reader.HasMember(L"error") ) |
| 233 | { |
| 234 | if (reader[L"error"].HasMember(L"message")) { |
| 235 | wxString str = reader[L"error"][L"message"].GetString(); |
| 236 | if (err) *err = "URDB API error: " + str; |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | names.Clear(); |
| 242 | |
| 243 | if (reader.HasMember(L"items")) { |
| 244 | |
| 245 | if (reader[L"items"].IsArray()) { |
| 246 | |
| 247 | auto item_list = reader[L"items"].GetArray(); |
| 248 | |
| 249 | int count = item_list.Size(); |
| 250 | for (int i = 0; i < count; i++) { |
| 251 | if (item_list[i].HasMember(L"label")) { |
| 252 | wxString buf = item_list[i][L"label"].GetString(); |
| 253 | buf.Replace("&", "&"); |
| 254 | names.Add(buf); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (err) *err = wxEmptyString; |
| 259 | return true; |
| 260 | } |
| 261 | else |
| 262 | return false; |
| 263 | } |
| 264 | else |
| 265 | return false; |
| 266 | |