Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a sample code for HAL and Swagger integration
This is a sample code for HAL and Swagger integration
* http://devkimchi.com/2041/creating-service-contract-with-autorest-swagger-and-hal
* http://blog.kloud.com.au/2015/10/26/creating-service-contract-with-autorest-swagger-and-hal
..\..\packages\autorest.0.11.0\tools\AutoRest.exe
-Input swagger.json
-Namespace HalSwaggerSample.WebApp.Proxies
-OutputDirectory ..\Proxies
-CodeGenerator CSharp
{
"_embedded": {
"products": [
{
"productId": 5,
"productName": "My Product 5",
"price": 150.00,
"_links": {
"self": { "href": "/products/5" },
"find": { "href": "/products/{productId}", "templated": true },
"collection": { "href": "/products" }
}
},
{
"productId": 6,
"productName": "My Product 6",
"price": 200.00,
"_links": {
"self": { "href": "/products/5" },
"find": { "href": "/products/{productId}", "templated": true },
"collection": { "href": "/products" }
}
}
]
},
"_links": {
"self": { "href": "/products" }
}
}
{
"products": [
{
"productId": 5,
"productName": "My Product 5",
"price": 150.00
},
{
"productId": 6,
"productName": "My Product 6",
"price": 200.00
}
]
}
GET https://my.api.service/products
{
"productId": 5,
"productName": "My Product",
"price": 150.00,
"_links": {
"self": { "href": "/products/5" },
"find": { "href": "/products/{productId}", "templated": true },
"collection": { "href": "/products" }
}
}
{
"productId": 5,
"productName": "My Product",
"price": 150.00
}
GET https://my.api.service/products/5
var config = GlobalConfiguration.Configuration;
var jsonFormatter = new JsonHalMediaTypeFormatter();
jsonFormatter
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.Insert(0, jsonFormatter);
config.Formatters.Insert(0, new XmlHalMediaTypeFormatter());
[RoutePrefix("products")]
public class ProductController
{
[Route("{productId}")
public virtual async Product GetProduct(int productId)
{
var product = await this._service.GetProductAsync(productId);
product.Rel = "product";
product.Href = "/products/" + productId;
product.Links = new List<Link>()
{
new Link("self", "/products/" + productId),
new Link("find", "/products/{productId}"),
new Link("collection", "/products"),
};
return product;
}
}
[RoutePrefix("products")]
public virtual class ProductController : Controller
{
private readonly IHalSwaggerSampleHalApiApp _proxy;
public ProductController(IHalSwaggerSampleHalApiApp proxy)
{
this._proxy = proxy;
}
[Route("view/{productId}")]
public virtual async Task<ActionResult> ViewProduct(int productId)
{
var response = await this._proxy
.ProductOperations
.GetProductWithHttpMessagesAsync(productId);
var product = response.Body;
return View(product);
}
}
public class Product : Representation
{
public int ProductId { get; set; }
public string ProductName { get; set;}
public decimal Price { get; set; }
}
public class ProductCollection : SimpleListRepresentation<Product>
{
public ProductCollection()
: base ()
{
}
public ProductCollection(IEnumerable<Product> items)
: base (items.ToList())
{
}
}
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set;}
public decimal Price { get; set; }
}
public class ProductCollection
{
public ProductCollection()
{
this.Items = new List<Product>();
}
public ProductCollection(IEnumerable<Product> items)
{
this.Items = items.ToList();
}
public List<Product> Items { get; private set; }
}
//[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace HalSwaggerSample.HalApiApp
{
public static class SwaggerConfig
{
public static void Register(this HttpConfiguration config)
{
...
config.EnableSwagger(c =>
...
}
}
}
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace HalSwaggerSample.HalApiApp
{
public class SwaggerConfig
{
public static void Register()
{
...
GlobalConfiguration.Configuration
.EnableSwagger(c =>
...
}
}
}
config.ConfigSwagger();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment