MCPcopy Index your code
hub / github.com/Rust-Web-Development/code / check_profanity

Function check_profanity

ch_11/src/profanity.rs:29–67  ·  view source on GitHub ↗
(content: String)

Source from the content-addressed store, hash-verified

27}
28
29pub async fn check_profanity(content: String) -> Result<String, handle_errors::Error> {
30 // We are already checking if the ENV VARIABLE is set inside main.rs, so safe to unwrap here
31 let api_key = env::var("BAD_WORDS_API_KEY").expect("BAD WORDS API KEY NOT SET");
32 let api_layer_url = env::var("API_LAYER_URL").expect("APILAYER URL NOT SET");
33
34 let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
35 let client = ClientBuilder::new(reqwest::Client::new())
36 // Trace HTTP requests. See the tracing crate to make use of these traces.
37 // Retry failed requests.
38 .with(RetryTransientMiddleware::new_with_policy(retry_policy))
39 .build();
40
41 let res = client
42 .post(format!(
43 "{}/bad_words?censor_character=*",
44 api_layer_url
45 ))
46 .header("apikey", api_key)
47 .body(content)
48 .send()
49 .await
50 .map_err(handle_errors::Error::MiddlewareReqwestAPIError)?;
51
52 if !res.status().is_success() {
53 if res.status().is_client_error() {
54 let err = transform_error(res).await;
55 return Err(handle_errors::Error::ClientError(err));
56 } else {
57 let err = transform_error(res).await;
58 return Err(handle_errors::Error::ServerError(err));
59 }
60 }
61
62 match res.json::<BadWordsResponse>()
63 .await {
64 Ok(res) => Ok(res.censored_content),
65 Err(e) => Err(handle_errors::Error::ReqwestAPIError(e)),
66 }
67}
68
69async fn transform_error(res: reqwest::Response) -> handle_errors::APILayerError {
70 handle_errors::APILayerError {

Callers 5

censor_profane_wordsFunction · 0.70
no_profane_wordsFunction · 0.70
add_answerFunction · 0.50
update_questionFunction · 0.50
add_questionFunction · 0.50

Calls 1

transform_errorFunction · 0.70

Tested by

no test coverage detected