Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve single objects #24

Open
hacki11 opened this issue Aug 17, 2019 · 2 comments
Open

Retrieve single objects #24

hacki11 opened this issue Aug 17, 2019 · 2 comments
Assignees

Comments

@hacki11
Copy link

hacki11 commented Aug 17, 2019

Hi, i found your library and the api looks very nice for consuming hal based rest apis.

I have two questions regarding the api usage:

  1. Retrieve a single resource
    My Resource:
{
  "_embedded" : {
    "users" : [ {
      "id" : 2,
      "name" : "asddf",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/2"
        },
        "user" : {
          "href" : "http://localhost:8080/users/2"
        },
        "address" : {
          "href" : "http://localhost:8080/users/2/address"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/users"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

How do i get the user resource with id 2 without fetching everything and traverse through the result? I am looking for a single http request solution to get /users/2 into my user dto.

  1. How is error handling solved?
    How do i check for all the http error codes and messages e.g.:
    /users/3 => 404 or with detailed json result.
@eoin55 eoin55 self-assigned this Aug 18, 2019
eoin55 added a commit that referenced this issue Aug 19, 2019
Also, fixed a deprecated NuGet parameter (PackageLicenseUrl).
eoin55 added a commit that referenced this issue Aug 19, 2019
… resources.

Now it looks for embedded resources by matching on the self link.
If there are no matches, it follows the link (via a HTTP request).
eoin55 added a commit that referenced this issue Aug 19, 2019
@hacki11
Copy link
Author

hacki11 commented Sep 3, 2019

Hi, i see there is some progress going on.
Do you mind giving some feedback whats planned?

eoin55 added a commit that referenced this issue Sep 4, 2019
eoin55 added a commit that referenced this issue Sep 4, 2019
eoin55 added a commit that referenced this issue Sep 4, 2019
…pears to have stopped producing coverage reports.
@eoin55
Copy link
Owner

eoin55 commented Sep 4, 2019

Hi Jürgen,

I'm very sorry for not responding sooner. I took a look a couple of weeks ago, but I've been crazy busy lately.

Thanks for getting in touch. I hope you're finding this library useful.

I was about to respond to your query, when I noticed a bug in the code. I've added a fix, which is unit-tested, but I haven't gotten a chance to fully integration-test it. The patch version is available on pre-release (2.3.150-alpha). Let me know if you encounter any issues.

To answer your first question; I think you need to add the "users" link, as per the example below. That instructs the HalClient how to retrieve a specific resource. First it will look in _embedded, using the href from _links to resolve the individual resource. Otherwise, it will look follow the href in _links.

{
  "_embedded" : {
    "users" : [ {
      "id" : 2,
      "name" : "asddf",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/2"
        },
        "user" : {
          "href" : "http://localhost:8080/users/2"
        },
        "address" : {
          "href" : "http://localhost:8080/users/2/address"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/users"
    },
    "users" : {
      "href" : "http://localhost:8080/users/{id}",
      "templated" : true
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

Here's an example query:

var user =
    client
        .Root("https://your-domain.com/api/") // navigate to the root of your API
        .Get(rel: "users-query", parameters: new {page = 0, size = 10}) // navigate to the paged users resource 
        .Get(rel: "users", new {id = 2}) // navigate to user with ID=2; HalClient will look first in _embedded, otherwise it will follow the "users" href
        .Item<UserDto>() // deserialise to object of type IResource<UserDto>
        .Data; // return UserDto object

To answer your second question; if the HTTP request is unsuccessful, it will throw an HttpRequestFailed exception. Currently it only contains the error code. Perhaps it should include the HttpResponseMessage object. What do you think?

If you're happy with this fix, could you take a look at the PR I raise, please? #25

Cheers,
Eoin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants