use super::BoxClient;
use super::Result;
use reqwest;
use reqwest::StatusCode;
use serde_json;
use Error;
use Kind;
pub struct Textbox {
url: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Analysis {
pub sentences: Vec<Sentence>,
pub keywords: Vec<Keyword>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Sentence {
pub text: String,
pub start: u32,
pub end: u32,
pub sentiment: f64,
pub entities: Vec<Entity>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Entity {
#[serde(rename = "type")]
pub entity_type: String,
pub text: String,
pub start: u32,
pub end: u32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Keyword {
pub keyword: String,
}
impl Textbox {
pub fn new(url: &str) -> Textbox {
Textbox {
url: url.to_owned(),
}
}
pub fn check(&self, text: &str) -> Result<Analysis> {
let url = format!("{}/textbox/check", self.url());
let params = [("text", text)];
let client = reqwest::Client::new();
match client.post(&url).form(¶ms).send() {
Ok(mut response) => {
let raw = response.text()?;
if response.status() != StatusCode::Ok {
Err(Error {
kind: Kind::Machinebox(format!("HTTP {}: {}", response.status(), raw)),
})
} else {
let analysis: Analysis = serde_json::from_str(&raw)?;
Ok(analysis)
}
}
Err(e) => Err(Error {
kind: Kind::Reqwest(e),
}),
}
}
}
impl BoxClient for Textbox {
fn url(&self) -> &str {
&self.url
}
}