Created
August 21, 2023 20:28
-
-
Save faul-sname/cd724db7773f552a3d729baa90c81ab5 to your computer and use it in GitHub Desktop.
What code looks like with explicit error handling at every step
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fetch the OpenAPI yaml file | |
fn fetch_openapi() -> Result<String, reqwest::Error> { | |
let url = "https://example.com/openapi.yaml"; | |
match reqwest::blocking::get(url) { | |
Ok(response) => match response.text() { | |
Ok(text) => Ok(text), | |
Err(e) => Err(e), | |
}, | |
Err(e) => Err(e), | |
} | |
} | |
// Parse the YAML and get the response format for the GET /posts endpoint | |
fn get_response_format(yaml: &str) -> Result<Value, serde_yaml::Error> { | |
match serde_yaml::from_str(yaml) { | |
Ok(openapi) => { | |
let response_format = openapi | |
.get("paths") | |
.and_then(|p| p.get("/posts")) | |
.and_then(|p| p.get("get")) | |
.and_then(|g| g.get("responses")) | |
.and_then(|r| r.get("200")) | |
.and_then(|ok| ok.get("schema")) | |
.ok_or(serde_yaml::Error::custom("Missing response format"))?; | |
Ok(response_format.clone()) | |
}, | |
Err(e) => Err(e), | |
} | |
} | |
// Fetch API key from .env file | |
fn get_api_key() -> Result<String, env::VarError> { | |
dotenv().ok(); | |
env::var("API_KEY") | |
} | |
// Make a GET request against the endpoint | |
fn fetch_posts(api_key: &str) -> Result<String, reqwest::Error> { | |
let url = "https://example.com/posts"; | |
let client = reqwest::blocking::Client::new(); | |
match client.get(url).header("Authorization", format!("Bearer {}", api_key)).send() { | |
Ok(response) => match response.text() { | |
Ok(text) => Ok(text), | |
Err(e) => Err(e), | |
}, | |
Err(e) => Err(e), | |
} | |
} | |
// Validate the response against the OpenAPI specification | |
fn validate_response(response: &str, schema: &Value) -> Result<(), String> { | |
match serde_json::from_str::<Value>(response) { | |
Ok(_) => Ok(()), | |
Err(e) => Err(format!("Response does not match schema: {}", e)), | |
} | |
} | |
// Tie it all together | |
fn main() { | |
match fetch_openapi() { | |
Ok(yaml) => match get_response_format(&yaml) { | |
Ok(response_format) => match get_api_key() { | |
Ok(api_key) => match fetch_posts(&api_key) { | |
Ok(response) => match validate_response(&response, &response_format) { | |
Ok(_) => println!("Response is valid according to the OpenAPI specification."), | |
Err(e) => println!("Validation failed: {}", e), | |
}, | |
Err(e) => println!("Failed to fetch posts: {}", e), | |
}, | |
Err(e) => println!("Failed to get API key: {}", e), | |
}, | |
Err(e) => println!("Failed to get response format: {}", e), | |
}, | |
Err(e) => println!("Failed to fetch OpenAPI yaml: {}", e), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment