use crate::{Error, Response, Result};
use serde::Deserialize;
use std::net::IpAddr;
pub fn register_user<S>(ip_address: IpAddr, devicetype: S) -> Result<String>
where
S: AsRef<str>,
{
let url = format!("http://{}/api", ip_address);
let body = format!("{{\"devicetype\":\"{}\"}}", devicetype.as_ref());
let http_response = ureq::post(&url).send_string(&body)?;
#[derive(Deserialize)]
struct User {
username: String,
}
let mut responses: Vec<Response<User>> = http_response.into_json()?;
match responses.pop() {
Some(v) => match v.into_result() {
Ok(user) => Ok(user.username),
Err(e) => Err(Error::Response(e)),
},
None => Err(Error::GetUsername),
}
}
pub fn register_user_with_clientkey<S>(
ip_address: IpAddr,
devicetype: S,
) -> Result<(String, String)>
where
S: AsRef<str>,
{
let url = format!("http://{}/api", ip_address);
let body = format!(
"{{\"devicetype\":\"{}\",\"generateclientkey\":true}}",
devicetype.as_ref()
);
let http_response = ureq::post(&url).send_string(&body)?;
#[derive(Deserialize)]
struct User {
username: String,
clientkey: String,
}
let mut responses: Vec<Response<User>> = http_response.into_json()?;
match responses.pop() {
Some(v) => match v.into_result() {
Ok(user) => Ok((user.username, user.clientkey)),
Err(e) => Err(Error::Response(e)),
},
None => Err(Error::GetUsername),
}
}