0

I've been trying to get this working all day so this is my last resort.

I am trying to call an api using guzzle, and this drupal_symfony_inject module: https://github.com/Capgemini/drupal_symfony_inject

I have the following config in hook_symfony_yaml_config_params():

// Assets client.
$assets_resources_file = $resources_folder . '/moderation.json';
$param_array['bubl_api_assets_service_client.service.description'] = $assets_resources_file;
$param_array['bubl_api_assets_client.class'] = 'BUBL\Clients\Assets\Client';

And this in my service definition:

{
  "name": "BUBL moderation client",
  "apiVersion": "v1",
  "description": "BUBL moderation REST API client",
  "operations": {
    "getAllBubls": {
      "httpMethod": "GET",
      "uri": "/su/bubls",
      "responseModel": "getJSONResponse",
      "parameters": {
        "before": {
          "location": "uri",
          "type": "integer",
          "required": false
        },
        "after": {
          "location": "uri",
          "type": "integer",
          "required": false
        },
        "max": {
          "location": "uri",
          "type": "integer",
          "required": false
        }
      }
    }
  },
  "models": {
    "getJSONResponse": {
      "type": "object",
      "additionalProperties": {
        "location": "json"
      }
    },
    "getHTMLResponse": {
      "type": "object",
      "properties": {
        "html": {
          "location": "body",
          "type": "string"
        }
      }
    }
  }
}

I have a client with the operations simply returning the response from guzzle, for example:

public function getAllBubls($before = NULL, $after = NULL) {
    $response = $this->client->sendRequest('getAllBubls', ['before' => $before, 'after' => $after]);
    return $response;
  }

And when using it it works absolutely fine, like so:

global $_drupal_symfony_di_container;
    /** @var BUBL\Clients\Moderation\Client $client */
    $client = $_drupal_symfony_di_container->get('bubl_api_moderation_client');
    if (is_callable(array($client, $service_name))) {
      $response = $client->{$service_name}();
      return $data_source = $response['bubls'];
    }

Now, my problem is that the response is ran through json_decode before I get hold of it, and giving me an array, which is causing me all kinds of problems with the migrate module. I know that it is because of the responseModel set at

"responseModel": "getJSONResponse",

However, I can't find a way to simply request a raw response. I have tried (as was alluded to in the documentation), removing the responseModal altogether and adding in:

"responseType": "primitive",

and (separately I mean)

"responseModel": "getHTMLResponse",

But unfortunately I wouldn't receive any data back with either - just an empty array. I just can't seem to find a way to ignore all of the parsing and just get the response back in JSON? Is it possible? I've also tried to create another Model to use, but the types are either array or object, and the rest of the properties are really confusing to me in the documentation, so nothing I've tried is helping. It doesn't seem like it shouldn't go through a Model or Response Class at all, and that there is some way to bypass it perhaps ("primitive" would make sense to me, but alas not).

BTW I'm new to guzzle, and I know this seems a bit over engineered for this one call, but it is important for elsewhere, it's in place and I would like to get my head round it if it's possible to use it.

Thanks.

Michael Mallett
  • 734
  • 1
  • 12
  • 28
  • This might be of help: http://guzzle3.readthedocs.org/http-client/response.html#response-body - you should be able to get the response as a string. – Richard Nov 19 '15 at 19:06
  • I've read these documents, but I can't figure out what I can do with them, the implementation we have is wrapped up in the service definitions so it doesn't look like I can intercept the request at that point – Michael Mallett Nov 20 '15 at 09:38
  • Ok I found some additional docs, I'll put it in an answer. Thanks – Michael Mallett Nov 20 '15 at 10:14

1 Answers1

0

Ok so I found a couple of things that helped me, namely this https://github.com/Rarst/wporg-client/blob/33fb0dcce9f170b92824d09f5717ca814d7ecd29/php/WporgService.php

Which is based on guzzle, so I took the parameters from the 'body' response response model and made this:

"getRawResponse": {
      "type": "object",
      "properties": {
        "body": {
          "location": "body",
          "type": "string"
        }
      }
    }

Which returned a Guzzle Stream. So searching for stream docs I found this Not getting expected response from Guzzle

Which directed me to change my request to

public function getAllBubls($before = NULL, $after = NULL) {
    $response = $this->client->sendRequest('getAllBubls', ['before' => $before, 'after' => $after]);
    // Get the raw JSON response.
    return $response['body']->getContents();
  }

Which has returned to me the unparsed JSON.

Community
  • 1
  • 1
Michael Mallett
  • 734
  • 1
  • 12
  • 28
  • This is not the RAW HTTP response prior to parsing; but is a fair way towards that. I Came here looking for the headers and body to be RAW and combined. I wouldn't want the low-level transport details, but the low-level RAW bytes could be useful in some situations. – MrMesees Jan 16 '22 at 05:19