def _FormatResultAsJson(self, result, format_mode=None): if result is None: return dict(status="OK") if format_mode == JsonMode.PROTO3_JSON_MODE: return json.loads( json_format.MessageToJson(result.AsPrimitiveProto())) elif format_mode == JsonMode.GRR_ROOT_TYPES_STRIPPED_JSON_MODE: result_dict = {} for field, value in result.ListSetFields(): if isinstance( field, (rdf_structs.ProtoDynamicEmbedded, rdf_structs.ProtoEmbedded, rdf_structs.ProtoList)): result_dict[field.name] = api_value_renderers.RenderValue( value) else: result_dict[field.name] = api_value_renderers.RenderValue( value)["value"] return result_dict elif format_mode == JsonMode.GRR_TYPE_STRIPPED_JSON_MODE: rendered_data = api_value_renderers.RenderValue(result) return api_value_renderers.StripTypeInfo(rendered_data) elif format_mode == JsonMode.GRR_JSON_MODE: return api_value_renderers.RenderValue(result) else: raise ValueError("Invalid format_mode: %s" % format_mode)
def _PrepareV1Request(self, method, args=None): """Prepares API v1 request for a given method and args.""" args_proto = None if args: args_proto = args.AsPrimitiveProto() request = self.connector.BuildRequest(method, args_proto) request.url = request.url.replace("/api/v2/", "/api/") if args and request.data: body_proto = args.__class__().AsPrimitiveProto() json_format.Parse(request.data, body_proto) body_args = args.__class__() body_args.ParseFromString(body_proto.SerializeToString()) request.data = json.dumps( api_value_renderers.StripTypeInfo( api_value_renderers.RenderValue(body_args)), cls=http_api.JSONEncoderWithRDFPrimitivesSupport) prepped_request = request.prepare() return request, prepped_request