Comments (4)
Hi @podwildcard,
It is possible to retrieve a resource and any embedded resources. However, I don't think your example JSON adheres to the HAL specification. I believe that the embedded elements should be resources, i.e. they should have links, etc. Please see the snippet below from the HAL example:
"_embedded": {
"ea:order": [{
"_links": {
"self": { "href": "/orders/123" },
"ea:basket": { "href": "/baskets/98712" },
"ea:customer": { "href": "/customers/7809" }
},
"total": 30.00,
"currency": "USD",
"status": "shipped"
}, {
"_links": {
"self": { "href": "/orders/124" },
"ea:basket": { "href": "/baskets/97213" },
"ea:customer": { "href": "/customers/12369" }
},
"total": 20.00,
"currency": "USD",
"status": "processing"
}]
}
If you update your API to return the SubDTOs as embedded resources, then the following HalClient code should work:
var hal = new HalClient(new HttpClient());
IResource<MainDTO> mainResource =
hal
.Root("http://api.com/")
.Get("main-link-relationship", new {id = 123456})
.Item<MainDTO>();
IEnumerable<IResource<SubDTO>> subResources =
hal
.Get(mainResource, "subDTOs")
.Items<SubDTO>();
MainDTO mainDto = mainResource.Data;
IEnumerable<SubDTO> subDtos = subResources.Data();
Although it looks like two separate Get calls, it will only make a single HTTP request (excluding the Root() call).
I hope this helps. Give me a shout if you have any more questions.
Cheers,
Eoin
from honeybear.halclient.
ok, thx for your help 👍
one last question :)
what is the best practice to receive Data from The Root Object, the embedded object and a third nested embedded object in the second embedded object? ^^
for example:
is it possible to receive the "items" Data in the "retail:order" call?
thanks for your help and reading my bad english XD
{
"pageNumber": 0,
"pageSize": 10,
"knownPagesAvailable": 1,
"totalItemsCount": 1,
"_links": {
"curies": [{
"href": "https://api.retail.com/v1/docs/{rel}",
"name": "retail",
"templated": true
}],
"self": {
"href": "/v1/order?pageNumber=0&pageSize=10"
},
"retail:order": {
"href": "/v1/order/{orderRef}",
"templated": true
}
},
"_embedded": {
"retail:order": [{
"orderRef": "e897113c-4c56-404b-8e83-7e7f705046b3",
"orderNumber": "789456",
"status": "AwaitingPayment",
"total": {
"amount": 100.0,
"currency": "USD"
},
"_links": {
"self": {
"href": "/v1/order/e897113c-4c56-404b-8e83-7e7f705046b3"
},
"retail:order-edit": {
"href": "/v1/order/e897113c-4c56-404b-8e83-7e7f705046b3"
},
"retail:order-delete": {
"href": "/v1/order/e897113c-4c56-404b-8e83-7e7f705046b3"
},
"retail:orderitem-queryby-order": {
"href": "/v1/orderitem?pageNumber={pageNumber}&pageSize={pageSize}&orderRef=e897113c-4c56-404b-8e83-7e7f705046b3",
"templated": true
}
},
"_embedded": {
"items": [{
"itemId": 1232,
"_links": {
"self": {
"href": "/v1/order/item/1232"
}
}
},
{
"itemId": 4563,
"_links": {
"self": {
"href": "/v1/order/item/4563"
}
}
}
]
}
}]
}
}
from honeybear.halclient.
Hi @podwildcard,
Yep, it's definitely possible to do that :-)
I took your JSON example and ran the following code against it and it worked fine. Under the hood, it only makes a single HTTP request, as all of your resources are embedded.
Note: The code is quite verbose, for demonstrative purposes. I'm sure you could refactor it to be more succinct.
HttpClient httpClient = new HttpClient {BaseAddress = new Uri("http://localhost/index.json")};
HalClient halClient = new HalClient(httpClient);
IResource<PagedList> rootResource =
halClient
.Root()
.Item<PagedList>();
PagedList rootObj = rootResource.Data; // Deserialise the root object
IEnumerable<IResource<Order>> orderResources =
halClient
.Get(rootResource, "order", "retail")
.Items<Order>();
IEnumerable<Order> orders = orderResources.Data(); // Deserialise the full list of Orders here...
foreach (var orderResource in orderResources)
{
Order order = orderResource.Data; // ...or deseralise each Order here
IEnumerable<OrderItem> items =
halClient
.Get(orderResource, "items") // Note: you didn't include the "retail" curie in your example, so I've excluded it here
.Items<OrderItem>()
.Data(); // Deserialise the list of OrderItems for this Order
}
I think it would be nice if HalClient allowed you to do this whole thing in-line. I'll see if I can extend the library to do this in a nice way.
If you have any other questions, give me a shout.
Thanks,
Eoin
from honeybear.halclient.
Closing due to lack of activity.
from honeybear.halclient.
Related Issues (11)
- Should accept application/hal+json as response content type HOT 5
- How do I access nested Links? HOT 7
- Async support HOT 5
- Add support for HAL links retrieval HOT 3
- Retrieve single objects HOT 2
- Problem getting empty list from _embedded
- Be able to specify which MediaTypeFormatter to use when PUT or POSTing HOT 1
- Support for .NET 4.5 HOT 2
- NUnit 2.6.4 in VS2015 HOT 8
- HAL Forms HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from honeybear.halclient.