A client for interacting with a Sia Skynet webportal.
This library targets .NET Standard 2.0, so can be used across various platforms. The webportal client is a typed client, that uses System.Net.Http.HttpClient to make HTTP requests.
usingvarhttpClient=newHttpClient{BaseAddress=newUri("https://siasky.net")};varskynetWebPortal=newSkynetWebPortal(httpClient);When using Microsoft.Extensions.DependencyInjection, the client can easily be configured and set up to work with any Skynet webportal.
publicvoidConfigureServices(IServiceCollectionservices){services.AddHttpClient<ISkynetWebPortal,SkynetWebPortal>(client =>{client.BaseAddress=newUri("https://siasky.net");});}Files can be downloaded from a Sia Skynet webportal, by providing a Skylink and optionally a path.
try{varskylink=Skylink.Parse("AABFphGLnADQbFx3tXOQdtjKf0MvFzqZoDIqj_VaebkqcA");HttpContentresponse=awaitskynetWebPortal.DownloadFile(skylink);}catch(HttpExceptione){// unsuccessful (non-2XX) response}This library uses Microsoft.Extensions.FileProviders.Abstractions (see FileProviders) which allows you to upload from many file providers. There are several methods available to simplify uploading single files, multiple files and directories.
try{varfile=newPhysicalFileInfo(newFileInfo("path/to/file.json"));Skylinkresponse=awaitskynetWebPortal.UploadFile(file);}catch(HttpExceptione){// unsuccessful (non-2XX) response}catch(HttpResponseExceptione){// invalid response from webportal}catch(IOExceptione){// file access errors}try{varfileProvider=newPhysicalFileProvider("");Skylinkresponse=awaitskynetWebPortal.UploadDirectory(fileProvider,"directory/to/upload",recurse:true);}catch(DirectoryNotFoundExceptione){// invalid directory}catch(HttpExceptione){// unsuccessful (non-2XX) response}catch(HttpResponseExceptione){// invalid response from webportal}catch(IOExceptione){// file access errors}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 individual UploadItem.
newUploadItem(file,"/images/sunset.jpg");// file will become available at https://siasky.net/{skylink}/images/sunset.jpgFor file uploads, the MIME type is automatically resolved based on the file extension. If you want to override this behaviour, you can explicitly specify the MIME type on an individual UploadItem.
newUploadItem(file,null,MediaTypeHeaderValue.Parse("image/gif"));// when downloaded, the Content-Type header will be set to image/gifUploadOptions and MultiFileUploadOptions can be used to configure how Skynet webportals handle requests for the upload.
varoptions=newMultiFileUploadOptions{FileName="tag:siasky.net,2020-10-10:AABFphGLnADQbFx3tXOQdtjKf0MvFzqZoDIqj_VaebkqcA",DefaultPath="wwwroot/index.html"DryRun=true};Skylinkresponse=awaitskynetWebPortal.UploadDirectory(fileProvider,"directory/to/upload",recurse:true,options);