An open API service indexing awesome lists of open source software.

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.

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);
}
```