1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
extern crate reqwest;

use serde_json;

/// Implements an author
#[derive(Debug, Clone)]
pub struct Author {
    pub author_id: Option<String>,
    pub name: Option<String>,
    pub url: Option<String>,
}

impl Author {
    pub fn new_from_json(j: &serde_json::Value) -> Result<Author, Box<::std::error::Error>> {
        if !j.is_object() {
            return Err(From::from(format!(
                "JSON for Author::new_from_json is not an object: {}",
                &j
            )));
        }
        Ok(Author {
            author_id: j["authorId"].as_str().map_or(None, |s| Some(s.to_string())),
            name: j["name"].as_str().map_or(None, |s| Some(s.to_string())),
            url: j["url"].as_str().map_or(None, |s| Some(s.to_string())),
        })
    }
}

/// Implements a topic
#[derive(Debug, Clone)]
pub struct Topic {
    pub topic: Option<String>,
    pub topic_id: Option<String>,
    pub url: Option<String>,
}

impl Topic {
    pub fn new_from_json(j: &serde_json::Value) -> Result<Topic, Box<::std::error::Error>> {
        if !j.is_object() {
            return Err(From::from(format!(
                "JSON for Topic::new_from_json is not an object: {}",
                &j
            )));
        }
        Ok(Topic {
            topic_id: j["topicId"].as_str().map_or(None, |s| Some(s.to_string())),
            topic: j["topic"].as_str().map_or(None, |s| Some(s.to_string())),
            url: j["url"].as_str().map_or(None, |s| Some(s.to_string())),
        })
    }
}

/// Implements a work (=paper)
#[derive(Debug, Clone)]
pub struct Work {
    pub arxiv_id: Option<String>,
    pub authors: Vec<Author>,
    pub citation_velocity: Option<u64>,
    pub citations: Vec<Work>,
    pub doi: Option<String>,
    pub influential_citation_count: Option<u64>,
    pub paper_id: Option<String>,
    pub references: Vec<Work>,
    pub title: Option<String>,
    pub topics: Vec<Topic>,
    pub url: Option<String>,
    pub venue: Option<String>,
    pub year: Option<u64>,
}

impl Work {
    pub fn new_from_json(j: &serde_json::Value) -> Result<Work, Box<::std::error::Error>> {
        if !j.is_object() {
            return Err(From::from(format!(
                "JSON for Work::new_from_json is not an object: {}",
                &j
            )));
        }
        let mut ret = Work {
            arxiv_id: j["arxivId"].as_str().map_or(None, |s| Some(s.to_string())),
            authors: vec![],
            citation_velocity: j["citationVelocity"].as_u64(),
            citations: vec![],
            doi: j["doi"].as_str().map_or(None, |s| Some(s.to_string())),
            influential_citation_count: j["influentialCitationCount"].as_u64(),
            paper_id: j["paperId"].as_str().map_or(None, |s| Some(s.to_string())),
            references: vec![],
            title: j["title"].as_str().map_or(None, |s| Some(s.to_string())),
            topics: vec![],
            url: j["url"].as_str().map_or(None, |s| Some(s.to_string())),
            venue: j["venue"].as_str().map_or(None, |s| Some(s.to_string())),
            year: j["year"].as_u64(),
        };
        for author in j["authors"].as_array().unwrap_or(&vec![]) {
            ret.authors.push(Author::new_from_json(author)?);
        }
        for topic in j["topics"].as_array().unwrap_or(&vec![]) {
            ret.topics.push(Topic::new_from_json(topic)?);
        }
        if let Some(citations) = j["citations"].as_array() {
            for paper in citations {
                if let Ok(paper) = Work::new_from_json(paper) {
                    ret.citations.push(paper);
                }
            }
        }
        if let Some(references) = j["references"].as_array() {
            for paper in references {
                ret.references.push(Work::new_from_json(paper)?);
            }
        }
        Ok(ret)
    }
}

#[derive(Debug, Clone)]
pub struct Client {}

impl Client {
    pub fn new() -> Client {
        Client {}
    }

    pub fn work(&self, id: &str) -> Result<Work, Box<::std::error::Error>> {
        let api_url = String::from("http://api.semanticscholar.org/v1");
        let url = api_url + "/paper/" + id;
        let json: serde_json::Value = reqwest::get(url.as_str())?.json()?;
        match json["error"].as_str() {
            Some(error_string) => Err(From::from(format!("{}:{}", error_string, id))),
            None => Work::new_from_json(&json),
        }
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn test1() {
        let doi = "10.1016/j.bpj.2008.12.3951";
        let client = super::Client::new();
        let work = client.work(&doi).unwrap();
        assert_eq!(work.authors.len(), 2);
        assert_eq!(work.doi, Some(doi.into()));
    }
}