public IHttpActionResult GetBar(int fooId) { // A collection of bars related to foo var bars = new List<object> { new { id = 1, fooId = fooId, type = "bar" }, new { id = 2, fooId = fooId, type = "bar" } }; // data about the bars related to foo var fooBarModel = new { fooId = fooId, count = bars.Count }; // Return a fooBar resource with embedded bars var response = new HALResponse(fooBarModel) .AddSelfLink(this.Request) .AddLinks(new Link[] { new Link("other", "/api/foo/{fooId}/bar", replaceParameters: false) }) .AddEmbeddedResource("baz", new { name = "bazza" }, new Link[] { new Link("a", "test") }) .AddEmbeddedCollection("bars", bars, new Link[] { new Link("self", "/api/bar/{id}") }); return this.Ok(response); }
public IActionResult GetBar(int fooId) { // A collection of bars related to foo var bars = new List<object> { new { id = 1, fooId = fooId, type = "bar" }, new { id = 2, fooId = fooId, type = "bar" } }; // data about the bars related to foo var fooBarModel = new { fooId = fooId, count = bars.Count }; // Return a fooBar resource with embedded bars var response = new HALResponse(fooBarModel) .AddLinks(new Link[] { new Link("self", "/api/foo/{fooId}/bar") }) .AddEmbeddedCollection("bars", bars, new Link[] { new Link("self", "/api/bar/{id}") }); return this.Ok(response); }
public void Should_not_be_able_to_convert_HalReponse_with_attribute_marked_model() { var model = new PersonModelWithAttributes(); var halResponse = new HALResponse(model); var converter = new HALAttributeConverter(); Assert.False(converter.CanConvert(halResponse.GetType())); }
public static HALResponse AddEmbeddedCollections(this HALResponse response, IEnumerable <KeyValuePair <string, IEnumerable <HALResponse> > > embeddedCollections) { foreach (var embeddedCollection in embeddedCollections) { response.AddEmbeddedCollection(embeddedCollection.Key, embeddedCollection.Value); } return(response); }
public static HALResponse AddEmbeddedResources <T>(this HALResponse response, IEnumerable <KeyValuePair <string, T> > resources) { foreach (var resource in resources) { response.AddEmbeddedResource(resource.Key, resource.Value); } return(response); }
public void Hal_Model(HttpStatusCode statusCode, bool forceHal, string linkBase) { var halModel = new HALResponse(new HALModelConfig { ForceHAL = forceHal, LinkBase = linkBase }); var result = this.controller.HAL(halModel, statuscode: statusCode) as NegotiatedContentResult<HALResponse>; AssertHalModelResult(statusCode, linkBase, forceHal, result); }
public static HALResponse AddEmbeddedResource <T>(this HALResponse hyperMedia, string resourceName, T model, IEnumerable <Link> links = null) { if (links == null) { links = Enumerable.Empty <Link>(); } var embedded = new HALResponse(model, hyperMedia.Config).AddLinks(links); hyperMedia.AddEmbeddedResource(resourceName, embedded); return(hyperMedia); }
public void To_Action_Result(HttpStatusCode statuscode) { var halModel = new HALResponse(null); var result = halModel.ToActionResult(this.controller, statuscode) as NegotiatedContentResult<HALResponse>; Assert.NotNull(result); Assert.Equal(statuscode, result.StatusCode); var response = result.Content; Assert.NotNull(response); Assert.Same(halModel, response); }
public static HALResponse AddEmbeddedCollection <T>(this HALResponse hyperMedia, string collectionName, IEnumerable <T> model, IEnumerable <Link> links = null) { if (links == null) { links = Enumerable.Empty <Link>(); } var embedded = model .Select(m => new HALResponse(m, hyperMedia.Config).AddLinks(links)) .ToArray(); hyperMedia.AddEmbeddedCollection(collectionName, embedded); return(hyperMedia); }
public IActionResult GetCliente(Guid id) { var clienteFromRepo = _clientesRepository.GetCliente(id); if (clienteFromRepo == null) { return(NotFound()); } var cliente = Mapper.Map <ClienteResponse>(clienteFromRepo); var links = CreateLinksForCliente(id); var response = new Halcyon.HAL.HALResponse(cliente) .AddLinks(links); return(Ok(response)); }
public void Sets_LinkBase() { var hal = new HALResponse(new HALModelConfig { LinkBase = "some-link-base" }); Assert.Equal(hal.Config.LinkBase, "some-link-base"); }
public void Sets_ForceHAL() { var hal = new HALResponse(new HALModelConfig { ForceHAL = true }); Assert.Equal(hal.Config.ForceHAL, true); }
private async Task AssertModelJson(object model, string contentType, bool forceHal, string expected) { using (var stream = new MemoryStream()) { var content = new StringContent(""); content.Headers.ContentType = new MediaTypeHeaderValue(contentType); var formatter = new JsonHALMediaTypeFormatter(); var link = new Link("self", "href"); var embedded = new[] { new { bar = true } }; var halModel = new HALResponse(model, new HALModelConfig { ForceHAL = forceHal }) .AddLinks(link) .AddEmbeddedCollection("bars", embedded); Assert.NotNull(formatter); await formatter.WriteToStreamAsync(typeof(HALResponse), halModel, stream, content, null); // Reset the position to ensure it can read stream.Position = 0; var reader = new StreamReader(stream); string result = await reader.ReadToEndAsync(); Assert.Equal(expected, result); } }
public HALResponse AddEmbeddedResource(string name, HALResponse resource) { embedded.Add(name, resource); return this; }
public static HALResponse AddLinks(this HALResponse halModel, params Link[] links) { return(halModel.AddLinks(links)); }
public HALResponse AddEmbeddedResource(string name, HALResponse resource) { embedded.Add(name, resource); return(this); }
public static bool HasSelfLink(this HALResponse response) { return(response.HasLink(Link.RelForSelf)); }