From 8340079b95735536a0e624075ba79c8cb476f5cb Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 13 Jul 2012 17:57:18 -0400 Subject: [PATCH] cc-4105: added a method in API client corresponding to chunked requests.\nAlso add prepared corresponding apicontroller method --- .../application/controllers/ApiController.php | 13 +++++--- python_apps/api_clients/api_client.cfg | 4 +++ python_apps/api_clients/api_client.py | 32 ++++++++++++------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index db4a73777..036174e62 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -32,6 +32,7 @@ class ApiController extends Zend_Controller_Action ->addActionContext('update-source-status', 'json') ->addActionContext('get-bootstrap-info', 'json') ->addActionContext('get-files-without-replay-gain', 'json') + ->addActionContext('reload-metadata-group', 'json') ->initContext(); } @@ -576,14 +577,16 @@ class ApiController extends Zend_Controller_Action //The key does not have any meaning as of yet but it could potentially correspond //to some unique id. $responses = array(); - Logging::log("inside reloadMetadataGroupAction"); - return; - foreach ($request->getRequest()->getParams() as $action => $raw_json) { + foreach ($request->getParams() as $k => $raw_json) { $info_json = json_decode($raw_json, $assoc=true); - array_push($responses, $this->dispatchMetaDataAction($info_json, $info_json['mode'])); + $mode = $info_json['mode']; + unset($info_json['mode']); + // TODO : uncomment the following line to actually do something + // array_push($responses, $this->dispatchMetaDataAction($info_json, $info_json['mode'])); + // Like wise, remove the following line when done Logging::log( $info_json ); } - // TODO : do something with $responses here instead of doing nothing + die(json_encode( array('successes' => 19, 'fails' => 123) )); } public function reloadMetadataAction() { diff --git a/python_apps/api_clients/api_client.cfg b/python_apps/api_clients/api_client.cfg index a3c04be94..1cbf9e322 100644 --- a/python_apps/api_clients/api_client.cfg +++ b/python_apps/api_clients/api_client.cfg @@ -64,6 +64,10 @@ show_schedule_url = 'recorded-shows/format/json/api_key/%%api_key%%' # URL to upload the recorded show's file to Airtime upload_file_url = 'upload-file/format/json/api_key/%%api_key%%' +# URL to commit multiple updates from media monitor at the same time + +reload_metadata_group = 'reload-metadata-group/format/json/api_key/%%api_key%%' + #number of retries to upload file if connection problem upload_retries = 3 diff --git a/python_apps/api_clients/api_client.py b/python_apps/api_clients/api_client.py index 299c7a66d..d2aea1e45 100644 --- a/python_apps/api_clients/api_client.py +++ b/python_apps/api_clients/api_client.py @@ -340,7 +340,6 @@ class AirTimeApiClient(): md = convert_dict_value_to_utf8(md) - data = urllib.urlencode(md) req = urllib2.Request(url, data) @@ -364,23 +363,34 @@ class AirTimeApiClient(): return response - def send_media_monitor_requests(self, md_list, mode, is_record=False): + def send_media_monitor_requests(self, action_list, is_record=False): logger = self.logger response = None try: - url = "http://%s:%s/%s/%s" % (self.config["base_url"], str(self.config["base_port"]), self.config["api_base"], self.config["update_media_url"]) - url = url.replace("%%api_key%%", self.config["api_key"]) - url = url.replace("%%mode%%", mode) - - md_list = convert_dict_value_to_utf8(md_list) - + url = self.construct_url('reload_metadata_group') + # We are assuming that action_list is a list of dictionaries such + # that every dictionary represents the metadata of a file along + # with a special mode key that is the action to be executed by the + # controller. + valid_actions = [] + # We could get a list of valid_actions in a much shorter way using + # filter but here we prefer a little more verbosity to help + # debugging + for action in action_list: + if not 'mode' in action: + self.logger.debug("Warning: Sending a request element without a 'mode'") + self.logger.debug("Here is the the request: '%s'" % str(action) ) + else: valid_actions.append(action) + md_list = { i : json.dumps(convert_dict_value_to_utf8(md)) for i,md in enumerate(valid_actions) } data = urllib.urlencode(md_list) req = urllib2.Request(url, data) - response = self.get_response_from_server(req) - logger.info("update media %s, filepath: %s, mode: %s", response, md_list['MDATA_KEY_FILEPATH'], mode) + # We no longer have a single mode for every http request. + #logger.info("update media %s, filepath: %s, mode: %s", response, md_list['MDATA_KEY_FILEPATH'], mode) response = json.loads(response) - + # TODO : this request returns a more detailed response of what + # happened through a json array. Hence we should handle errors + # differently if("error" not in response and is_record): url = "http://%s:%s/%s/%s" % (self.config["base_url"], str(self.config["base_port"]), self.config["api_base"], self.config["upload_recorded"]) url = url.replace("%%fileid%%", str(response[u'id']))