[Solar-talk] Sungrazr_Server: Request for comments

Clay Loveless clay at killersoft.com
Tue Dec 18 17:53:06 CST 2007


Hey folks,

If anyone is interested, I'm implementing an abstracted web services  
server for Solar, which is similar (at least in theory) to Zend_Server  
and bits in Zend Framework that inherit from it, such as  
Zend_Rest_Server.

This work is sponsored by and donated by Mashery.

I'm interested in comments/feedback from the Solar community on this  
work, as I hope it will prove to be useful to more people than just me/ 
Mashery.

The 5,000 ft view is this:

Sungrazr_Server:
     Factory class to load a server adapter, similar to many other  
factories within Solar.
     http://sungrazr.com/browser/trunk/Sungrazr/Server.php

Sungrazr_Server_Adapter:
     Abstract class which handles setting up server introspection,  
returning responses, etc.
         - Leverages Solar_Docs_Apiref for introspection, since a lot  
of handy Reflection
           stuff is already in there. (Actually calls  
Sungrazr_Server_Api.)
         - Implements Serializable interface, so adapters can use  
custom serialize/unserialize
           methods for XML-RPC, JSON-RPC, WHO-KNOWS-WHAT'S-NEXT-RPC,  
etc.
     http://sungrazr.com/browser/trunk/Sungrazr/Server/Adapter.php

Sungrazr_Server_Api:
     Extends Solar_Docs_Apiref to provide equivalent behavior to  
Zend_Server_Reflection.
     Supports internal caching so that request handler classes don't  
have to be analyzed
     with the Reflection API repetitively.
     http://sungrazr.com/browser/trunk/Sungrazr/Server/Api.php

Sungrazr_Server_Handler:
     Abstract class intended to act as a base for an actual API  
request handler class.
     Extends Solar_Base and adds some handy fault-response methods and  
a couple properties
     for awareness of what web service method was used to call the  
class, should it
     be necessary.
     http://sungrazr.com/browser/trunk/Sungrazr/Server/Handler.php

Sungrazr_Server_Adapter_JsonRpc:
     First adapter. Supports JSON-RPC, described at json-rpc.org.
     http://sungrazr.com/browser/trunk/Sungrazr/Server/Adapter/JsonRpc.php

Sungrazr_Docs_Phpdoc:
     Minor extension to Solar_Doc_Phpdoc to support a @supports  
docblock tag that
     Sungrazr_Server_Api can pick up. Can be used to tell the  
forthcoming
     Sungrazr_Server_Adapter_Rest class what methods may/may not be  
supported by specific
     HTTP request methods.
     http://sungrazr.com/browser/trunk/Sungrazr/Docs/Phpdoc.php


It's all still a little rough at the moment, but is iterating rapidly  
this week.

Example usage:

server.php:

$request = Solar::factory('Solar_Request');
$server = Solar::factory('Sungrazr_Server', array('adapter' =>  
'Sungrazr_Server_Adapter_JsonRpc'));
$server->addClass('Vendor_Api_Stuff');

$server->handle($request);


So... the class:

Vendor_Api_Stuff extends Sungrazr_Server_Handler:
...
public function create($apikey, $properties) { ... }


... is callable like this:

$url = 'http://example.com/api

$call = array(
     'method' => 'stuff.create',
     'params' => array(
         array('apikey' => 'foo'),
         array('properties' => array(
             'first_name' => 'John',
             'last_name'  => 'Public',
         )),
     ),
     'id' => 55
);


$call = json_encode($call);

$req = new HttpRequest($url, HttpRequest::METH_POST);
$req->setBody($call);
$msg = $req->send();


... sends a message via POST to the JSON-RPC api that has a POST body  
of:

{"method":"stuff.create","params":[{"apikey":"foo"},{"properties": 
{"first_name":"John","last_name":"Public"}}],"id":55}


And receives a response of:

{"result":{"success":true},"error":null,"id":55}


... more to come. But, that's what's there now. Comments/feedback  
welcome!

Regards,
Clay


--
Killersoft.com





More information about the Solar-talk mailing list