HoneyBear.HalClient
HoneyBear.HalClient copied to clipboard
Retrieve single objects
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:
- 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.
- 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.
Hi, i see there is some progress going on. Do you mind giving some feedback whats planned?
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