Open konstlepa opened 10 years ago
+1 for supporting HAL format!
Hey,
Using selfLink
and allUrl
and oneUrl
I've tried to support all of the similar HyperMedia formats.
Could you give me exact examples of how your response your formed and what you don't know how you'd support with Restangular?
Thanks!
Hi. I wrote own implementation of HAL client. It could be helpful for you.
https://gist.github.com/KonstantinLepa/b33b5efc190c64bc4710
Thanks
+1 for HAL support.
+1
+1 for supporting HAL format!
+1
+1
+1 would definitely need it
Hey,
Can any of you give me an example of a HAL response that is currently not supported in Restangular?
Thanks!
Here's another made up example:
{
"_links" : {
"self" : {
"href" : "/api/sources"
}
},
"_embedded" : {
"children" : [ {
"_links" : {
"self" : {
"href" : "/api/lists/1"
}
},
"id" : 1,
"label" : "News Homepage",
"query" : "http://www.bbc.co.uk/news/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/2"
}
},
"id" : 2,
"label" : "News World",
"query" : "http://www.bbc.co.uk/news/world/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/3"
}
},
"id" : 3,
"label" : "News UK",
"query" : "http://www.bbc.co.uk/news/uk/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/4"
}
},
"id" : 4,
"label" : "News England",
"query" : "http://www.bbc.co.uk/news/england/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/5"
}
},
"id" : 5,
"label" : "News Nothern Ireland",
"query" : "http://www.bbc.co.uk/news/northern_ireland/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/6"
}
},
"id" : 6,
"label" : "News Scotland",
"query" : "http://www.bbc.co.uk/news/scotland/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/7"
}
},
"id" : 7,
"label" : "News Wales",
"query" : "http://www.bbc.co.uk/news/wales/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/8"
}
},
"id" : 8,
"label" : "News Business",
"query" : "http://www.bbc.co.uk/news/business/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/9"
}
},
"id" : 9,
"label" : "News Technology",
"query" : "http://www.bbc.co.uk/news/technology/",
"src" : "bbc"
}, {
"_links" : {
"self" : {
"href" : "/api/lists/10"
}
},
"id" : 10,
"label" : "News Science and Environment",
"query" : "http://www.bbc.co.uk/news/science_and_environment/",
"src" : "bbc"
} ]
}
}
+1
:+1:
Another example:
git clone git@github.com:spring-projects/spring-boot.git cd spring-boot/spring-boot-samples/spring-boot-sample-data-rest mvn spring-boot:run then http://localhost:8080/hotels
Thanks
+1
HAL doesn't work out of the box, but so far I've managed to get Restangular working with my HAL API, making use of the following config:
NB: The embeddedData.forEach((item)
segment is a (hopefully temporary) hackaround for the following issue: https://github.com/mgonto/restangular/issues/493
BASE_URL = "https://my.awesome-and-wonderful.api"
restAngular = Restangular.withConfig((Configurer) ->
Configurer.setBaseUrl BASE_URL
Configurer.setRestangularFields
selfLink: '_links.self.href'
Configurer.setResponseInterceptor((data, operation, what) ->
if operation == 'getList'
embeddedData = data._embedded[what]
embeddedData.forEach((item) ->
link = item['_links'].self.href
item['_links'].self.href = BASE_URL + link if link[0] == '/'
)
return embeddedData
)
return
)
here's a response that I can't figure out how to support, this is mostly a problem with lists I think, I'm not sure how I'd support in Restangular, including dealing with paging.
LM % curl -i http://localhost:8080/task slave-vi
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/hal+json
Transfer-Encoding: chunked
Date: Wed, 03 Sep 2014 02:23:24 GMT
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/task{?page,size,sort}",
"templated" : true
}
},
"_embedded" : {
"task" : [ {
"description" : "first task",
"_links" : {
"self" : {
"href" : "http://localhost:8080/task/f8eabf43-49be-477b-a119-4d61f2b84bba"
}
}
}, {
"description" : "first task",
"_links" : {
"self" : {
"href" : "http://localhost:8080/task/61ab9de1-efac-4741-96f9-84c1a6cd059a"
}
}
}, {
"description" : "first task",
"_links" : {
"self" : {
"href" : "http://localhost:8080/task/a599502c-dbb6-4bbc-9795-f7dd894a89e3"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 3,
"totalPages" : 1,
"number" : 0
}
}
You could set a response interceptor (addResponseInterceptor
). For getList
operations, it should extract _embedded.task
and return that array.
If you need the keep the page attribute just add it as a property to that array.
function halInterceptor(data, operation, what) {
var resp;
if (operation === 'getList') {
resp = data._embedded[what];
resp.page = data.page;
return resp
}
return data;
}
@dinoboff assuming the list got larger than the page size, how would I iterate the list?
@xenoterracide recall the getList
method again with the page number as parameter.
To be supported in V2 with "plug-in" system. Anyone that has already started work on that, please hold on for a couple of weeks to help us when the next version is more stable.
NICE WORK, waiting for that "plug-in" !
I wrote a little post about consuming hypermedia API's with Restangular in case someone needs to check it out while the "plug-in" is not ready: http://www.javifernandez.me/2014/12/21/consuming-hypermedia-apis-with-restangular/
@mgonto I think we can link that in our FAQ section.
Does anyone know where the native support for HAL is - or is it a case of making custom interceptors and mapping the _links/_embedded accordingly?
+1 to implementation of HAL for Restangular
+1 for adding HAL support
+1
+1
+1
+1
What is the status of this issue?
+1
+1
+1
Hi.
I'm trying to add support of HAL. I read #42 and #44. But I still have many questions. I've made a first attempt to adapt Restangular. Please, see the commit with my comments: https://github.com/KonstantinLepa/restangular/commit/5b71894aa149de807dbafd5afc5e9256e6e181e8. It doesn't work correctly now.
There are my problems:
$scope.user.one('messages', 123).one('from', 123).getList('unread');
are relations. They are not parts of URL. What will happen if from relation doesn't exist in a data? I thinkone('from', 123)
must return an empty restangular object. Or not?Thanks
P.S. Also, HAL supports the CURIE syntax http://tools.ietf.org/html/draft-kelly-json-hal-06#section-8.2. But I didn't think about it.