|
1 | 1 | [](https://www.nuget.org/packages/Shard.Requests) [](https://www.nuget.org/packages/Shard.Requests) [](https://github.com/TypNull/requests/blob/master/LICENSE)  |
2 | | -# Requests |
3 | | -## 🌟 What Is Requests? |
4 | 2 |
|
5 | | -**Requests** is library for C# .NET 6; it's your trusty sidekick in the world of handling requests. Imagine a friendly companion that takes care of your requests, ensuring they're processed efficiently and systematically. Whether you're dealing with HTTP requests or tackling CPU-intensive tasks like directory searching. |
| 3 | +# Requests |
6 | 4 |
|
7 | | -## 🚀 Why Choose Requests? |
| 5 | +Performance async task management with priority queues, automatic retries, and pause/resume capabilities for .NET |
8 | 6 |
|
9 | | -- **Priority Magic**: Our priority channel ensures that high-priority requests get the VIP treatment—they're processed before the rest. No more waiting in line! |
10 | | -- **Flexibility at Its Best**: Requests is designed to be as flexible as possible. Customize it to fit your specific needs, whatever you're building. |
11 | | -- **Parallel Asynchronous Awesomeness**: Handle requests in parallel, like a symphony of asynchronous harmony. 🎶 |
| 7 | +## Why Requests? |
12 | 8 |
|
13 | | -## 📦 Installation |
| 9 | +**Requests** transforms chaotic async workflows into manageable, |
| 10 | +priority-driven operations. Built for developers who need more than |
| 11 | +basic task scheduling—think game engines, batch processors, API |
| 12 | +orchestrators, and complex data pipelines. |
14 | 13 |
|
15 | | -Getting started with **Requests** is a breeze: |
16 | | -1. Open your NuGet Package Manager. |
17 | | -2. Search for "Shard.Requests" |
18 | | -3. Install it. Voilà! 🎉 |
| 14 | +### Key Features |
19 | 15 |
|
20 | | - - [Nuget](https://www.nuget.org/packages/Shard.Requests) |
21 | | - - [GitHub](https://github.com/TypNull/Requests) |
| 16 | +**Priority Scheduling**: High-priority requests jump the queue automatically |
| 17 | +**Smart Retries**: Configurable retry logic with exponential backoff |
| 18 | +**Pause & Resume**: Stop and restart long-running operations without data loss |
| 19 | +**Progress Tracking**: Real-time aggregated progress across multiple operations |
| 20 | +**Dynamic Parallelism**: Auto-adjusts concurrency based on system load |
| 21 | +**Zero Lock-in**: Simple wrapper pattern around your existing async code |
22 | 22 |
|
23 | | -## Usage |
| 23 | +## Quick Start |
24 | 24 |
|
25 | | -To utilize the Requests library in C#, begin by importing it: |
| 25 | +```bash |
| 26 | +dotnet add package Shard.Requests |
| 27 | +``` |
26 | 28 |
|
27 | 29 | ```csharp |
28 | 30 | using Shard.Requests; |
29 | | -``` |
30 | | - |
31 | | -Next, instantiate a `Request` object, and it will automatically be included in the `RequestHandler`. If a request encounters an error, the `RequestHandler` will automatically retry the request based on the specified retry settings. |
32 | | - |
33 | | -## Classes |
34 | | - |
35 | | -This library includes the following classes: |
36 | | - |
37 | | -- **Request:** Main abstract class that can be used to expand functionality on a class-based level. |
38 | | - - All subclasses have a retry function ♾️ |
39 | | - - A priority function 🔝 |
40 | | - - Delegates to notify when a `Request` started, failed, completed, or canceled 📢 |
41 | | - - Implementation for custom `CancellationToken` and a main `CancellationTokenSource` to cancel the request. |
42 | | -- **OwnRequest:** Wrapper around your own request. It is an easy-to-expand class for handling the requests without the creation of a specific class. |
43 | | -- **RequestContainer:** A container class to merge requests together and to start, pause, and await them. |
44 | | -- **ProgressableContainer:** A container class to merge requests together that are using a `Progress` object to report the progress. |
45 | | -- **RequestHandler:** A class to handle requests. Every handler is independent of any other handler. |
46 | | - |
47 | | -> Expand and use as you like! |
48 | | -> |
49 | | -> Because handling requests should be as delightful as a warm cup of cocoa on a winter day. |
50 | | -
|
51 | | - For additional information, refer to the Requests [Wiki](https://github.com/TypNull/Requests/wiki/). |
52 | 31 |
|
53 | | -## Examples |
54 | | - |
55 | | -Meet our star, the `OwnRequest` class: |
56 | | - |
57 | | -```cs |
58 | | -public class OwnRequest : Request<RequestOptions<VoidStruct, VoidStruct>, VoidStruct, VoidStruct> |
| 32 | +// Wrap any async operation |
| 33 | +var request = new OwnRequest(async token => |
59 | 34 | { |
60 | | - private readonly Func<CancellationToken, Task<bool>> _own; |
61 | | - |
62 | | - public OwnRequest(Func<CancellationToken, Task<bool>> own, RequestOptions<VoidStruct, VoidStruct>? requestOptions = null) : base(requestOptions) |
63 | | - { |
64 | | - _own = own; |
65 | | - AutoStart(); |
66 | | - } |
67 | | - |
68 | | - protected override async Task<RequestReturn> RunRequestAsync() |
69 | | - { |
70 | | - return new RequestReturn() { Successful = await _own.Invoke(Token) }; |
71 | | - } |
72 | | -} |
| 35 | + var response = await httpClient.GetAsync(url, token); |
| 36 | + return response.IsSuccessStatusCode; |
| 37 | +}, new() { |
| 38 | + Priority = RequestPriority.High, |
| 39 | + NumberOfAttempts = 3, |
| 40 | + DelayBetweenAttempts = TimeSpan.FromSeconds(2) |
| 41 | +}); |
| 42 | + |
| 43 | +// Request auto-starts with built-in retry and priority handling |
| 44 | +await request.Task; |
73 | 45 | ``` |
74 | 46 |
|
75 | | -OwnRequest is a straightforward implementation of a child class of Request. It doesn’t overwhelm you with complexity, but it’s incredibly useful for quick implementations: |
| 47 | +## Architecture at a Glance |
76 | 48 |
|
77 | | -```cs |
78 | | -// Create an object and pass as a parameter an action that uses a CancellationToken |
79 | | -new OwnRequest(async (token) => |
80 | | -{ |
81 | | - using HttpClient client = new(); |
82 | | - // Create your request message. Here the body of google.com |
83 | | - HttpRequestMessage requestMessage = new(HttpMethod.Get, "https://www.google.com"); |
84 | | - // Send your request and get the result. Pass the CancellationToken for handling it later over the Request object |
85 | | - HttpResponseMessage response = await client.SendAsync(requestMessage, token); |
86 | | - // If the response does not succeed |
87 | | - if (!response.IsSuccessStatusCode) |
88 | | - return false; // Return false to retry and call the failed method |
89 | | - // If the response succeeds. Do what you want and return to finish the request |
90 | | - Console.WriteLine("Finished"); |
91 | | - return true; |
92 | | -}); |
93 | | -``` |
| 49 | +- **Request**: Base class for all operations with state machine and lifecycle hooks |
| 50 | +- **RequestHandler**: Parallel or sequential execution engines with priority channels |
| 51 | +- **RequestContainer**: Group multiple requests with unified control |
| 52 | +- **ProgressableContainer**: Track aggregated progress across request batches |
| 53 | +- **OwnRequest**: Zero-boilerplate wrapper for ad-hoc async operations |
94 | 54 |
|
95 | | -Create your own requests with a sprinkle of magic! ✨ |
| 55 | +## Advanced Capabilities |
96 | 56 |
|
97 | | -## 🌟 Contributing |
| 57 | +- **Cooperative Cancellation**:`await Request.Yield()` for graceful interruption |
| 58 | +- **Subsequent Requests**: Chain operations without re-queuing |
| 59 | +- **Quaternary Heap**: O(log n) priority queue with FIFO ordering within priority levels |
98 | 60 |
|
99 | | -Join our quest! If you'd like to contribute to this library, submit a pull request or open an issue. We appreciate your help in making `Requests` the best it can be! |
| 61 | +## Real-World Use Cases |
100 | 62 |
|
101 | | -## 📜 License |
| 63 | +- **Batch Processing**: Download 1000 files with 10 parallel workers and retry failed transfers |
| 64 | +- **Game Development**: Priority-ordered asset loading with pause when backgrounded |
| 65 | +- **API Rate Limiting**: Throttle concurrent requests with dynamic parallelism control |
| 66 | +- **Long Operations**: Multi-hour processes with save/resume support |
102 | 67 |
|
103 | | -**Requests** is licensed under the MIT license. |
| 68 | +## Documentation |
| 69 | + |
| 70 | +**[Wiki](https://github.com/TypNull/Requests/wiki/)** Architecture deep-dives, examples, and API reference |
104 | 71 |
|
105 | 72 | ## **Free Code** and **Free to Use** |
106 | | -#### Have fun! |
| 73 | +#### Have fun! |
| 74 | + |
| 75 | +*Built for developers who need industrial-strength async control without the complexity* |
0 commit comments