| 25 | }; |
| 26 | |
| 27 | pub fn configured_value(database: &Database) -> Self { |
| 28 | let (endpoint, region) = if let Some(Value::Object(o)) = database.settings.get(Setting::ApiCodeWhispererService) |
| 29 | { |
| 30 | // The following branch is evaluated in case the user has set their own endpoint. |
| 31 | ( |
| 32 | o.get("endpoint").and_then(|v| v.as_str()).map(|v| v.to_owned()), |
| 33 | o.get("region").and_then(|v| v.as_str()).map(|v| v.to_owned()), |
| 34 | ) |
| 35 | } else if let Ok(Some(profile)) = database.get_auth_profile() { |
| 36 | // The following branch is evaluated in the case of user profile being set. |
| 37 | let region = profile.arn.split(':').nth(3).unwrap_or_default().to_owned(); |
| 38 | match Self::CODEWHISPERER_ENDPOINTS |
| 39 | .iter() |
| 40 | .find(|e| e.region().as_ref() == region) |
| 41 | { |
| 42 | Some(endpoint) => (Some(endpoint.url().to_owned()), Some(region)), |
| 43 | None => { |
| 44 | error!("Failed to find endpoint for region: {region}"); |
| 45 | (None, None) |
| 46 | }, |
| 47 | } |
| 48 | } else { |
| 49 | (None, None) |
| 50 | }; |
| 51 | |
| 52 | match (endpoint, region) { |
| 53 | (Some(endpoint), Some(region)) => Self { |
| 54 | url: endpoint.clone().into(), |
| 55 | region: Region::new(region.clone()), |
| 56 | }, |
| 57 | _ => Endpoint::DEFAULT_ENDPOINT, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub(crate) fn url(&self) -> &str { |
| 62 | &self.url |