summaryrefslogtreecommitdiffstats
path: root/src/repository/client.rs
blob: 4466e05f49c587a2d8423d81312cd0db99c66578 (plain)
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
use std::io::Cursor;
use std::sync::Arc;
use std::ops::Deref;
use std::result::Result as RResult;

use ipfs_api::IpfsClient;
use ipfs_api::TryFromUri;
use anyhow::Error;
use futures::future::Future;
use futures::future::FutureExt;
use futures::stream::Stream;
use futures::stream::StreamExt;
use futures::stream::TryStreamExt;
use failure::Fail;

use serde_json::from_str as serde_json_from_str;
use serde_json::to_string as serde_json_to_str;
use serde::Serialize;
use serde::de::DeserializeOwned;
use chrono::NaiveDateTime;

use crate::types::block::Block;
use crate::types::content::Content;
use crate::types::payload::Payload;
use crate::types::util::IPFSHash;
use crate::types::util::IPNSHash;


/// Internal ClientFassade types
///
/// Abstracts the procedural interface of IpfsClient calls.
#[derive(Clone)]
pub struct ClientFassade(Arc<IpfsClient>);

impl std::fmt::Debug for ClientFassade {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> RResult<(), std::fmt::Error> {
        write!(f, "ClientFassade")
    }
}

impl ClientFassade {
    fn new(host: &str, port: u16) -> Result<ClientFassade, Error> {
        debug!("Creating new ClientFassade object: {}:{}", host, port);
        IpfsClient::from_str(&format!("{}:{}", host, port))
            .map(Arc::new)
            .map(|c| ClientFassade(c))
            .map_err(|e| Error::from(e.compat()))
    }

    pub async fn get_raw_bytes<H: AsRef<IPFSHash>>(&self, hash: H) -> Result<Vec<u8>, Error> {
        debug!("Get: {}", hash.as_ref());
        self.0
            .clone()
            .cat(hash.as_ref())
            .map_ok(|b| b.to_vec())
            .try_concat()
            .map(|r| r.map_err(|e| anyhow!("UNIMPLEMENTED!()")))
            .await
    }

    pub async fn put_raw_bytes(&self, data: Vec<u8>) -> Result<IPFSHash, Error> {
        debug!("Put: {:?}", data);
        self.0
            .clone()
            .add(Cursor::new(data))
            .await
            .map(|res| IPFSHash::from(res.hash))
            .map_err(|e| anyhow!("UNIMPLEMENTED!()"))
    }

    pub async fn publish(&self, key: &str, hash: &str) -> Result<IPNSHash, Error> {
        debug!("Publish: {:?} -> {:?}", key, hash);
        self.0
            .clone()
            .name_publish(hash, false, None, None, Some(key))
            .await
            .map(|res| IPNSHash::from(res.value))
            .map_err(|e| anyhow!("UNIMPLEMENTED!()"))
    }

    pub async fn resolve(&self, ipns: IPNSHash) -> Result<IPFSHash, Error> {
        self.0
            .clone()
            .name_resolve(Some(&ipns), true, false)
            .await
            .map(|res| IPFSHash::from(res.path))
            .map_err(|e| anyhow!("UNIMPLEMENTED!()"))
    }
}

/// Client wrapper for working with types directly on the client
#[derive(Debug, Clone)]
pub struct TypedClientFassade(ClientFassade);

impl Deref for TypedClientFassade {
    type Target = ClientFassade;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl TypedClientFassade {
    pub fn new(host: &str, port: u16) -> Result<TypedClientFassade, Error> {
        ClientFassade::new(host, port).map(TypedClientFassade)
    }

    pub async fn get_typed<H, D>(&self, hash: H) -> Result<D, Error>
        where H: AsRef<IPFSHash>,
              D: DeserializeOwned
    {
        self.0
            .clone()
            .get_raw_bytes(hash)
            .await
            .and_then(|data| {
                debug!("Got data, building object: {:?}", data);

                serde_json::from_slice(&data).map_err(|e| Error::from(e.compat()))
            })
    }

    pub async fn put_typed<S, Ser>(&self, data: &S) -> Result<IPFSHash, Error>
        where S: AsRef<Ser>,
              Ser: Serialize
    {
        let client = self.0.clone();

        let data = serde_json_to_str(data.as_ref())?;
        client.put_raw_bytes(data.into_bytes()).await
    }

}