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

Function check_profanity

ch_08/src/profanity.rs:30–63  ·  view source on GitHub ↗
(
    content: String,
)

Source from the content-addressed store, hash-verified

28}
29
30pub async fn check_profanity(
31 content: String,
32) -> Result<String, handle_errors::Error> {
33 let retry_policy =
34 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("https://api.apilayer.com/bad_words?censor_character=*")
43 .header("apikey", "API_KEY")
44 .body(content)
45 .send()
46 .await
47 .map_err(|e| handle_errors::Error::MiddlewareReqwestAPIError(e))?;
48
49 if !res.status().is_success() {
50 if res.status().is_client_error() {
51 let err = transform_error(res).await;
52 return Err(handle_errors::Error::ClientError(err));
53 } else {
54 let err = transform_error(res).await;
55 return Err(handle_errors::Error::ServerError(err));
56 }
57 }
58
59 match res.json::<BadWordsResponse>().await {
60 Ok(res) => Ok(res.censored_content),
61 Err(e) => Err(handle_errors::Error::ReqwestAPIError(e)),
62 }
63}
64
65async fn transform_error(
66 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