Git Product home page Git Product logo

nuget-webapiclient's Introduction

Sinch WebApiClient

This package supports

API definition with interface
Auto generating implementation using Castle.Core
Attribute routing
Asynchronous implementation
Action Filters

License

Sinch WebApiClient is © 2015 Sinch AB. It is free software, and may be redistributed under the terms of the MIT license.

API definition with interface

Map methods, reguest and response using an interface.

public interface IEchoApi
{
	[HttpGet("echo/{name}")]
	Task<string> Echo(string name);
}

Auto generating implementation using Castle.Core

An implementation is generated for the defined interface using Castle.Core dynamic proxy.

var factory = new WebApiClientFactory();
var echoApi = factory.CreateClient<IEchoApi>("http://127.0.0.1:4711");
var echoResult =  await echoApi.Echo("Hello world!");
Console.WriteLine(echoResult);

Attribute routing

Routing is performed similar to WebApi Controllers.

public interface IAttributeRoutingApi
{
	[HttpGet("products")]
	Task<ProductList> GetAll();

	[HttpGet("products/{id}")]
	Task<Product> GetById(string id);
        
	[HttpPost("products/{id}")]
	Task UpdateById(string id, Product product);

	[HttpDelete("products/{id}")]
	Task DeleteById(string id);
}

Also support [ToUri] and [ToBody] attribute routing.

public class Name
{
	public string First { get; set; }
	public string Last { get; set; }
}
	
public interface IUserApi
{
	[HttpGet("users/{last}/{first}")]
	public Task<UserList> QueryByName([ToUri] Name name);
}

Action Filters

Action filters are hooks into pre/post processing. The request can be modified in OnActionExecuting and the response can be accessed before it is processed in OnActionExecuted

public class ConsoleLoggerActionFilter : IActionFilter
{
	public Task OnActionExecuting(HttpRequestMessage requestMessage)
	{
		Console.WriteLine($"[{requestMessage.Method}] {requestMethod.RequestUri}");
		return Task.FromResult(true);
	}
		
	public Task OnActionExecuted(HttpResponseMessage responseMessage)
	{
		Console.WriteLine($"[{responseMessage.StatusCode} {responseMessage.ReasonPhrase}]");
		return Task.FromResult(true);
	}
}
	
var factory = new WebApiClientFactory();
var echoApi = factory.CreateClient<IEchoApi>("http://127.0.0.1:4711", new ConsoleLoggerActionFilter());
var echoResult =  await echoApi.Echo("Hello world!");
Console.WriteLine(echoResult);

And that's all, folks. If you have any questions, email us at [email protected].

nuget-webapiclient's People

Contributors

brunoblank avatar danjohnso avatar spacedsweden avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.