Skip to main content

drasi_reaction_http/
config.rs

1// Copyright 2025 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Configuration types for HTTP reactions.
16//!
17//! This module contains configuration types for HTTP reaction and shared types
18//! used by HTTP Adaptive reaction implementations.
19
20use serde::{Deserialize, Serialize};
21use std::collections::HashMap;
22
23fn default_base_url() -> String {
24    "http://localhost".to_string()
25}
26
27fn default_timeout_ms() -> u64 {
28    5000
29}
30
31/// Specification for an HTTP call, including URL, method, headers, and body template.
32///
33/// This type is used to configure HTTP requests for different operation types (added, updated, deleted).
34/// All fields support Handlebars template syntax for dynamic content generation.
35#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
36pub struct CallSpec {
37    /// URL path (appended to base_url) or absolute URL.
38    /// Supports Handlebars templates for dynamic URLs.
39    pub url: String,
40
41    /// HTTP method: GET, POST, PUT, DELETE, or PATCH (case-insensitive).
42    pub method: String,
43
44    /// Request body as a Handlebars template.
45    /// If empty, sends the raw JSON data.
46    #[serde(default)]
47    pub body: String,
48
49    /// Additional HTTP headers as key-value pairs.
50    /// Header values support Handlebars templates.
51    #[serde(default)]
52    pub headers: HashMap<String, String>,
53}
54
55/// Configuration for query-specific HTTP calls.
56///
57/// Defines different HTTP call specifications for each operation type (added, updated, deleted).
58/// Each operation type can have its own URL, method, body template, and headers.
59#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
60pub struct QueryConfig {
61    /// HTTP call specification for ADD operations (new rows in query results).
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub added: Option<CallSpec>,
64
65    /// HTTP call specification for UPDATE operations (modified rows in query results).
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub updated: Option<CallSpec>,
68
69    /// HTTP call specification for DELETE operations (removed rows from query results).
70    #[serde(skip_serializing_if = "Option::is_none")]
71    pub deleted: Option<CallSpec>,
72}
73
74/// HTTP reaction configuration
75#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
76pub struct HttpReactionConfig {
77    /// Base URL for HTTP requests
78    #[serde(default = "default_base_url")]
79    pub base_url: String,
80
81    /// Optional authentication token
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub token: Option<String>,
84
85    /// Request timeout in milliseconds
86    #[serde(default = "default_timeout_ms")]
87    pub timeout_ms: u64,
88
89    /// Query-specific call configurations
90    #[serde(default)]
91    pub routes: HashMap<String, QueryConfig>,
92}
93
94impl Default for HttpReactionConfig {
95    fn default() -> Self {
96        Self {
97            base_url: default_base_url(),
98            token: None,
99            timeout_ms: default_timeout_ms(),
100            routes: HashMap::new(),
101        }
102    }
103}