コード例 #1
0
ファイル: FooController.cs プロジェクト: visualeyes/halcyon
        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);
        }
コード例 #2
0
ファイル: FooController.cs プロジェクト: visualeyes/halcyon
        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);
        }
コード例 #3
0
        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()));
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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));
        }
コード例 #11
0
 public void Sets_LinkBase()
 {
     var hal = new HALResponse(new HALModelConfig { LinkBase = "some-link-base" });
     Assert.Equal(hal.Config.LinkBase, "some-link-base");
 }
コード例 #12
0
 public void Sets_ForceHAL()
 {
     var hal = new HALResponse(new HALModelConfig { ForceHAL = true });
     Assert.Equal(hal.Config.ForceHAL, true);
 }
コード例 #13
0
        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);
            }
        }
コード例 #14
0
ファイル: HALResponse.cs プロジェクト: visualeyes/halcyon
 public HALResponse AddEmbeddedResource(string name, HALResponse resource)
 {
     embedded.Add(name, resource);
     return this;
 }
コード例 #15
0
 public static HALResponse AddLinks(this HALResponse halModel, params Link[] links)
 {
     return(halModel.AddLinks(links));
 }
コード例 #16
0
 public HALResponse AddEmbeddedResource(string name, HALResponse resource)
 {
     embedded.Add(name, resource);
     return(this);
 }
コード例 #17
0
 public static bool HasSelfLink(this HALResponse response)
 {
     return(response.HasLink(Link.RelForSelf));
 }