3

Is there any existing Drupal 8 module (on drupal.org, GitHub or anywhere else) that would leverage Guzzle to consume JSON and/or HAL+JSON Web Services over REST?

Alternatively, is the recommended way to necessarily write a custom module leveraging the Guzzle HTTP Client that ships with Drupal core?

My end goal is to be able to create a 1:1 relationship to map fields (node, user...) with key/value pairs from RESTful web services. Then I need to be able to read, update or delete those values.

  • 1
    I think you're missing one of the important points behind Drupal 8 here - if a piece of functionality already exists in a well-established library, it's not going to be reproduced for Drupal 8. "Proudly Invented Elsewhere" is the mantra now, instead of the old "Not Invented Here" mentality. So yeah, use Guzzle :) Or pick another PHP-based REST client (like HTTPFul). But I wouldn't expect that sort of thing to wind up in a module, unless it's just to wrap their functionality in a symfony service to help with DI, but you may as well do that yourself – Clive Aug 30 '14 at 11:15
  • Well yes, I don't want to reinvent the wheel either. Thus why one option I'm thinking about would be to take what's in core (Guzzle), and have a GUI Drupal module built on top of it to be able to map Drupal fields with JSON data that you could pull on demand. – anavarre Aug 30 '14 at 12:04
  • Oh I see - sorry, I glossed over the last paragraph somehow. Sounds like the sort of functionality the core aggregator module gives you, maybe that can be leveraged in some way? A custom fetcher/parser/processor combination might be the way to go – Clive Aug 30 '14 at 12:22
  • Looks like this would have to be custom, because I don't know of any way in Drupal 8 to (yet) leverage the core Aggregator module to do anything else but retrieve XML-based formats. In my wonderland, I would also not wait on cron runs to handle things. I'd have to most likely rely on Rules to trigger events on viewing/saving nodes/user accounts and, if needed, PUT/PATCH the resource object. – anavarre Aug 30 '14 at 12:33
  • Yeah looking at the code a bit more it would probably be more work to extend that than to just write it from scratch. I guess another challenge with that sort of setup is having to maintain identical entity/field config between the two different sites (unless you're consuming the same install from the client of course). If you start writing anything as a public module let me know I'd be happy to contribute – Clive Aug 30 '14 at 12:36
10

OH, just realized now, maybe I misunderstood the question 100%

This is the code which basically generates the output:

$serializer = \Drupal::service('serializer'):
$output = $serializer->serialize($entity, $format);

with $format for example being 'json'.

Luckily the serializer is symetric, so you can do the same the other way round.

$client = \Drupal::service('http_client');
$result = $client->get('http://example.com/entity/node/1', ['Accept' => 'application/json']);
$output = $result->getBody();

$serializer = \Drupal::service('serializer'):
$entity = $serializer->deserialize($output, 'Drupal\node\Entity\Node', $format);
  • Thanks, I think that's a good starting point for what I want to achieve. – anavarre Sep 1 '14 at 7:36
  • What happens when the $output is an array of an unknown content type? – Guillaume Bois Jun 2 '16 at 17:54
2

From the HTTP Client Manager module page:

Http Client Manager introduces a new Guzzle based plugin which allows you to manage HTTP clients using Guzzle Service Descriptions via JSON files, in a simple and efficient way

0

There is a module "RESTful Web Services" - Exposes entities and other resources as RESTful web API. The guzzle is the standard http client used in the Drupal core. "RESTful Web Services" It is modified module "Services" from Drupal 7. It is the service. But the idea is the same. To communicate any service with Drupal as a service.

https://www.youtube.com/watch?v=0mTTfCZ8Iac

  • If you're talking about drupal.org/project/restws I know it's been added to Drupal 8 to expose entities and I'm familiar with how to expose those entites already. My question is rather to determine the best way (ideally via an existing module) to consume those exposed entities on another Drupal 8 site. – anavarre Aug 30 '14 at 10:13
  • I understand you need a Drupal client to Drupal services. Drupal is alpha now. So There are not such modules for Drupal 8. You can study such modules: Migrate(It is the main D8 migration module). You can create a source that communicates with services. You can also look at the module Feeds and the module feeds_import drupal.org/project/feed_import. The module feeds exists for D8. You can help to migrate this module. – viktor.likin Aug 30 '14 at 10:41
  • D8 to D8 migrations won't likely make it before 8.1.x so I don't count on it too much for now. I think "no such module exists for Drupal 8" is a fine answer and what I understand is it'd be preferable to build custom code to achieve the expected behavior. – anavarre Aug 30 '14 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.