Comments (7)
Hi Simon,
Thanks for getting in touch.
I think the reason it's not working is because visibleLocations isn't a HAL resource. I wouldn't expect HalClient to work in this scenario.
I also ran your JSON through the HAL browser and it didn't seem to respect it either.
Can I suggest that you convert your locations array to a HAL resource and embed the array of location resources. I tried this by modifying your JSON and the following worked for me. (Note: I'm using static JSON files, hence the file extensions.)
{
"_links": {
"self": { "href": "http://localhost/locations.json" }
},
"_embedded": {
"locations": [
{
"locationId": 40000003,
"name": "1000 IFA Firm 40000003 Ltd",
"_links": {
"self": {
"href": "http://localhost/locations-40000003.json"
},
"advisers": {
"href": "http://localhost/locations-40000003-advisers.json"
}
}
},
{
"locationId": 100002441,
"name": "10000 IFA Firm 100002441 Ltd",
"_links": {
"self": {
"href": "http://localhost/locations-100002441.json"
},
"advisers": {
"href": "http://localhost/locations-100002441-advisers.json"
}
}
}
]
}
}
...and the C# code:
var http = new HttpClient {BaseAddress = new Uri("http://localhost/root.json")};
var halClient = new HalClient(http);
var locations =
halClient
.Root()
.Get("visibleLocations")
.Get("locations")
.Items<Location>();
foreach (var location in locations)
{
var adviser =
halClient
.Get(location, "advisers")
.Item<Adviser>()
.Data;
// Do something with this adviser...
}
I hope this helps. Give me a shout if you have any questions.
Cheers,
Eoin
from honeybear.halclient.
from honeybear.halclient.
Hi Simon,
That's a cool solution. I hadn't considered that approach.
Also, I didn't realise that you don't own the API. By exposing locations as a plain array, I don't believe they're strictly adhering to HAL. (However, I could be wrong. This is just my opinion.)
I think what you have is fine; POCOs should work fine for you. Something like this may also work (although you'll probably need to add links to your Location POCO):
var locations =
halClient
.Root()
.Get("visibleLocations")
.Item<AllLocations>()
.Data;
foreach (var location in locations.Locations)
{
var advisers =
halClient
.Root(location.Href)
.Get("advisers")
.Item<Adviser>()
.Data;
}
Let me know if you have any questions.
Cheers,
Eoin
from honeybear.halclient.
Typical! My first go with this HAL stuff and I might be dealing with non-standard data. :-)
The code you mention is vaguely how I expected it to work but all Hrefs I look at appear to be null - presumably because of the JSON array issue you mention.
The JSON I included originally truncated the locations object - it does have this at the end:-
"_links": { "docs": { "href": "https://XXX/nbs/docs/locations" } }
Maybe I can somehow fudge things (I have your source code) so a "self" property gets added to the _links with its HRef set to the current url.
from honeybear.halclient.
from honeybear.halclient.
Hi Simon,
Sure, no problem. Just email me at eoin55 (at) gmail.com
Cheers,
Eoin
from honeybear.halclient.
@simmotech Would you mind contacting me about this - I almost certain that I am in the process of starting to use the very same API and would appreciate any information to help me out. My email is barry [at] codingbadger [dot] com
from honeybear.halclient.
Related Issues (11)
- Should accept application/hal+json as response content type HOT 5
- 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
- Async support HOT 5
- Add support for HAL links retrieval HOT 3
- Fill DTO Object with Elements from the resource and embedded Objects? HOT 4
- Retrieve single objects HOT 2
- Problem getting empty list from _embedded
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.