문제

I use Guzzle to make requests to a webservice.

I have JSON files that look something like this:

{
    "name": "Webservice name",
    "apiVersion": "1.0",
    "description": "description",
    "operations": {
        "commandName1": {
            "httpMethod": "POST",
            "uri": "some/uri/to/some/resource/{value}",
            "summary": "description",
            "parameters": {
                "value": {
                    "location": "uri",
                    "description": "description"
                }
            }
        },
        "commandName2": {
            "httpMethod": "POST",
            "uri": "some/uri/to/some/resource/{value}",
            "summary": "description",
            "parameters": {
                "value": {
                    "location": "uri",
                    "description": "description"
                }
            }
        }
    }
}

and the code that uses it looks something like this:

$client = new Client(); // instance of Guzzle\Service\Client

$this->client->setDefaultOption(
    'auth',
    array('admin', 'admin', 'Basic')
);

$this->client->setDefaultOption(
    'headers',
    array('Accept' => 'text/html', 'Content-Type' => 'text/html')
);

$description = ServiceDescription::factory('/path/to/json/file/with/routes');
$client->setDescription($description);

$params = array(
    'command.request_options' = array(
        'timeout'         => 5,
        'connect_timeout' => 2
    )
);

$command = $client->getCommand('commandName1', $params);
$command->prepare();

$client->execute($command);

As you can see, I specify the Content-Type and Accept headers in the PHP code. Is there some way I can move that information in the JSON file and specify different values for each operation ? For example: I want "commandName1" to have HTML as content type, but "commandName2" to have JSON.

I want to do this in order to avoid a lot of code duplication.

I've been looking on the web and in Guzzle's documentation for the past 2 hours and came up empty. However, in my opinion, the documentation is kind of poorly written1 and I did miss things in the past while reading it. So it's very possible it happened again.

Did anyone ever had to do something like this ? How did you solve it ? Thank you in advance.

1 = by "poorly written" I actually mean that every part is incomplete. Every chapter seems to touch on a subject, but NEVER provides actually complete or in-depth description of parameters, methods etc or its full capabilities. There is NO CODE SNIPPET that is a SSCCE so that you can see it work in front of your eyes in less than 2 minutes of copy-pasting. But this is another subject...

도움이 되었습니까?

해결책

I looked at Guzzle's source code and indeed there is no way to add such information to the JSON file.

However I succeeded in changing this:

$params = array(
    'command.request_options' = array(
        'timeout'         => 5,
        'connect_timeout' => 2
    )
);

to this:

$params = array(
    'command.request_options' => array(
        'timeout'         => 5,
        'connect_timeout' => 2
    ),
    'command.headers' => array(
        'Accept'        => 'whatever value I want',
        'Content-Type'  => 'whatever value I want'
    )
);

and it worked.

Since that part of the code is in a separate / common class that every other class uses, there's no code duplication and therefore it works... kind of.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top