MCPcopy Create free account
hub / github.com/Rust-Web-Development/code / check_profanity

Function check_profanity

ch_10/src/profanity.rs:31–68  ·  view source on GitHub ↗
(
    content: String,
)

Source from the content-addressed store, hash-verified

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

Callers 3

add_answerFunction · 0.50
update_questionFunction · 0.50
add_questionFunction · 0.50

Calls 1

transform_errorFunction · 0.70

Tested by

no test coverage detected