restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Support of HAL format

Open konstlepa opened this issue 11 years ago • 33 comments

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:

  • Processing of _embedded property. It's optimization only. It can exist or not exist. It must be transparent to the client. How should I implement it?
  • Chaining of methods. For example, messages, from and unread for $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 think one('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.

konstlepa avatar Apr 05 '14 12:04 konstlepa

+1 for supporting HAL format!

Toub avatar Apr 11 '14 09:04 Toub

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!

mgonto avatar Apr 16 '14 08:04 mgonto

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

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

Thanks

konstlepa avatar Apr 17 '14 13:04 konstlepa

+1 for HAL support.

GonzaloAlvarez avatar May 04 '14 17:05 GonzaloAlvarez

+1

subvertnormality avatar May 07 '14 15:05 subvertnormality

+1 for supporting HAL format!

azizur avatar May 07 '14 15:05 azizur

+1

cboden avatar May 13 '14 15:05 cboden

+1

razbomi avatar May 23 '14 12:05 razbomi

+1 would definitely need it

luaks avatar May 28 '14 11:05 luaks

Hey,

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

Thanks!

mgonto avatar May 30 '14 01:05 mgonto

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"
    } ]
  }
}

azizur avatar Jun 04 '14 14:06 azizur

+1

fabioginzel avatar Jun 17 '14 00:06 fabioginzel

:+1:

Another example:

git clone [email protected]: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

raymondBourges avatar Jun 18 '14 12:06 raymondBourges

+1

javifr avatar Jul 22 '14 16:07 javifr

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
  )

RemoteCTO avatar Jul 24 '14 12:07 RemoteCTO

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
  }
}

xenoterracide avatar Sep 10 '14 02:09 xenoterracide

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 avatar Sep 10 '14 12:09 dinoboff

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

xenoterracide avatar Sep 10 '14 12:09 xenoterracide

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

dinoboff avatar Sep 11 '14 17:09 dinoboff

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.

grabbou avatar Feb 24 '15 10:02 grabbou

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/

javifr avatar Feb 24 '15 12:02 javifr

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

grabbou avatar Feb 24 '15 12:02 grabbou

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?

adammartin1981 avatar Sep 03 '15 14:09 adammartin1981

+1 to implementation of HAL for Restangular

stevermeister avatar Feb 12 '16 09:02 stevermeister

+1 for adding HAL support

EdwardDiehl avatar Feb 12 '16 10:02 EdwardDiehl

+1

geoHeil avatar Feb 18 '16 13:02 geoHeil

+1

krash84 avatar Mar 31 '16 08:03 krash84

+1

PatrickHuetter avatar Mar 31 '16 10:03 PatrickHuetter

+1

yusufumac avatar May 13 '16 00:05 yusufumac

What is the status of this issue?

daviesgeek avatar Jun 16 '16 21:06 daviesgeek