Skip to content
This repository was archived by the owner on May 20, 2022. It is now read-only.

Commit 5fa2ea8

Browse files
committed
2 parents 4257149 + 49791bd commit 5fa2ea8

File tree

1 file changed

+107
-2
lines changed

1 file changed

+107
-2
lines changed

README.md

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,113 @@
22

33
A client for interacting with a Sia Skynet webportal.
44

5-
![.NET Core](https://github.com/drmathias/csharp-skynet/workflows/.NET%20Core/badge.svg?branch=master) [![NuGet](https://img.shields.io/nuget/v/Sia.Skynet)](https://www.nuget.org/packages/Sia.Skynet/)
5+
![.NET Core](https://github.com/drmathias/csharp-skynet/workflows/.NET%20Core/badge.svg?branch=master) [![NuGet](https://img.shields.io/nuget/v/Sia.Skynet)](https://www.nuget.org/packages/Sia.Skynet/) ![MIT License](https://img.shields.io/github/license/drmathias/csharp-skynet)
66

77
## Usage
88

9-
View the [wiki](https://github.com/drmathias/csharp-skynet/wiki) for instructions on how to use the package.
9+
### Setup
10+
11+
This library targets [.NET Standard 2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support), so can be used across various platforms. The webportal client is a [typed client](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests#typed-clients), that uses `System.Net.Http.HttpClient` to make HTTP requests.
12+
13+
```csharp
14+
using var httpClient = new HttpClient { BaseAddress = new Uri("https://siasky.net") };
15+
var skynetWebportal = new SkynetWebPortal(httpClient);
16+
```
17+
18+
#### With Dependency Injection
19+
20+
When using `Microsoft.Extensions.DependencyInjection`, the client can easily be configured and set up to work with any Skynet webportal.
21+
22+
```csharp
23+
public void ConfigureServices(IServiceCollection services)
24+
{
25+
services.AddHttpClient<ISkynetWebPortal, SkynetWebPortal>(client =>
26+
{
27+
client.BaseAddress = new Uri("https://siasky.net");
28+
});
29+
}
30+
```
31+
32+
### Downloading Files
33+
34+
Files can be downloaded from a Sia Skynet webportal, by providing a Skylink and optionally a path.
35+
36+
```csharp
37+
try
38+
{
39+
var skylink = Skylink.Parse("AABFphGLnADQbFx3tXOQdtjKf0MvFzqZoDIqj_VaebkqcA");
40+
HttpContent response = await skynetWebportal.DownloadFile(skylink);
41+
}
42+
catch(HttpException e)
43+
{
44+
// unsuccessful (non-2XX) response
45+
}
46+
```
47+
48+
### Uploading Files
49+
50+
This library uses `Microsoft.Extensions.FileProviders.Abstractions` (see [FileProviders](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/file-providers)) which allows you to upload from many file providers. There are several methods available to simplify uploading single files, multiple files and directories.
51+
52+
#### Upload a File
53+
54+
```csharp
55+
try
56+
{
57+
var file = new PhysicalFileInfo(new FileInfo("path/to/file.json"));
58+
Skylink response = await skynetWebPortal.UploadFile(file);
59+
}
60+
catch(HttpException e)
61+
{
62+
// unsuccessful (non-2XX) response
63+
}
64+
catch(HttpResponseException e)
65+
{
66+
// invalid response from webportal
67+
}
68+
catch(IOException e)
69+
{
70+
// file access errors
71+
}
72+
```
73+
74+
#### Upload a Directory
75+
76+
```csharp
77+
try
78+
{
79+
var fileProvider = new PhysicalFileProvider("");
80+
Skylink response = await skynetWebPortal.UploadDirectory(fileProvider, "directory/to/upload", recurse: true);
81+
}
82+
catch(DirectoryNotFoundException e)
83+
{
84+
// invalid directory
85+
}
86+
catch(HttpException e)
87+
{
88+
// unsuccessful (non-2XX) response
89+
}
90+
catch(HttpResponseException e)
91+
{
92+
// invalid response from webportal
93+
}
94+
catch(IOException e)
95+
{
96+
// file access errors
97+
}
98+
```
99+
100+
#### Configuration
101+
102+
By default, the Skynet path of an uploaded file is set to the file name. This behaviour can be changed, by specifying the Skynet path on an `UploadItem`.
103+
104+
```csharp
105+
new UploadItem(file, "/images/sunset.jpg")
106+
// file will become available at https://siasky.net/{skylink}/images/sunset.jpg
107+
```
108+
109+
For file uploads, the MIME type is automatically resolved based on the file extension. If you want to overwrite this behaviour, you can explicitly specify the MIME type on an `UploadItem`.
110+
111+
```csharp
112+
new UploadItem(file, null, MediaTypeHeaderValue.Parse("image/gif"))
113+
// when downloaded, the Content-Type header will be set to image/gif
114+
```

0 commit comments

Comments
 (0)