@@ -203,17 +203,46 @@ def get_stat_all(places, stat_vars):
203203 }
204204 """
205205 url = utils ._API_ROOT + utils ._API_ENDPOINTS ['get_stat_all' ]
206- req_json = {'stat_vars' : stat_vars , 'places' : places }
207-
208- # Send the request
209- res_json = utils ._send_request (url , req_json = req_json , use_payload = False )
210-
211- if 'placeData' not in res_json :
212- raise ValueError ('No data in response.' )
213-
214- # Unnest the REST response for keys that have single-element values.
215- place_statvar_series = collections .defaultdict (dict )
216- for place_dcid , place in res_json ['placeData' ].items ():
217- for stat_var_dcid , stat_var in place ['statVarData' ].items ():
218- place_statvar_series [place_dcid ][stat_var_dcid ] = stat_var
219- return dict (place_statvar_series )
206+ places = list (places )
207+ # Get number of batches via an arithmetic ceiling trick:
208+ # 11//10 rounds down to 1.
209+ # -11//10 rounds down to -2.
210+ # We can divide with, then remove the negative to get the ceiling.
211+ batches = - (- len (places ) // utils ._QUERY_BATCH_SIZE )
212+ res = {}
213+ for i in range (batches ):
214+ req_json = {
215+ 'stat_vars' :
216+ stat_vars ,
217+ 'places' :
218+ places [i * utils ._QUERY_BATCH_SIZE :(i + 1 ) *
219+ utils ._QUERY_BATCH_SIZE ]
220+ }
221+ # Send the request
222+ res_json = utils ._send_request (url ,
223+ req_json = req_json ,
224+ use_payload = False )
225+ if 'placeData' not in res_json :
226+ # The REST API spec will always return a dictionary under
227+ # placeData, even if no places exist or have no
228+ # data. If no Places are provided, REST will return an
229+ # error, which will have been caught and passed on in
230+ # _send_request.
231+ raise ValueError ("Unexpected response from REST stat/all API." )
232+
233+ # Unnest the REST response for keys that have single-element values.
234+ place_statvar_series = collections .defaultdict (dict )
235+ for place_dcid , place in res_json ['placeData' ].items ():
236+ stat_var_data = place .get ('statVarData' )
237+ if not stat_var_data :
238+ # The REST API spec will always return a dictionary under
239+ # statVarData, even if no StatVars exist or have no
240+ # data. If no StatVars are provided, REST will return an
241+ # error, which will have been caught and passed on in
242+ # _send_request.
243+ raise ValueError ("Unexpected response from REST stat/all API." )
244+ for stat_var_dcid , stat_var in stat_var_data .items ():
245+ place_statvar_series [place_dcid ][stat_var_dcid ] = stat_var
246+ res .update (dict (place_statvar_series ))
247+
248+ return res
0 commit comments