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');
}
}
Comment | File | Size | Author |
---|---|---|---|
#10 | wsclient-hook_alter_endpoint_calls-1934274-10.patch | 457 bytes | tcmug |
#7 | wsclient-request_alter-1934274-7.patch | 2.6 KB | tito.brasolin |
#3 | wsclient_rest-request_alter_commented-1934274.patch | 3.37 KB | tito.brasolin |
#1 | wsclient_request_alter-1934274.patch | 1.14 KB | tito.brasolin |
Comments
Comment #1
tito.brasolin CreditAttribution: tito.brasolin commentedComment #2
klausiNice idea!
Doc block is missing, see http://drupal.org/node/1354#functions
And this new hook should be documented in wsclient_rest.api.php.
Comment #3
tito.brasolin CreditAttribution: tito.brasolin commentedThank you klausi, I have generated with Drush Issue Queue Commands a new (and hopefully better) patch.
Comment #4
tito.brasolin CreditAttribution: tito.brasolin commentedPlease look at: https://drupal.org/node/2044587
... maybe my patch can solve PatchRanger's issue too?
Comment #5
PatchRanger CreditAttribution: PatchRanger commentedLooks like it needs review again.
I confirm that patch works in my case (see forementioned #2044587: Empty remaining parameters inside request URI).
Comment #6
klausiPatch from #3 does not apply, see https://drupal.org/node/707484
Comment #7
tito.brasolin CreditAttribution: tito.brasolin commentedThank you klausi, here is a new patch.
Comment #8
PatchRanger CreditAttribution: PatchRanger commentedI am using the patch from #7 at production and confirm that it works perfectly.
Comment #9
PatchRanger CreditAttribution: PatchRanger commentedThis 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 .
Comment #10
tcmug CreditAttribution: tcmug commentedReopening 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.
Comment #11
klausiThat 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?
Comment #12
PatchRanger CreditAttribution: PatchRanger commented@klausi I guess it should though I don't have an opportunity to test it with my current projects. Anyway it looks nice.
Comment #13
klausiThe most recent patch misses the API docs.
Comment #14
adrien.felipe CreditAttribution: adrien.felipe commentedThis now looks integrated in code, even if slightly differently than the patch did initially.
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:
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.