Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/Winson-Ng/EasyHttpClient
- Owner: Winson-Ng
- License: mit
- Created: 2016-06-27T04:59:03.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-08-31T21:27:44.000Z (about 2 years ago)
- Last Synced: 2024-08-02T18:40:04.700Z (3 months ago)
- Language: C#
- Homepage:
- Size: 106 KB
- Stars: 9
- Watchers: 1
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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:
...csfactory.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();
}
}
...