Struct hcaptcha::HcaptchaCaptcha
source · pub struct HcaptchaCaptcha { /* private fields */ }
Expand description
Capture the Hcaptcha data coming from the client.
Implementations§
source§impl HcaptchaCaptcha
impl HcaptchaCaptcha
sourcepub fn new(response: &str) -> Result<Self, HcaptchaError>
pub fn new(response: &str) -> Result<Self, HcaptchaError>
Create a new HcaptchaCaptcha from a response string slice.
§Input
response - The response token from the client
§Output
The HcaptchaCaptcha is returned if the input is valid. A HcaptchaError is returned if the validation fails.
§Example
Create HcaptchaCaptcha from response key.
use hcaptcha::HcaptchaCaptcha;
// 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 = HcaptchaCaptcha::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, HcaptchaError>
pub fn set_remoteip(&mut self, remoteip: &str) -> Result<Self, HcaptchaError>
Update the remoteip field in HcaptchaCaptcha.
§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 HcaptchaError is returned.
§Example
use hcaptcha::HcaptchaCaptcha;
let remoteip = get_remoteip_address();
let captcha = HcaptchaCaptcha::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, HcaptchaError>
pub fn set_sitekey(&mut self, sitekey: &str) -> Result<Self, HcaptchaError>
Update the remoteip field in HcaptchaCaptcha.
§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 HcaptchaError is returned.
§Example
use hcaptcha::HcaptchaCaptcha;
// 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 = HcaptchaCaptcha::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) -> HcaptchaClientResponse
pub fn response(self) -> HcaptchaClientResponse
Return the value of the response field.
§Output
A string containing the value of the response field.
§Example
use hcaptcha::HcaptchaCaptcha;
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<HcaptchaRemoteip>
pub fn remoteip(&self) -> Option<HcaptchaRemoteip>
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::HcaptchaCaptcha;
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<HcaptchaSitekey>
pub fn sitekey(&self) -> Option<HcaptchaSitekey>
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::HcaptchaCaptcha;
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.
Trait Implementations§
source§impl Clone for HcaptchaCaptcha
impl Clone for HcaptchaCaptcha
source§fn clone(&self) -> HcaptchaCaptcha
fn clone(&self) -> HcaptchaCaptcha
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more