| // Copyright 2025 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| //! This module defines structures for handling composite requests and responses in an API system. |
| //! It includes types for parsing incoming composite requests, generating responses, and |
| //! managing individual sub-requests and sub-responses within a composite operation. |
| |
| // For using Json name directly |
| #![allow(non_snake_case)] |
| |
| use serde_json::Value; |
| |
| /// Represents a composite request containing multiple sub-requests. |
| #[derive(serde::Deserialize)] |
| pub struct CompositeRequest { |
| /// A vector of sub-requests to be processed as part of this composite request. |
| #[allow(dead_code)] |
| pub compositeRequest: Vec<SubRequest>, |
| } |
| |
| /// Represents an individual sub-request within a composite request. |
| #[derive(Debug, Default, Clone, PartialEq, serde::Deserialize)] |
| pub struct SubRequest { |
| /// The HTTP method for this sub-request (e.g., "GET", "POST"). |
| pub method: String, |
| /// The URL for this sub-request. |
| pub url: String, |
| /// A unique identifier for this sub-request within the composite request. |
| pub referenceId: String, |
| /// If true, the entire composite request should be aborted if this sub-request fails. |
| pub abortOnFailure: Option<bool>, |
| /// The body of the sub-request, if any. |
| pub body: Option<Value>, |
| } |
| |
| /// Represents the response to a composite request. |
| #[derive(serde::Serialize)] |
| pub struct CompositeResponse { |
| /// A vector of sub-responses corresponding to each sub-request in the original composite request. |
| pub compositeResponse: Vec<SubResponse>, |
| } |
| |
| /// Represents the possible types of responses that can be returned from a request. |
| #[derive(Debug)] |
| pub enum RequestResponse { |
| /// A JSON response. |
| Json(Value), |
| } |
| |
| impl Default for RequestResponse { |
| /// Provides a default error response. |
| fn default() -> Self { |
| RequestResponse::Json(serde_json::json!({ |
| "status": "error", |
| "message": "Invalid response", |
| "code": 500 |
| })) |
| } |
| } |
| |
| /// Represents the response to an individual sub-request within a composite request. |
| #[derive(Default, serde::Serialize)] |
| pub struct SubResponse { |
| /// The body of the sub-response. |
| pub body: Option<Value>, |
| /// Any HTTP headers included in the sub-response. |
| pub httpHeaders: Option<Value>, |
| /// The HTTP status code for this sub-response. |
| pub httpStatusCode: u16, |
| /// The reference ID of the corresponding sub-request. |
| pub referenceId: String, |
| } |