|
1 | 1 | //! Helper functions for `aisdk` |
2 | 2 |
|
| 3 | +use reqwest::{IntoUrl, Url}; |
| 4 | + |
3 | 5 | use crate::{ |
4 | | - Error, |
| 6 | + Error, Result, |
5 | 7 | core::{Message, language_model::LanguageModelOptions, messages::TaggedMessage}, |
6 | 8 | }; |
7 | 9 |
|
@@ -118,6 +120,38 @@ pub(crate) fn validate_base_url(s: &str) -> crate::error::Result<String> { |
118 | 120 | Ok(url.to_string()) |
119 | 121 | } |
120 | 122 |
|
| 123 | +/// Joins a base URL with a path, handling trailing/leading slashes automatically. |
| 124 | +/// |
| 125 | +/// This function normalizes the URL components to ensure proper joining: |
| 126 | +/// - Strips trailing slashes from base_url |
| 127 | +/// - Strips leading slashes from path |
| 128 | +/// - Joins them with a single slash |
| 129 | +/// |
| 130 | +/// # Examples |
| 131 | +/// |
| 132 | +/// ```ignore |
| 133 | +/// // All of these produce "https://api.example.com/v1/chat/completions" |
| 134 | +/// join_url("https://api.example.com/v1", "chat/completions"); |
| 135 | +/// join_url("https://api.example.com/v1/", "chat/completions"); |
| 136 | +/// join_url("https://api.example.com/v1", "/chat/completions"); |
| 137 | +/// join_url("https://api.example.com/v1/", "/chat/completions"); |
| 138 | +/// ``` |
| 139 | +pub(crate) fn join_url(base_url: impl IntoUrl, path: &str) -> Result<Url> { |
| 140 | + let base_url = base_url |
| 141 | + .into_url() |
| 142 | + .map_err(|_| Error::InvalidInput("Invalid base URL".into()))?; |
| 143 | + |
| 144 | + // Normalize: strip trailing slashes from base, strip leading slashes from path |
| 145 | + let base_str = base_url.as_str().trim_end_matches('/'); |
| 146 | + let path_str = path.trim_start_matches('/'); |
| 147 | + |
| 148 | + // Join with a single slash |
| 149 | + let full_url = format!("{}/{}", base_str, path_str); |
| 150 | + |
| 151 | + Url::parse(&full_url) |
| 152 | + .map_err(|_| Error::InvalidInput("Failed to join base URL and path".into())) |
| 153 | +} |
| 154 | + |
121 | 155 | #[cfg(test)] |
122 | 156 | mod tests { |
123 | 157 | use super::*; |
@@ -145,4 +179,19 @@ mod tests { |
145 | 179 | fn test_sum_options_both_none() { |
146 | 180 | assert_eq!(sum_options(None, None), None); |
147 | 181 | } |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn test_join_url() { |
| 185 | + let url = join_url("https://api.example.com/v1", "chat/completions").unwrap(); |
| 186 | + assert_eq!(url.as_str(), "https://api.example.com/v1/chat/completions"); |
| 187 | + |
| 188 | + let url = join_url("https://api.example.com/v1/", "chat/completions").unwrap(); |
| 189 | + assert_eq!(url.as_str(), "https://api.example.com/v1/chat/completions"); |
| 190 | + |
| 191 | + let url = join_url("https://api.example.com/v1", "/chat/completions").unwrap(); |
| 192 | + assert_eq!(url.as_str(), "https://api.example.com/v1/chat/completions"); |
| 193 | + |
| 194 | + let url = join_url("https://api.example.com/v1/", "/chat/completions").unwrap(); |
| 195 | + assert_eq!(url.as_str(), "https://api.example.com/v1/chat/completions"); |
| 196 | + } |
148 | 197 | } |
0 commit comments