Skip to content

Commit

Permalink
chore: remove a few unwrap calls
Browse files Browse the repository at this point in the history
  • Loading branch information
imor committed Oct 3, 2023
1 parent ce16842 commit 130c7a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
23 changes: 13 additions & 10 deletions wrappers/src/fdw/airtable_fdw/airtable_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ use std::collections::HashMap;
use url::Url;

use supabase_wrappers::prelude::*;
use thiserror::Error;

use super::result::AirtableResponse;
use super::{AirtableFdwError, AirtableFdwResult};

fn create_client(api_key: &str) -> ClientWithMiddleware {
fn create_client(api_key: &str) -> Result<ClientWithMiddleware, AirtableFdwError> {
let mut headers = header::HeaderMap::new();
let value = format!("Bearer {}", api_key);
let mut auth_value = header::HeaderValue::from_str(&value).unwrap();
let mut auth_value =
header::HeaderValue::from_str(&value).map_err(|_| AirtableFdwError::InvalidApiKeyHeader)?;
auth_value.set_sensitive(true);
headers.insert(header::AUTHORIZATION, auth_value);
let client = reqwest::Client::builder()
.default_headers(headers)
.build()
.unwrap();
.build()?;
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
ClientBuilder::new(client)
Ok(ClientBuilder::new(client)
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build()
.build())
}

#[wrappers_fdw(
Expand Down Expand Up @@ -78,7 +77,7 @@ impl AirtableFdw {
resp_body: &str,
columns: &[Column],
) -> AirtableFdwResult<(Vec<Row>, Option<String>)> {
let response: AirtableResponse = serde_json::from_str(resp_body).unwrap();
let response: AirtableResponse = serde_json::from_str(resp_body)?;
let mut result = Vec::new();

for record in response.records.iter() {
Expand All @@ -98,10 +97,14 @@ impl ForeignDataWrapper<AirtableFdwError> for AirtableFdw {
.unwrap_or_else(|| "https://api.airtable.com/v0".to_string());

let client = match options.get("api_key") {
Some(api_key) => Some(create_client(api_key)),
Some(api_key) => Some(create_client(api_key)?),
None => {
let key_id = require_option("api_key_id", options)?;
get_vault_secret(&key_id).map(|api_key| create_client(&api_key))
if let Some(api_key) = get_vault_secret(&key_id) {
Some(create_client(&api_key)?)
} else {
None
}
}
};

Expand Down
11 changes: 10 additions & 1 deletion wrappers/src/fdw/airtable_fdw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ enum AirtableFdwError {
#[error("parse url failed: {0}")]
UrlParseError(#[from] url::ParseError),

#[error("invalid api_key header")]
InvalidApiKeyHeader,

#[error("request failed: {0}")]
RequestError(#[from] reqwest_middleware::Error),
RequestError(#[from] reqwest::Error),

#[error("request middleware failed: {0}")]
RequestMiddlewareError(#[from] reqwest_middleware::Error),

#[error("invalid json response: {0}")]
SerdeError(#[from] serde_json::Error),

#[error("{0}")]
OptionsError(#[from] OptionsError),
Expand Down

0 comments on commit 130c7a7

Please sign in to comment.