mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

Support of HAL format #661

Open konstlepa opened 10 years ago

konstlepa commented 10 years ago

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:

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.

Toub commented 10 years ago

+1 for supporting HAL format!

mgonto commented 10 years ago

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!

konstlepa commented 10 years ago

Hi. I wrote own implementation of HAL client. It could be helpful for you.

https://gist.github.com/KonstantinLepa/b33b5efc190c64bc4710

Thanks

GonzaloAlvarez commented 10 years ago

+1 for HAL support.

subvertnormality commented 10 years ago

+1

azizur commented 10 years ago

+1 for supporting HAL format!

cboden commented 10 years ago

+1

razbomi commented 10 years ago

+1

luaks commented 10 years ago

+1 would definitely need it

mgonto commented 10 years ago

Hey,

Can any of you give me an example of a HAL response that is currently not supported in Restangular?

Thanks!

azizur commented 10 years ago

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"
    } ]
  }
}
fabioginzel commented 10 years ago

+1

raymondBourges commented 10 years ago

:+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

javifr commented 10 years ago

+1

RemoteCTO commented 10 years ago

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
  )
xenoterracide commented 10 years ago

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
  }
}
dinoboff commented 10 years ago

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;
}
xenoterracide commented 10 years ago

@dinoboff assuming the list got larger than the page size, how would I iterate the list?

dinoboff commented 10 years ago

@xenoterracide recall the getList method again with the page number as parameter.

grabbou commented 9 years ago

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.

javifr commented 9 years ago

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/

grabbou commented 9 years ago

@mgonto I think we can link that in our FAQ section.

adammartin1981 commented 9 years ago

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?

stevermeister commented 8 years ago

+1 to implementation of HAL for Restangular

EdwardDiehl commented 8 years ago

+1 for adding HAL support

geoHeil commented 8 years ago

+1

krash84 commented 8 years ago

+1

PatrickHuetter commented 8 years ago

+1

yusufumac commented 8 years ago

+1

daviesgeek commented 8 years ago

What is the status of this issue?

mappedinn commented 8 years ago

+1

sldurkovic commented 7 years ago

+1

GDuchemin commented 7 years ago

+1