pub struct Captcha { /* private fields */ }Expand description
Capture the Hcaptcha data coming from the client.
Implementations§
Source§impl Captcha
impl Captcha
Sourcepub fn new(response: &str) -> Result<Self, Error>
pub fn new(response: &str) -> Result<Self, Error>
Create a new Captcha from a response string slice.
§Input
response - The response token from the client
§Output
The Captcha is returned if the input is valid. A Error is returned if the validation fails.
§Example
Create Captcha from response key.
use hcaptcha_no_wasm::Captcha;
// Get the body JSON string from the event.
let body_str = e.body.unwrap_or_else(|| "".to_owned());
// Get the form data from the body string.
let form: Form = serde_json::from_str(&body_str)?;
let captcha = Captcha::new(&form.response)?;
§Logging
If the tracing feature is enabled a debug level span is set for the method.
Sourcepub fn set_remoteip(&mut self, remoteip: &str) -> Result<Self, Error>
pub fn set_remoteip(&mut self, remoteip: &str) -> Result<Self, Error>
Update the remoteip field in Captcha.
§Input
remoteip - The response token from the client
§Output
If the remoteip string is empty the field is set to None. If the remoteip string is a valid v4 or v6 ip address the field is set to Some(remoteip). If the remoteip string is invalid a Error is returned.
§Example
use hcaptcha_no_wasm::Captcha;
let remoteip = get_remoteip_address();
let captcha = Captcha::new(&form.response)?
.set_remoteip(&remoteip)?;
assert_some!(captcha.remoteip());
if let Some(sk) = captcha.remoteip() {
assert_eq!(remoteip, sk.to_string());
};§Logging
If the tracing feature is enabled a debug level span is set for the method.
Sourcepub fn set_sitekey(&mut self, sitekey: &str) -> Result<Self, Error>
pub fn set_sitekey(&mut self, sitekey: &str) -> Result<Self, Error>
Update the remoteip field in Captcha.
§Input
sitekey - The response token from the client
§Output
If the sitekey string is empty the field is set to None. If the sitekey string is a valid uuid the field is set to Some(sitekey). If the sitekey string is invalid a Error is returned.
§Example
use hcaptcha_no_wasm::Captcha;
// Get the body JSON string from the event.
let body_str = e.body.unwrap_or_else(|| "".to_owned());
// Get the form data from the body string.
let form: Form = serde_json::from_str(&body_str)?;
let sitekey = get_sitekey();
let captcha = Captcha::new(&form.response)?
.set_sitekey(&sitekey)?;
assert_some!(captcha.sitekey());
if let Some(sk) = captcha.sitekey() {
assert_eq!(sitekey, sk.to_string());
};
§Logging
If the tracing feature is enabled a debug level span is set for the method.
Sourcepub fn response(self) -> ClientResponse
pub fn response(self) -> ClientResponse
Return the value of the response field.
§Output
A string containing the value of the response field.
§Example
use hcaptcha_no_wasm::Captcha;
let (response, captcha) = get_captcha();
assert_eq!(response, captcha.response().to_string());
§Logging
If the tracing feature is enabled a debug level span is set for the method.
Sourcepub fn remoteip(&self) -> Option<Remoteip>
pub fn remoteip(&self) -> Option<Remoteip>
Get the value of the remoteip field.
§Output
An Option enum containing the value of the remoteip in the Some variant or a None variant if the value is not set.
§Example
use hcaptcha_no_wasm::Captcha;
let (remoteip, captcha) = get_captcha();
let value = captcha.remoteip();
assert_some!(&value);
if let Some(v) = value {
assert_eq!(remoteip, v.to_string());
}
§Logging
If the tracing feature is enabled a debug level span is set for the method.
Sourcepub fn sitekey(&self) -> Option<Sitekey>
pub fn sitekey(&self) -> Option<Sitekey>
Get the value of the sitekey field.
§Output
An Option enum containing the value of the sitekey in the Some variant or a None variant if the value is not set.
§Example
use hcaptcha_no_wasm::Captcha;
let (sitekey, captcha) = get_captcha();
let value = captcha.sitekey();
assert_some!(&value);
if let Some(v) = value {
assert_eq!(sitekey, v.to_string());
};
§Logging
If the tracing feature is enabled a debug level span is set for the method.