The latest version of http_client module has a nice feature:
http://drupalcode.org/project/http_client.git/blob/HEAD:/includes/HttpCl...

The constructor supports an optional request_alter parameter, which can be an object with a public alterRequest method. The object may be the WSClientRESTEndpoint itself, so we could add a new public function to it e.g.

  public function alterRequest($request) {
    drupal_alter('wsclient_rest_request', $request, $this->service);
  }

… and modify lines 41 and 44 accordingly:

41 $this->client = new HttpClient(NULL, $this->service->settings['formatter'], $this);
44 $this->client = new HttpClient(NULL, new HttpClientBaseFormatter(HttpClientBaseFormatter::FORMAT_JSON), $this);

I'm using this modification in a pre-production environment, the hook I've implemented in my module looks like this:

function myhook_wsclient_rest_request_alter($request, $service) {
  if ($service->name == 'my_service') {
    // remove empty parameters
    $request->parameters = array_filter($request->parameters, 'strlen');
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tito.brasolin’s picture

klausi’s picture

Status: Active » Needs work

Nice idea!

+++ b/wsclient_rest/wsclient_rest.module
@@ -110,6 +110,9 @@ class WSClientRESTEndpoint extends WSClientEndpoint {
   }
 
+  public function alterRequest($request) {
+    drupal_alter('wsclient_rest_request', $request, $this->service);
+  }

Doc block is missing, see http://drupal.org/node/1354#functions

And this new hook should be documented in wsclient_rest.api.php.

tito.brasolin’s picture

Thank you klausi, I have generated with Drush Issue Queue Commands a new (and hopefully better) patch.

tito.brasolin’s picture

Please look at: https://drupal.org/node/2044587
... maybe my patch can solve PatchRanger's issue too?

PatchRanger’s picture

Status: Needs work » Needs review

Looks like it needs review again.
I confirm that patch works in my case (see forementioned #2044587: Empty remaining parameters inside request URI).

klausi’s picture

Status: Needs review » Needs work

Patch from #3 does not apply, see https://drupal.org/node/707484

tito.brasolin’s picture

Status: Needs work » Needs review
FileSize
2.6 KB

Thank you klausi, here is a new patch.

PatchRanger’s picture

I am using the patch from #7 at production and confirm that it works perfectly.

PatchRanger’s picture

Status: Needs review » Closed (duplicate)

This patch was merged into the most recent version of the patch from #1280332: Advanced REST service formatters + UI setting due to the overlapping conflicts. See https://drupal.org/node/1280332#comment-7786939 .

tcmug’s picture

Status: Closed (duplicate) » Needs review
FileSize
457 bytes

Reopening for a new approach.

The above patch only focuses on the rest client call altering -- I came about and simply added a drupal_alter to the WSClientServiceDescription->invoke method. This way also SOAP and other types of service calls can be altered.

Patch attached.

klausi’s picture

That recent patch also looks nice and would provide a hook for SOAP services, too (or any other kind of service for that matter).

@tito.brasolin and @PatchRanger: would that work for you too?

PatchRanger’s picture

@klausi I guess it should though I don't have an opportunity to test it with my current projects. Anyway it looks nice.

klausi’s picture

Status: Needs review » Needs work

The most recent patch misses the API docs.

adrien.felipe’s picture

This now looks integrated in code, even if slightly differently than the patch did initially.

    // In wsclient.inc: line 87
    drupal_alter('wsclient_invoke_arguments', $named_arguments, $operation, $this);

    $response = $this->endpoint()->call($operation, $named_arguments);

    drupal_alter('wsclient_invoke_response', $response, $operation, $this);

The $service (or request) alteration is hence available in the third parameter of the drupal_alter call ($this)

But this has two main issues:
- In case of the wsclient_soap module, SoapClient::__setCookie can't be executed this way and hence cookies can't be added, nor the client object really be altered.
- The fourth argument of drupal_alter should not be used:

$context3: This parameter is deprecated and will not exist in Drupal 8; consequently, it should not be used for new Drupal 7 code either.

Hooks where added to the REST api, but SOAP has none yet.
#2560453: Add support to alter SoapClient object has a patch that adds a hook to alter the SOAP request too.