Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/Winson-Ng/EasyHttpClient

C# HttpClient Library
https://github.com/Winson-Ng/EasyHttpClient

Last synced: 5 days ago
JSON representation

C# HttpClient Library

Awesome Lists containing this project

README

        

# .Net(C#) EasyHttpClient

### What's EasyHttpClient?

EasyHttpClient provide you an easy way to access HTTP resource(e.g. REST API client, html, stream, upload and down).

## Usage
### 3 steps to go

#### Step 1, create your http client interface:

...cs

[RoutePrefix("api/value")]
public interface TestApiClient
{
[Route("{key}")]
[Authorize]
[HttpGet]
object GetValue(string key, string def="test");

[Route("{key}")]
[HttpPut]
void SetValue([PathParam][JsonBody]string key, [JsonBody]dynamic value);
}

...

#### Step 2, config the HttpClientWrapperFactory:

...cs

var factory = new HttpClientWrapperFactory()
{
DefaultHost = new Uri(host)
};

...

#### Step 3, use in my code:

...cs

var testApiClient = factory.CreateFor();
var val = testApiClient.GetValue("myname");

...

### Config OAuth2:
...cs

factory.HttpClientSettings.OAuth2ClientHandler = new MyOAuth2ClientHandler();

...

### Extend HttpClientProvider, to add my handlers on System.Net.Http.HttpClient class:

...cs

factory.HttpClientProvider = new HttpClientProvider();

...

### Extend EasyHttpClient.ActionFilters.ActionFilterAttribute class to enhance your function, for example logging, cache, validation:

...cs

[RoutePrefix("api/value")]
public interface TestApiClient
{
[Route("{key}")]
[Authorize]
[HttpGet]
[ApiClientCache]
object GetValue(string key, string def="test");

[Route("{key}")]
[HttpPut]
void SetValue([PathParam][JsonBody]string key, [JsonBody]dynamic value);
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ApiClientCacheAttribute : ActionFilterAttribute
{
public static ICacheClient CacheClient { get; set; }

public override Task ActionInvoke(ActionContext context, Func> Continuation)
{
//TODO: handle cache
return Continuation();
}
}

...