Create a new RESTApi from options. # Arguments `options` - The configuration options containing URI, warehouse, etc. `config_required` - Whether to fetch config from server and merge with options. # Returns A new RESTApi instance. # Errors Returns an error if required options are missing or if config fetch fails.
(options: Options, config_required: bool)
| 101 | /// # Errors |
| 102 | /// Returns an error if required options are missing or if config fetch fails. |
| 103 | pub async fn new(options: Options, config_required: bool) -> Result<Self> { |
| 104 | let uri = options |
| 105 | .get(CatalogOptions::URI) |
| 106 | .ok_or_else(|| crate::Error::ConfigInvalid { |
| 107 | message: "URI cannot be empty".to_string(), |
| 108 | })?; |
| 109 | |
| 110 | if uri.trim().is_empty() { |
| 111 | return Err(crate::Error::ConfigInvalid { |
| 112 | message: "URI cannot be empty".to_string(), |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | let auth_provider = AuthProviderFactory::create_auth_provider(&options)?; |
| 117 | let mut base_headers: HashMap<String, String> = |
| 118 | RESTUtil::extract_prefix_map(&options, Self::HEADER_PREFIX); |
| 119 | // Create auth function first, before making any requests |
| 120 | let rest_auth_function = RESTAuthFunction::new(base_headers.clone(), auth_provider); |
| 121 | |
| 122 | let mut client = HttpClient::new(uri, Some(rest_auth_function))?; |
| 123 | |
| 124 | let options = if config_required { |
| 125 | let warehouse = options.get(CatalogOptions::WAREHOUSE).ok_or_else(|| { |
| 126 | crate::Error::ConfigInvalid { |
| 127 | message: "Warehouse name cannot be empty".to_string(), |
| 128 | } |
| 129 | })?; |
| 130 | |
| 131 | if warehouse.trim().is_empty() { |
| 132 | return Err(crate::Error::ConfigInvalid { |
| 133 | message: "Warehouse name cannot be empty".to_string(), |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | let query_params: Vec<(&str, String)> = vec![( |
| 138 | CatalogOptions::WAREHOUSE, |
| 139 | RESTUtil::encode_string(warehouse), |
| 140 | )]; |
| 141 | let config_response: ConfigResponse = client |
| 142 | .get(&ResourcePaths::config(), Some(&query_params)) |
| 143 | .await?; |
| 144 | |
| 145 | // Merge config response with options (client config takes priority) |
| 146 | let merged = config_response.merge_options(&options); |
| 147 | |
| 148 | // Update base headers from merged options and recreate auth function |
| 149 | base_headers.extend(RESTUtil::extract_prefix_map(&merged, Self::HEADER_PREFIX)); |
| 150 | // Recreate auth function with updated headers if needed |
| 151 | let auth_provider = AuthProviderFactory::create_auth_provider(&merged)?; |
| 152 | let rest_auth_function = RESTAuthFunction::new(base_headers, auth_provider); |
| 153 | |
| 154 | client.set_auth_function(rest_auth_function); |
| 155 | |
| 156 | merged |
| 157 | } else { |
| 158 | options |
| 159 | }; |
| 160 |
nothing calls this directly
no test coverage detected