summaryrefslogtreecommitdiffstats
path: root/src/repository/repository.rs
blob: cc940c627f9fd0a7147a0bad2c8a0231d8fce369 (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
use std::io::Cursor;
use std::sync::Arc;
use std::ops::Deref;

use ipfs_api::IpfsClient;
use anyhow::Error;
use futures::future::Future;
use futures::stream::Stream;

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;
use crate::repository::client::TypedClientFassade;


/// High-level Client abstraction
///
/// Still a low-level interface though, because we're still operating on the repository directly.
///
/// Should not be used too extensively, but rather through the "Profile" type, which represents the
/// profile of a user.
#[derive(Debug, Clone)]
pub struct Repository(TypedClientFassade);

impl Deref for Repository {
    type Target = TypedClientFassade;

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

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

    pub async fn get_block<H>(&self, hash: H) -> Result<Block, Error>
        where H: AsRef<IPFSHash>
    {
        self.0.get_typed(hash).await
    }

    pub async fn put_block<B>(&self, b: B) -> Result<IPFSHash, Error>
        where B: AsRef<Block>
    {
        self.0.put_typed(b.as_ref()).await
    }

    pub async fn get_content<H>(&self, hash: H) -> Result<Content, Error>
        where H: AsRef<IPFSHash>
    {
        self.0.get_typed(hash).await
    }

    pub async fn put_content<C>(&self, c: C) -> Result<IPFSHash, Error>
        where C: AsRef<Content>
    {
        self.0.put_typed(c.as_ref()).await
    }

}