2个版本
| 0.5.2 | 2022年11月13日 | 
|---|---|
| 0.5.1 | 2022年11月13日 | 
#1748 in Web编程
每月24次下载
在 izihawa-ipfs-api 中使用
210KB
 4K  SLoC
ipfs-api
组件
| 名称 | 文档 | Crate | 
|---|---|---|
| ipfs-api-prelude | ||
| ipfs-api-backend-actix | ||
| ipfs-api-backend-hyper | ||
| ipfs-api (已弃用) | 
使用Hyper/Actix连接到IPFS HTTP API的Rust库。
使用方法
使用Hyper
要使用Hyper后端,声明
[dependencies]
ipfs-api-backend-hyper = "0.5"
您可以指定 with-hyper-rustls 或 with-hyper-tls(互斥)功能以支持TLS。
使用Actix
要使用Actix后端,声明
[dependencies]
ipfs-api-backend-actix = "0.6"
构建器模式
使用Hyper或Actix后端,您可以通过指定 with-builder 功能来启用构建器模式,以便在构建请求时使用。
使用方法(已弃用)
[dependencies]
ipfs-api = "0.16.0"
功能标志(已弃用)
您可以使用 actix-web 作为后端,而不是 hyper。
[dependencies]
ipfs-api = { version = "0.16.0", features = ["with-actix"], default-features = false }
您还可以选择使用 rustls 而不是本地TLS
[dependencies]
ipfs-api = { version = "0.16.0", features = ["with-hyper-rustls"], default-features = false }
要启用构建器模式(默认)请使用 with-builder 功能
[dependencies]
ipfs-api = { version = "0.16.0", features = ["with-hyper-rustls", "with-builder"], default-features = false }
示例
将文件写入IPFS
使用Hyper
use ipfs_api::{IpfsApi, IpfsClient};
use std::io::Cursor;
#[tokio::main]
async fn main() {
    let client = IpfsClient::default();
    let data = Cursor::new("Hello World!");
    match client.add(data).await {
        Ok(res) => println!("{}", res.hash),
        Err(e) => eprintln!("error adding file: {}", e)
    }
}
使用Actix
use ipfs_api::{IpfsApi, IpfsClient};
use std::io::Cursor;
#[actix_rt::main]
async fn main() {
    let client = IpfsClient::default();
    let data = Cursor::new("Hello World!");
    match client.add(data).await {
        Ok(res) => println!("{}", res.hash),
        Err(e) => eprintln!("error adding file: {}", e)
    }
}
从IPFS读取文件
使用Hyper
use futures::TryStreamExt;
use ipfs_api_backend_hyper::{IpfsApi, IpfsClient};
use std::io::{self, Write};
#[tokio::main]
async fn main() {
    let client = IpfsClient::default();
    match client
        .get("/test/file.json")
        .map_ok(|chunk| chunk.to_vec())
        .try_concat()
        .await
    {
        Ok(res) => {
            let out = io::stdout();
            let mut out = out.lock();
            out.write_all(&res).unwrap();
        }
        Err(e) => eprintln!("error getting file: {}", e)
    }
}
使用Actix
use futures::TryStreamExt;
use ipfs_api::{IpfsApi, IpfsClient};
use std::io::{self, Write};
#[actix_rt::main]
async fn main() {
    let client = IpfsClient::default();
    match client
        .get("/test/file.json")
        .map_ok(|chunk| chunk.to_vec())
        .try_concat()
        .await
    {
        Ok(res) => {
            let out = io::stdout();
            let mut out = out.lock();
            out.write_all(&res).unwrap();
        }
        Err(e) => eprintln!("error getting file: {}", e)
    }
}
其他示例
项目中也包含了一些示例,我使用了它们进行测试
要获取示例列表,请运行
$ cargo run --example
您可以使用 cargo 运行任何示例
$ cargo run --example add_file
许可证
根据您选择以下其中一种许可
- Apache许可证版本2.0(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证(LICENSE-MIT 或 https://open-source.org.cn/licenses/MIT)
。
贡献
除非您明确说明,否则根据Apache-2.0许可证定义的,您有意提交以包含在该作品中的任何贡献,应按上述方式双重许可,不附加任何额外条款或条件。
依赖项
~11–26MB
~421K SLoC