From e70cb5b9ff7560cf1f72fbc208cb586a5c47c548 Mon Sep 17 00:00:00 2001 From: Adam Shirt Date: Sun, 4 Oct 2020 19:37:16 +0100 Subject: [PATCH 1/2] Update README.md --- README.md | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index da1d74c..9f05a0e 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,109 @@ A client for interacting with a Sia Skynet webportal. ## Usage -View the [wiki](https://github.com/drmathias/csharp-skynet/wiki) for instructions on how to use the package. +### Setup + +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. + +```csharp +using var httpClient = new HttpClient { BaseAddress = new Uri("https://siasky.net") }; +var skynetWebportal = new SkynetWebPortal(httpClient); +``` + +#### With Dependency Injection + +With `Microsoft.Extensions.DependencyInjection`, the client can easily be configured and set up to work with any Skynet webportal. + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddHttpClient(client => + { + client.BaseAddress = new Uri("https://siasky.net"); + }); +} +``` + +### Downloading Files + +Files can be downloaded from a Sia Skynet webportal, by providing a Skylink and optionally a path. + +```csharp +try +{ + var skylink = Skylink.Parse("AABFphGLnADQbFx3tXOQdtjKf0MvFzqZoDIqj_VaebkqcA"); + HttpContent response = await skynetWebportal.DownloadFile(skylink); +} +catch(HttpException e) +{ + // unsuccessful (non-2XX) response +} +``` + +### Uploading Files + +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. + +#### Upload a File + +```csharp +try +{ + var file = new PhysicalFileInfo(new FileInfo("path/to/file.json")); + Skylink response = await skynetWebPortal.UploadFile(file); +} +catch(HttpException e) +{ + // unsuccessful (non-2XX) response +} +catch(HttpResponseException e) +{ + // invalid response from webportal +} +catch(IOException e) +{ + // file access errors +} +``` + +#### Upload a Directory + +```csharp +try +{ + var fileProvider = new PhysicalFileProvider(""); + Skylink response = await skynetWebPortal.UploadDirectory(fileProvider, "directory/to/upload", recurse: true); +} +catch(DirectoryNotFoundException e) +{ + // invalid directory +} +catch(HttpException e) +{ + // unsuccessful (non-2XX) response +} +catch(HttpResponseException e) +{ + // invalid response from webportal +} +catch(IOException e) +{ + // file access errors +} +``` + +#### Configuration + +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`. + +```csharp +new UploadItem(file, "/images/sunset.jpg") +// file will become available at https://siasky.net/{skylink}/images/sunset.jpg +``` + +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`. + +```csharp +new UploadItem(file, null, MediaTypeHeaderValue.Parse("image/gif")) +// when downloaded, the Content-Type header will be set to image/gif +``` From 49791bd521c87dff807a93991691551b6e015f4c Mon Sep 17 00:00:00 2001 From: Adam Shirt Date: Sun, 4 Oct 2020 19:45:38 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f05a0e..eb7efc4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A client for interacting with a Sia Skynet webportal. -![.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/) +![.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) ## Usage @@ -17,7 +17,7 @@ var skynetWebportal = new SkynetWebPortal(httpClient); #### With Dependency Injection -With `Microsoft.Extensions.DependencyInjection`, the client can easily be configured and set up to work with any Skynet webportal. +When using `Microsoft.Extensions.DependencyInjection`, the client can easily be configured and set up to work with any Skynet webportal. ```csharp public void ConfigureServices(IServiceCollection services)