• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jeremy-ellis-tech/net-ipfs-http-client: .NET API client for the InterPlanetary F ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

jeremy-ellis-tech/net-ipfs-http-client

开源软件地址:

https://github.com/jeremy-ellis-tech/net-ipfs-http-client

开源编程语言:

C# 100.0%

开源软件介绍:

net-ipfs-api v0.4.10

The .NET portable class library (PCL) for the InterPlanetary File System (IPFS) API.

This library should be easy to use if you're familiar with the IPFS CLI. Top level commands (eg. ipfs cat, ipfs add) are methods in IpfsClient. Subcommands are methods in the IPFS client's properties named after the subcommands.

ie. ipfs swarm peers becomes ipfs.Swarm.Peers() and ipfs add <file> becomes ipfs.Add(file). Easy!

The only exception is if a valid 'top level' command has subcommands, since C# can't have methods and properties with the same name. ie. ipfs config and ipfs config edit become ipfs.ConfigCommand() and ipfs.Config.Edit().

CLI arguments are required method parameters and CLI options are optional method parameters.

NuGet

This library is available on NuGet.

You can install it with the NuGet package manager:

PM> Install-Package net-ipfs-api

Examples

Adding & reading a file:

using (var ipfs = new IpfsClient())
{
  //Name of the file to add
  string fileName = "test.txt";

  //Wrap our stream in an IpfsStream, so it has a file name.
  IpfsStream inputStream = new IpfsStream(fileName, File.OpenRead(fileName));

  MerkleNode node = await ipfs.Add(inputStream);

  Stream outputStream = await ipfs.Cat(node.Hash);

  using(StreamReader sr = new StreamReader(outputStream))
  {
    string contents = sr.ReadToEnd();

    Console.WriteLine(contents); //Contents of test.txt are printed here!
  }
}

Disconnect from all peers:

using (var ipfs = new IpfsClient())
{
    var peers = await ipfs.Swarm.Peers();
    await ipfs.Swarm.Disconnect(peers);
}

Cancellation support

try
{
    var cts = new CancellationTokenSource(1000);
    
    using (var httpClient = new HttpClient() { Timeout = Timeout.InfiniteTimeSpan })
    using (var ipfsClient = new IpfsClient(new Uri("http://127.0.0.1:5001"), httpClient))
    {
        //The CancellationTokenSource will cancel the task after 1 second
        //Simulate a 2 second delay for this example to ensure the task gets cancelled
        await Task.Delay(2000);
        var result = await ipfsClient.Cat("QmPb8TjUbiRm1M9pvz8QW14PfBjkQyRitnJCZ95oRTRYBt", cts.Token);
    }
}
catch (TaskCanceledException)
{
    Console.WriteLine("The request was canceled");                
}

License

MIT license, see LICENSE for details

Thanks

Thanks to @slothbag & @timothyparez




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ipfs-shipyard/tevere: 发布时间:2022-06-24
下一篇:
Planetable/Planet: Decentralized blogs and websites powered by IPFS and Ethereum ...发布时间:2022-06-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap