https://github.com/usausa/rester
HttpClient extensions for rest api and upload/download.
https://github.com/usausa/rester
rest rest-client
Last synced: 5 days ago
JSON representation
HttpClient extensions for rest api and upload/download.
- Host: GitHub
- URL: https://github.com/usausa/rester
- Owner: usausa
- License: mit
- Created: 2019-04-18T12:27:59.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-15T05:56:07.000Z (3 months ago)
- Last Synced: 2025-04-27T16:04:21.015Z (22 days ago)
- Topics: rest, rest-client
- Language: C#
- Homepage:
- Size: 764 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rester - HttpClient extensions for rest api
## What is this?
* HttpClient extensions with serialization function
* Download/Upload helper api with progress### Usage example
```csharp
// Config
RestConfig.Default.UseJsonSerializer();// Prepare HttpClient
var client = new HttpClient();// Get
var response = await client.GetAsync("api/test/get/123");
// Check result and get TestGetResponse
var result = response.IsSuccess();
var content = response.Content;// Post with compress
var response = await client.PostAsync("api/test/post", new TestPostRequest { Data = "..." }, compress: true);// Download with progress
var response = await client.DownloadAsync(
"api/test/download/test.dat",
"test.dat",
progress: (processed, total) =>
{
...
});// Multiple file upload with other parameter and progress
var response = await client.MultipartUploadAsync(
"api/test/upload",
new List
{
new MultipartUploadEntry(stream1, "file1", "test.txt"),
new MultipartUploadEntry(stream2, "file2", "test.csv").WithGzip()
},
new Dictionary
{
{ "Code", 123 },
{ "Tag", "abc" }
},
progress: (processed, total) =>
{
...
});
```## NuGet
| Id | Description |
|------------------------------------|---------------------|
| Rester | Rester core |## Functions
### Config
Rester config.
```csharp
public sealed class RestConfig
{
// Serializer for object
public ISerializer Serializer { get; set; }// Content-Encoding
public ContentEncoding ContentEncoding { get; set; }// Download/Upload buffer size
public int TransferBufferSize { get; set; }// Download content length missing handler
public Func LengthResolver { get; set; }
}
```Serializer config for Json.NET.
```csharp
public static RestConfig UseJsonSerializer(this RestConfig config);public static RestConfig UseJsonSerializer(this RestConfig config, Action action);
```Json.NET config default.
```csharp
public sealed class JsonSerializerConfig
{
public string ContentType { get; set; } = "application/json";public JsonSerializerSettings Settings { get; } = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
DateFormatHandling = DateFormatHandling.IsoDateFormat
};
}
```### Get
```csharp
public static Task> GetAsync(
this HttpClient client,
string path,
IDictionary headers = null,
CancellationToken cancel = default);public static async Task> GetAsync(
this HttpClient client,
RestConfig config,
string path,
IDictionary headers = null,
CancellationToken cancel = default);
```### Post
```csharp
public static Task PostAsync(
this HttpClient client,
string path,
object parameter,
IDictionary headers = null,
bool compress = false,
CancellationToken cancel = default);public static async Task PostAsync(
this HttpClient client,
RestConfig config,
string path,
object parameter,
IDictionary headers = null,
bool compress = false,
CancellationToken cancel = default);public static Task> PostAsync(
this HttpClient client,
string path,
object parameter,
IDictionary headers = null,
bool compress = false,
CancellationToken cancel = default);public static async Task> PostAsync(
this HttpClient client,
RestConfig config,
string path,
object parameter,
IDictionary headers = null,
bool compress = false,
CancellationToken cancel = default);
```### Download
```csharp
public static Task DownloadAsync(
this HttpClient client,
string path,
string filename,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static async Task DownloadAsync(
this HttpClient client,
RestConfig config,
string path,
string filename,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static Task DownloadAsync(
this HttpClient client,
string path,
Stream stream,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static async Task DownloadAsync(
this HttpClient client,
RestConfig config,
string path,
Stream stream,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);
```### Upload
```csharp
public static Task MultipartUploadAsync(
this HttpClient client,
string path,
Stream stream,
string name,
string filename,
Func, Task> filter = null,
IDictionary parameters = null,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static Task MultipartUploadAsync(
this HttpClient client,
RestConfig config,
string path,
Stream stream,
string name,
string filename,
Func, Task> filter = null,
IDictionary parameters = null,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static Task MultipartUploadAsync(
this HttpClient client,
string path,
string name,
string filename,
Func, Task> filter = null,
IDictionary parameters = null,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static async Task MultipartUploadAsync(
this HttpClient client,
RestConfig config,
string path,
string name,
string filename,
Func, Task> filter = null,
IDictionary parameters = null,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static Task MultipartUploadAsync(
this HttpClient client,
string path,
IList entries,
IDictionary parameters = null,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);public static async Task MultipartUploadAsync(
this HttpClient client,
RestConfig config,
string path,
IList entries,
IDictionary parameters = null,
IDictionary headers = null,
Action progress = null,
CancellationToken cancel = default);
```### Custom serializer
```csharp
public interface ISerializer
{
string ContentType { get; }string Serialize(object obj);
T Deserialize(string json);
}
```