Last active
October 28, 2015 03:41
-
-
Save justinyoo/833c78c436948493bf87 to your computer and use it in GitHub Desktop.
This is a sample code for HAL and Swagger integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
..\..\packages\autorest.0.11.0\tools\AutoRest.exe | |
-Input swagger.json | |
-Namespace HalSwaggerSample.WebApp.Proxies | |
-OutputDirectory ..\Proxies | |
-CodeGenerator CSharp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_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" } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"products": [ | |
{ | |
"productId": 5, | |
"productName": "My Product 5", | |
"price": 150.00 | |
}, | |
{ | |
"productId": 6, | |
"productName": "My Product 6", | |
"price": 200.00 | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET https://my.api.service/products |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"productId": 5, | |
"productName": "My Product", | |
"price": 150.00, | |
"_links": { | |
"self": { "href": "/products/5" }, | |
"find": { "href": "/products/{productId}", "templated": true }, | |
"collection": { "href": "/products" } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"productId": 5, | |
"productName": "My Product", | |
"price": 150.00 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET https://my.api.service/products/5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = GlobalConfiguration.Configuration; | |
var jsonFormatter = new JsonHalMediaTypeFormatter(); | |
jsonFormatter | |
.SerializerSettings | |
.ContractResolver = new CamelCasePropertyNamesContractResolver(); | |
config.Formatters.Insert(0, jsonFormatter); | |
config.Formatters.Insert(0, new XmlHalMediaTypeFormatter()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] | |
namespace HalSwaggerSample.HalApiApp | |
{ | |
public static class SwaggerConfig | |
{ | |
public static void Register(this HttpConfiguration config) | |
{ | |
... | |
config.EnableSwagger(c => | |
... | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] | |
namespace HalSwaggerSample.HalApiApp | |
{ | |
public class SwaggerConfig | |
{ | |
public static void Register() | |
{ | |
... | |
GlobalConfiguration.Configuration | |
.EnableSwagger(c => | |
... | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.ConfigSwagger(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment