def callback(self, msg): if self.binary: req = StringIO() msg.serialize(req) reqs = req.getvalue() content_type = ROS_MSG_MIMETYPE_WITH_TYPE(self.rostype) else: req = msgconv.extract_values(msg) req['_format'] = 'ros' reqs = json.dumps(req) content_type = 'application/json' wsreq = urllib2.Request(self.url.encode('utf-8'), data=reqs, headers={'Content-Type': content_type}) try: wsres = urllib2.urlopen(wsreq) except Exception, e: print content_type print reqs raise e
def _handle_get(self, environ, start_response): path = environ['PATH_INFO'] full = get_query_bool(environ['QUERY_STRING'], 'full') kwargs = urlparse.parse_qs(environ['QUERY_STRING'], keep_blank_values=True) for key in kwargs: kwargs[key] = kwargs[key][-1] if self.base_path and path.startswith(self.base_path): path = path[len(self.base_path):] json_suffix = '.json' jsn = False handler = NullTransform() last_path = path.split('/')[-1].split('.') if len(last_path) > 1: file_type = last_path[-1] path = path[:-(len(file_type) + 1)] # cuts suffix ('json') and preceding period if file_type in mappings: handler = mappings.get(file_type) else: handler = BadTransform() else: jsn = get_query_bool(environ['QUERY_STRING'], 'json') use_ros = environ.get('HTTP_ACCEPT', '').find(ROS_MSG_MIMETYPE) != -1 suffix = get_suffix(path) # TODO: Figure out what this does if path == CONFIG_PATH: dfile = definitions.manifest(self.services, self.topics, self.actions, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') if not suffix: if not self.topics.has_key(path): for action_suffix in [ Action.STATUS_SUFFIX, Action.RESULT_SUFFIX, Action.FEEDBACK_SUFFIX ]: action_name = path[:-(len(action_suffix) + 1)] if path.endswith('/' + action_suffix ) and self.actions.has_key(action_name): action = self.actions[action_name] msg = action.get(action_suffix) break else: return response_404(start_response) else: topic = self.topics[path] if not topic.allow_sub: return response_405(start_response) msg = topic.get() if handler and handler.valid(): return handler.get(start_response, msg, **kwargs) elif use_ros: content_type = ROS_MSG_MIMETYPE output_data = StringIO() if msg is not None: msg.serialize(output_data) output_data = output_data.getvalue() else: content_type = 'application/json' output_data = msgconv.extract_values( msg) if msg is not None else None output_data = json.dumps(output_data) return response_200(start_response, output_data, content_type=content_type) path = path[:-(len(suffix) + 1)] if suffix == MSG_PATH and self.topics.has_key(path): return response_200(start_response, definitions.get_topic_msg(self.topics[path]), content_type='text/plain') elif suffix == SRV_PATH and self.services.has_key(path): return response_200(start_response, definitions.get_service_srv( self.services[path]), content_type='text/plain') elif suffix == ACTION_PATH and self.actions.has_key(path): return response_200(start_response, definitions.get_action_action( self.actions[path]), content_type='text/plain') elif suffix == CONFIG_PATH: if self.services.has_key(path): service_name = path service = self.services[service_name] dfile = definitions.describe_service(service_name, service, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') elif self.topics.has_key(path): topic_name = path topic = self.topics[topic_name] dfile = definitions.describe_topic(topic_name, topic, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') elif self.actions.has_key(path): action_name = path action = self.actions[action_name] dfile = definitions.describe_action(action_name, action, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') else: for suffix in [ Action.STATUS_SUFFIX, Action.RESULT_SUFFIX, Action.FEEDBACK_SUFFIX, Action.GOAL_SUFFIX, Action.CANCEL_SUFFIX ]: if path.endswith('/' + suffix): path = path[:-(len(suffix) + 1)] if self.actions.has_key(path): action_name = path action = self.actions[action_name] dfile = definitions.describe_action_topic( action_name, suffix, action, full=full) if jsn: return response_200( start_response, str(dfile.tojson()), content_type='application/json') else: return response_200( start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') return response_404(start_response) else: return response_404(start_response)
def _handle_get(self, environ, start_response): path = environ['PATH_INFO'] full = get_query_bool(environ['QUERY_STRING'], 'full') kwargs = urlparse.parse_qs(environ['QUERY_STRING'], keep_blank_values=True) for key in kwargs: kwargs[key] = kwargs[key][-1] if self.base_path and path.startswith(self.base_path): path = path[len(self.base_path):] json_suffix = '.json' jsn = False handler = NullTransform() last_path = path.split('/')[-1].split('.') if len(last_path) > 1: file_type = last_path[-1] path = path[:-(len(file_type) + 1)] # cuts suffix ('json') and preceding period if file_type in mappings: handler = mappings.get(file_type) else: handler = BadTransform() else: jsn = get_query_bool(environ['QUERY_STRING'], 'json') use_ros = environ.get('HTTP_ACCEPT','').find(ROS_MSG_MIMETYPE) != -1 suffix = get_suffix(path) # TODO: Figure out what this does if path == CONFIG_PATH: dfile = definitions.manifest(self.services, self.topics, self.actions, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') if not suffix: if not self.topics.has_key(path): for action_suffix in [Action.STATUS_SUFFIX,Action.RESULT_SUFFIX,Action.FEEDBACK_SUFFIX]: action_name = path[:-(len(action_suffix)+1)] if path.endswith('/' + action_suffix) and self.actions.has_key(action_name): action = self.actions[action_name] msg = action.get(action_suffix) break else: return response_404(start_response) else: topic = self.topics[path] if not topic.allow_sub: return response_405(start_response) msg = topic.get() if handler and handler.valid(): return handler.get(start_response, msg, **kwargs) elif use_ros: content_type = ROS_MSG_MIMETYPE output_data = StringIO() if msg is not None: msg.serialize(output_data) output_data = output_data.getvalue() else: content_type = 'application/json' output_data = msgconv.extract_values(msg) if msg is not None else None output_data = json.dumps(output_data) return response_200(start_response, output_data, content_type=content_type) path = path[:-(len(suffix)+1)] if suffix == MSG_PATH and self.topics.has_key(path): return response_200(start_response, definitions.get_topic_msg(self.topics[path]), content_type='text/plain') elif suffix == SRV_PATH and self.services.has_key(path): return response_200(start_response, definitions.get_service_srv(self.services[path]), content_type='text/plain') elif suffix == ACTION_PATH and self.actions.has_key(path): return response_200(start_response, definitions.get_action_action(self.actions[path]), content_type='text/plain') elif suffix == CONFIG_PATH: if self.services.has_key(path): service_name = path service = self.services[service_name] dfile = definitions.describe_service(service_name, service, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') elif self.topics.has_key(path): topic_name = path topic = self.topics[topic_name] dfile = definitions.describe_topic(topic_name, topic, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') elif self.actions.has_key(path): action_name = path action = self.actions[action_name] dfile = definitions.describe_action(action_name, action, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') else: for suffix in [Action.STATUS_SUFFIX,Action.RESULT_SUFFIX,Action.FEEDBACK_SUFFIX,Action.GOAL_SUFFIX,Action.CANCEL_SUFFIX]: if path.endswith('/' + suffix): path = path[:-(len(suffix)+1)] if self.actions.has_key(path): action_name = path action = self.actions[action_name] dfile = definitions.describe_action_topic(action_name, suffix, action, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') return response_404(start_response) else: return response_404(start_response)
def _handle_get(self, environ, start_response): path = environ['PATH_INFO'][1:] full = get_query_bool(environ['QUERY_STRING'], 'full') json_suffix = '.json' if path.endswith(json_suffix): path = path[:-len(json_suffix)] jsn = True else: jsn = get_query_bool(environ['QUERY_STRING'], 'json') use_ros = environ.get('HTTP_ACCEPT','').find(ROS_MSG_MIMETYPE) != -1 suffix = get_suffix(path) if path == CONFIG_PATH: dfile = definitions.manifest(self.services, self.topics, self.actions, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') if not suffix: if not self.topics.has_key(path): for action_suffix in [Action.STATUS_SUFFIX,Action.RESULT_SUFFIX,Action.FEEDBACK_SUFFIX]: action_name = path[:-(len(action_suffix)+1)] if path.endswith('/' + action_suffix) and self.actions.has_key(action_name): action = self.actions[action_name] msg = action.get(action_suffix) break else: return response_404(start_response) else: topic = self.topics[path] if not topic.allow_sub: return response_405(start_response) msg = topic.get() if use_ros: content_type = ROS_MSG_MIMETYPE output_data = StringIO() if msg is not None: msg.serialize(output_data) output_data = output_data.getvalue() else: content_type = 'application/json' output_data = msgconv.extract_values(msg) if msg is not None else None output_data = json.dumps(output_data) return response_200(start_response, output_data, content_type=content_type) path = path[:-(len(suffix)+1)] if suffix == MSG_PATH and self.topics.has_key(path): return response_200(start_response, definitions.get_topic_msg(self.topics[path]), content_type='text/plain') elif suffix == SRV_PATH and self.services.has_key(path): return response_200(start_response, definitions.get_service_srv(self.services[path]), content_type='text/plain') elif suffix == ACTION_PATH and self.actions.has_key(path): return response_200(start_response, definitions.get_action_action(self.actions[path]), content_type='text/plain') elif suffix == CONFIG_PATH: if self.services.has_key(path): service_name = path service = self.services[service_name] dfile = definitions.describe_service(service_name, service, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') elif self.topics.has_key(path): topic_name = path topic = self.topics[topic_name] dfile = definitions.describe_topic(topic_name, topic, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') elif self.actions.has_key(path): action_name = path action = self.actions[action_name] dfile = definitions.describe_action(action_name, action, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') else: for suffix in [Action.STATUS_SUFFIX,Action.RESULT_SUFFIX,Action.FEEDBACK_SUFFIX,Action.GOAL_SUFFIX,Action.CANCEL_SUFFIX]: if path.endswith('/' + suffix): path = path[:-(len(suffix)+1)] if self.actions.has_key(path): action_name = path action = self.actions[action_name] dfile = definitions.describe_action_topic(action_name, suffix, action, full=full) if jsn: return response_200(start_response, str(dfile.tojson()), content_type='application/json') else: return response_200(start_response, dfile.tostring(suppress_formats=True), content_type='text/plain') return response_404(start_response) else: return response_404(start_response)