Coder Social home page Coder Social logo

Comments (7)

eoin55 avatar eoin55 commented on June 7, 2024

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.

simmotech avatar simmotech commented on June 7, 2024

from honeybear.halclient.

eoin55 avatar eoin55 commented on June 7, 2024

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.

simmotech avatar simmotech commented on June 7, 2024

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.

simmotech avatar simmotech commented on June 7, 2024

from honeybear.halclient.

eoin55 avatar eoin55 commented on June 7, 2024

Hi Simon,

Sure, no problem. Just email me at eoin55 (at) gmail.com

Cheers,
Eoin

from honeybear.halclient.

codingbadger avatar codingbadger commented on June 7, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.