Problem/Motivation

Would it be possible to provide an action plugin so that the power of this module becomes accessible from the Drupal UI or for other modules like e.g. ECA or Views Bulk Operations?

Proposed resolution

Such a plugin would require a configuration form which allows the selection of the http request service from a drop down list and a text field, where the arguments for the request could be configured in e.g. YAML format by also supporting tokens. Something like that would be really powerful and I could provide an MR, if maintainers were interested.

Note: this request was triggered by #3316178: Action to send data to external API

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jurgenhaas created an issue. See original summary.

aronne’s picture

Hi @jurgenhaas,
I just read your feature request and it sounds like a really cool request. If you're already working on that and wanna open a MR please do. I'll be happy to try it out and add it to the module.

Thanks again,
aronne

jurgenhaas’s picture

Thanks @aronne for your quick response. I'm happy to get this started, just wondering if this module provides any services that gives me access to all available services with their commands and parameters. And if I understand correctly, there can also be pre-configured requests? If that's correct, would be nice to get a list of those too.

amal.bukhari’s picture

I'm more of an end user and I'll be happy to help in use cases.

Also very helpful if the client manager can catch error codes and make them accessible as tokens for later use elsewhere (say, in ECA, VBO etc.), such that end users can perform necessary actions after catching error the codes.

aronne’s picture

Hi @jurgenhaas and @amal.bukhari

Services API list

you can use the http_client_manager.http_services_api service to get a list of all the available services API.
eg: \Drupal::service('http_client_manager.http_services_api')->getServicesApi()

You can find an example usage here: https://git.drupalcode.org/project/http_client_manager/-/blob/9.3.x/src/...

Services API Commands list

Also, you can get a list of all the Service API Commands in that way:

$client = \Drupal::service('http_client_manager.factory')->get($serviceApi);
$commands = $client->getCommands();

Another example usage: https://git.drupalcode.org/project/http_client_manager/-/blob/9.3.x/src/...

Error handling

You can either use a try catch:

try {
  return $httpClient->call($command, $args);
}
catch (CommandException $e) {
  // do something here.
}

or use an HTTP Service API Wrapper as explained here https://www.drupal.org/docs/contributed-modules/http-client-manager/http... and override the basic implementation of the logError method:

/**
   * Logs a command exception.
   *
   * This method is meant to be overridden by any Service Api Wrapper.
   * By default it prints the error message by using the Messenger service.
   *
   * @param \GuzzleHttp\Command\Exception\CommandException $e
   *   The Command Exception object.
   */
  protected function logError(CommandException $e) {
    $this->messenger->addError($e->getMessage());
  }

Preconfigured requests

You can create them for every command defined in you http services api. https://www.drupal.org/docs/8/modules/http-client-manager/http-config-re...
You can list them all by using the entity_type.manager service, since they are ConfigEntities.

\Drupal::entityTypeManager()->getStorage('http_config_request')->loadByProperties()

Hope it helps!

jurgenhaas’s picture

Status: Active » Needs review

Please have a look at the initial implementation. Here is what it creates:

  • A configurable action plugin for each command
  • An action for each pre-configured request

What's not yet included is the error handling and storage of the result. The problem with the latter: regular actions do not have access to ECA's token system, that's only available, if we implement this as an ECA integration, but then ECA would become a dependency of this module, what's certainly not desirable. Instead, we could store the result in the expirable key/value store?

aronne’s picture

I took a look at your implementation and I really like it. About the storing of results I agree with you on using the expirable key_value storage or even the StateApi.
You could try and go ahead with the expirable key_value and see how it goes.

jurgenhaas’s picture

I've now implemented the storage into the expirable key value store as well. Seems to be working just fine.

What I've noticed during testing, both for commands and for preconfigured requests, the integer arguments are throwing exceptions in PHP 8.1 because Drupal provides them as strings. I guess, you would have to go through all the command arguments and cast the strings to integer, when they are of that type. This is off-topic here but I thought I'll capture it.

aronne’s picture

Yes I know @jurgenhaas
for now don't care about that bug. I'm gonna work on it in a separate issue tomorrow.

jurgenhaas’s picture

@aronne wondering if you had a chance to move this one forward? Asking because it may well be that we gonna have a client project where this would become very interesting soon.

aronne’s picture

Hi @jurgenhaas
I'm working on this issue #3323767: Add a data integrity layer which should fix the problem you raised.

It should be available soon.

aronne’s picture

Hi @jurgenhaas,
could you try and apply the patch I provided on the following issue? #3323767: Add a data integrity layer
Let me know if it works for you together with your implementation.

  • f3032ad committed on 9.3.x
    Issue #3316197 by jurgenhaas: Provide an action plugin
    
aronne’s picture

Status: Needs review » Fixed

Available @dev version

jurgenhaas’s picture

This is great news, thanks @aronne

I wonder, if you wanted to add "ECA: Event - Condition - Action" as an eco-system into the project page of your module? It would then automatically show up in the list of modules that integrate with ECA.

aronne’s picture

Yes of course! done ;)

jurgenhaas’s picture

Amazing, thank you so much. Already announced your integration in our ECA slack channel.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.