<commit_before>use IpfsApi;

use serde_json::Value;

extern crate reqwest;
extern crate serde_json;

error_chain! {
    foreign_links {
        Reqwest(reqwest::Error);
        JsonDecode(serde_json::Error);
    }
}

impl IpfsApi {
    /// Resolve an IPNS hash or a domain name
    ///
    /// ```
    /// let api = IpfsApi::new("127.0.0.1", 5001);
    /// let hash = api.name_resolve("gkbrk.com");
    ///
    /// println!("{}", hash);
    /// ```
    pub fn name_resolve(&self, name: &str) -> Result<String> {
        let url = format!("http://{}:{}/api/v0/name/resolve?arg={}", self.server, self.port, name);
        let resp = reqwest::get(&url)?;
        let resp: Value = serde_json::from_reader(resp)?;
        Ok(resp["Path"].to_string())
    }

    /// Publish an IPFS hash in IPNS.
    pub fn name_publish(&self, hash: &str) -> Result<()> {
        let url = format!("http://{}:{}/api/v0/name/publish?arg={}", self.server, self.port, hash);
        let _resp = reqwest::get(&url)?;
        Ok(())
    }
}<commit_msg>Fixed IPNS decoding issue<commit_after>use IpfsApi;

use serde_json::Value;

extern crate reqwest;
extern crate serde_json;

error_chain! {
    foreign_links {
        Reqwest(reqwest::Error);
        JsonDecode(serde_json::Error);
    }
}

impl IpfsApi {
    /// Resolve an IPNS hash or a domain name
    ///
    /// ```
    /// let api = IpfsApi::new("127.0.0.1", 5001);
    /// let hash = api.name_resolve("gkbrk.com");
    ///
    /// println!("{}", hash);
    /// ```
    pub fn name_resolve(&self, name: &str) -> Result<String> {
        let url = format!("http://{}:{}/api/v0/name/resolve?arg={}", self.server, self.port, name);
        let resp = reqwest::get(&url)?;
        let resp: Value = serde_json::from_reader(resp)?;
        
        if resp["Path"].is_string() {
            Ok(resp["Path"].as_str().unwrap().into())
        } else {
            Err("Key error".into())
        }
    }

    /// Publish an IPFS hash in IPNS.
    pub fn name_publish(&self, hash: &str) -> Result<()> {
        let url = format!("http://{}:{}/api/v0/name/publish?arg={}", self.server, self.port, hash);
        let _resp = reqwest::get(&url)?;
        Ok(())
    }
}
{'fraction_non_alphanumeric': 0.20082154267457783, 'fraction_numerical': 0.010953902327704245, 'mean_word_length': 2.361963190184049, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4570716', 'n_tokens_mistral': 771, 'n_tokens_neox': 771, 'n_words': 158}