Shard.DonwloadLibrary 1.0.5.1

.NET 6.0
dotnet add package Shard.DonwloadLibrary --version 1.0.5.1
NuGet\Install-Package Shard.DonwloadLibrary -Version 1.0.5.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Shard.DonwloadLibrary" Version="1.0.5.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Shard.DonwloadLibrary --version 1.0.5.1
#r "nuget: Shard.DonwloadLibrary, 1.0.5.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Shard.DonwloadLibrary as a Cake Addin
#addin nuget:?package=Shard.DonwloadLibrary&version=1.0.5.1

// Install Shard.DonwloadLibrary as a Cake Tool
#tool nuget:?package=Shard.DonwloadLibrary&version=1.0.5.1

NuGet Downloads License Maintainability

DownloadLibrary

Shard HttpClient Library to handle Requests

The Shard Download Library is an on .Net 6.0 based wrapper around the HttpClient to manage your HTTP requests. But it can also be used without the HttpClient to handle CPU intensive tasks. All Requests will be handled by an PriorityChannel in a Parallel asyncron state to have a simple and efficient way to handle many (simultaneously tested with more than 1000) HTTP Requests.

  • Easy to use! πŸ”“
  • Efficient ♾️
  • ✨ Contains file downloader! ✨

Table of Contents

Features

At the moment:

  • StatusRequest: Calls a Head request and returns a response message with the header information.πŸ”Ž
  • SiteRequest: Is parsing a the HTML body of a website and list all references and files. πŸ”–
  • OwnRequest: Wrapper around your own requests. Easy to self-expand function of handling the requests.
  • RequestContainer: A container class to merge requests together and to start, pause and await them.
  • Request: Main abstract class that can be used to expand functionality on class-based level.
    • All subclasses have a retry function
    • A priority function πŸ”
    • A second thread for bigger files to hold the application responsive 🌐
    • Delegates to notify when a Request started, failed, completed, or canceled
    • Implementation for custom CancellationToken and a main CancellationTokenSource on Downloader to cancel all downloads
  • LoadRequest: To download the response content into files.
    • This is an HTTP file downloader with these functions:
    • Pause and Start a download
    • Resume a download
    • Get the file name and extension from the server πŸ“₯
    • Timeout function βŒ›
    • Monitor the progress of the download with IProgress<float>
    • Can set path and filename
    • Download a specified range of a file πŸ”›
    • Download a file into chunks ⛓️
    • Exclude extensions for safety (.exe; .bat.; etc...) πŸ›‘οΈ

Expand and use as you like!

Tech

It is available on Github:

Installation

Installation over NuGet Package manager in Visual Studio or online. Package Manager Console: PM> NuGet\Install-Package Shard.DonwloadLibrary

How to use

Import the Library.

using DownloaderLibrary.Web.Request;

Then create a new Request object like this LoadRequest. This LoadRequest downloads a file into the download's folder of the PC with a ".part" file and uses the name that the server provides.

//To download a file and store it in "Downloads" folder
new LoadRequest("[Your URL]"); // e.g. https://www.sample-videos.com/video123/mkv/240/big_buck_bunny_240p_30mb.mkv

To set options on the Request create a RequestOption or for a LoadRequest a LoadRequestOption.

// Create an option for a LoadRequest
  LoadRequestOptions requestOptions = new()
        {
            // Sets the filename of the download without the extension
            // The extension will be added automatically!
            FileName = "downloadfile", 
            // If this download has priority (default is false)
            Priority = RequestPriority.High, 
            //(default is download folder)
            DestinationPath = "C:\\Users\\[Your Username]\\Desktop", 
            // If this Request contains a heavy request put it in second thread (default is false)
            IsDownload = true,
            //If the downloader should Override, Create a new file or Append (default is Append)
            //Resume function only available with append!
            Mode = LoadMode.Create, 
            // Progress that writes the % to the Console
            Progress = new Progress<float>(f => Console.WriteLine((f).ToString("0.0%"))),
            //Chunk a file to download faster
            Chunks = 3
        };

And use it in the request.

//To download a file and store it on the Desktop with a different name
new LoadRequest(" https://speed.hetzner.de/100MB.bin",requestOptions);

To wait on the request, use await or Wait();.

await new LoadRequest(" https://speed.hetzner.de/100MB.bin",requestOptions).Task;
//new LoadRequest(" https://speed.hetzner.de/100MB.bin",requestOptions).Wait();

Create an OwnRequest like this:

    //Create an object that passes a CancellationToken
   new OwnRequest((downloadToken) =>
        {
            //Create your request Message. Here the body of google.com
            HttpRequestMessage requestMessage = new(HttpMethod.Get, "https://www.google.com");
            //Send your request and get the result. Pass the CancellationToken for handling it later over the Request object
            var response = await RequestObject.HttpClient.SendAsync(requestMessage, downloadToken);
            //If the response does not succeed
            if (!response.IsSuccessStatusCode)
                return false; // Return false to retry and call the failed method
            //If the response succeed. Do what you want and return to to finish the request
            Console.WriteLine("Finished");
            return true;
        });

To create your own Request child. Here is the implementation of the OwnRequest class:

    public class OwnRequest : : Request<RequestOptions<VoidStruct, VoidStruct>, VoidStruct, VoidStruct>
    {
        private readonly Func<CancellationToken, Task<bool>> _own;
        
        //Parent doesn't require a RequestOption because it creates then a new one.
        //But to use the options it have to be passed over to the parent
        public OwnRequest(Func<CancellationToken, Task<bool>> own, RequestOptions<VoidStruct, VoidStruct>? requestOptions = null) : base(requestOptions)
        {
            _own = own;
            //Has to be called to inject it into the management process
            AutoStart();
        }
        
        // Here will the Request be handled and a object returned that indicates if it succeed
       protected override async Task<RequestReturn> RunRequestAsync() => new() 
       { 
            Successful = await _own.Invoke(Token) 
       };
    }

License

MIT

Free Code and Free to Use

Have fun!
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5.1 75 1/25/2023
1.0.4 222 12/27/2022
1.0.3.2 191 10/24/2022
1.0.3.1 198 10/21/2022
1.0.2.4 203 10/20/2022
1.0.2.3 191 10/17/2022
1.0.2.2 210 10/10/2022
1.0.2 195 9/30/2022

Solved Issue:
HttpClient.SendAsync await throws NullReferenceException