문제

I have a basic service description schema to get a user model that looks like this:

{
    "name": "API",
    "baseUrl": "http://localhost/",
    "operations": {
        "GetUser": {
            "httpMethod": "GET",
            "uri": "users/{user_id}",
            "summary": "Show user details",
            "responseClass": "GetUserOutput",
            "parameters": {
                "user_id": {
                    "location": "uri",
                    "description": "ID of the user to be returned"
                 }
             }
         }
    },
    "models": {
        "User" : {
            "type": "object",
            "properties": {
                "id": {
                    "location": "json",
                    "type": "integer",
                    "sentAs": "user_id"
                 },
                "username": {
                     "location": "json",
                     "type": "string"
                 },
                "email": {
                    "location": "json",
                    "type": "string"
                }
            }
        },
       "GetUserOutput": {
            "$ref": "User"
        }
    }
}

my client does the following:

require_once('../../vendor/autoload.php');

$client = new \Guzzle\Service\Client();
$client->setDescription(\Guzzle\Service\Description\ServiceDescription::factory(__DIR__ . '/client.json'));
$authPlugin = new \Guzzle\Plugin\CurlAuth\CurlAuthPlugin('23', '9bd2cb3f1bccc01c0c1091d7e88e51b208b3792b');

$client->addSubscriber($authPlugin);
$command = $client->getCommand('getUser', array('user_id' => 23));
$request = $command->prepare();
$request->addHeader('Accept', 'application/json');

try {
    $result = $command->execute();
    echo '<pre>' . print_r($result, true) . '</pre>';
}

which returns a Guzzle\Service\Resource\Model Object, which at the bottom contains the user data i wanted:

[data:protected] => Array
    (
        [user] => Array
            (
                [id] => 23
                [username] => gardni
                [email] => email@email.com

how do i map this to the schema object? or more importantly my own application object? apparently the solution here doesn't work:

class User implements ResponseClassInterface
{
    public static function fromCommand(OperationCommand $command)
    {
        $parser = OperationResponseParser::getInstance();
        $parsedModel = $parser->parse($command);

        return new self($parsedModel);
    }

    public function __construct(Model $parsedModel)
    {
        // Do something with the parsed model object
    }
}
도움이 되었습니까?

해결책

not sure if working how intended but in order to get an object from the schema - first i changed the json to include a responseType of class and a responseClassof the model directory:

"uri": "users/{user_id}",
"summary": "Show user details",
"responseType": "class",
"responseClass": "\\Users\\User",

then in the user model i built the user in the fromCommand

public static function fromCommand(\Guzzle\Service\Command\OperationCommand $command)
{
    $result = $command->getResponse()->json();
    $user = new self();
    $user->setId($result['user']['id']);
    $user->setUsername($result['user']['username']);
    $user->setEmail($result['user']['email']);
    return $user;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top