2005-02-18 18:55:29 -05:00
|
|
|
= Action Web Service -- Serving APIs on rails
|
2005-02-18 05:35:25 -05:00
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
Action Web Service provides a way to publish interoperable web service APIs with
|
2005-02-18 05:35:25 -05:00
|
|
|
Rails without spending a lot of time delving into protocol details.
|
|
|
|
|
|
|
|
|
|
|
|
== Features
|
|
|
|
|
|
|
|
* SOAP RPC protocol support
|
|
|
|
* Dynamic WSDL generation for APIs
|
|
|
|
* XML-RPC protocol support
|
|
|
|
* Clients that use the same API definitions as the server for
|
2005-02-18 18:55:29 -05:00
|
|
|
easy interoperability with other Action Web Service based applications
|
2005-02-18 05:35:25 -05:00
|
|
|
* Type signature hints to improve interoperability with static languages
|
|
|
|
* Active Record model class support in signatures
|
|
|
|
|
|
|
|
|
|
|
|
== Defining your APIs
|
|
|
|
|
|
|
|
You specify the methods you want to make available as API methods in an
|
2005-02-18 18:55:29 -05:00
|
|
|
ActionWebService::API::Base derivative, and then specify this API
|
2005-02-18 05:35:25 -05:00
|
|
|
definition class wherever you want to use that API.
|
|
|
|
|
|
|
|
The implementation of the methods is done seperately to the API
|
|
|
|
specification.
|
|
|
|
|
|
|
|
|
|
|
|
==== Method name inflection
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
Action Web Service will camelcase the method names according to Rails Inflector
|
2005-02-18 05:35:25 -05:00
|
|
|
rules for the API visible to public callers. What this means, for example
|
|
|
|
is that the method names in generated WSDL will be camelcased, and callers will
|
|
|
|
have to supply the camelcased name in their requests for the request to
|
|
|
|
succeed.
|
|
|
|
|
|
|
|
If you do not desire this behaviour, you can turn it off with the
|
2005-02-18 18:55:29 -05:00
|
|
|
ActionWebService::API::Base +inflect_names+ option.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
==== Inflection examples
|
|
|
|
|
|
|
|
:add => Add
|
|
|
|
:find_all => FindAll
|
|
|
|
|
|
|
|
|
|
|
|
==== Disabling inflection
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
class PersonAPI < ActionWebService::API::Base
|
2005-02-18 05:35:25 -05:00
|
|
|
inflect_names false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
==== API definition example
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
class PersonAPI < ActionWebService::API::Base
|
2005-02-18 05:35:25 -05:00
|
|
|
api_method :add, :expects => [:string, :string, :bool], :returns => [:int]
|
|
|
|
api_method :remove, :expects => [:int], :returns => [:bool]
|
|
|
|
end
|
|
|
|
|
|
|
|
==== API usage example
|
|
|
|
|
|
|
|
class PersonController < ActionController::Base
|
2005-02-18 16:22:52 -05:00
|
|
|
web_service_api PersonAPI
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
def add
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
== Publishing your APIs
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
Action Web Service uses Action Pack to process protocol requests. There are two
|
2005-02-18 05:35:25 -05:00
|
|
|
modes of dispatching protocol requests, _Direct_, and _Delegated_.
|
|
|
|
|
|
|
|
|
|
|
|
=== Direct dispatching
|
|
|
|
|
2005-06-25 02:27:39 -04:00
|
|
|
This is the default mode. In this mode, public controller instance methods
|
|
|
|
implement the API methods, and parameters are passed through to the methods in
|
|
|
|
accordance with the API specification.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
2005-06-25 02:27:39 -04:00
|
|
|
The return value of the method is sent back as the return value to the
|
2005-02-18 05:35:25 -05:00
|
|
|
caller.
|
|
|
|
|
|
|
|
In this mode, a special <tt>api</tt> action is generated in the target
|
2005-06-25 02:27:39 -04:00
|
|
|
controller to unwrap the protocol request, forward it on to the relevant method
|
2005-02-18 05:35:25 -05:00
|
|
|
and send back the wrapped return value. <em>This action must not be
|
|
|
|
overridden.</em>
|
|
|
|
|
|
|
|
==== Direct dispatching example
|
|
|
|
|
|
|
|
class PersonController < ApplicationController
|
2005-02-18 16:22:52 -05:00
|
|
|
web_service_api PersonAPI
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
def add
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
class PersonAPI < ActionWebService::API::Base
|
2005-02-18 05:35:25 -05:00
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
For this example, protocol requests for +Add+ and +Remove+ methods sent to
|
2005-06-25 02:27:39 -04:00
|
|
|
<tt>/person/api</tt> will be routed to the controller methods +add+ and +remove+.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
=== Delegated dispatching
|
|
|
|
|
2005-02-18 16:22:52 -05:00
|
|
|
This mode can be turned on by setting the +web_service_dispatching_mode+ option
|
2005-02-27 16:21:40 -05:00
|
|
|
in a controller to <tt>:delegated</tt>.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
2005-02-18 16:22:52 -05:00
|
|
|
In this mode, the controller contains one or more web service objects (objects
|
2005-02-18 18:55:29 -05:00
|
|
|
that implement an ActionWebService::API::Base definition). These web service
|
2005-02-18 05:35:25 -05:00
|
|
|
objects are each mapped onto one controller action only.
|
|
|
|
|
|
|
|
==== Delegated dispatching example
|
|
|
|
|
|
|
|
class ApiController < ApplicationController
|
2005-02-18 16:22:52 -05:00
|
|
|
web_service_dispatching_mode :delegated
|
2005-02-18 05:35:25 -05:00
|
|
|
|
2005-02-18 16:22:52 -05:00
|
|
|
web_service :person, PersonService.new
|
2005-02-18 05:35:25 -05:00
|
|
|
end
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
class PersonService < ActionWebService::Base
|
2005-02-18 16:22:52 -05:00
|
|
|
web_service_api PersonAPI
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
def add
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
class PersonAPI < ActionWebService::API::Base
|
2005-02-18 05:35:25 -05:00
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
For this example, all protocol requests for +PersonService+ are
|
|
|
|
sent to the <tt>/api/person</tt> action.
|
|
|
|
|
2005-02-18 16:22:52 -05:00
|
|
|
The <tt>/api/person</tt> action is generated when the +web_service+
|
2005-02-18 05:35:25 -05:00
|
|
|
method is called. <em>This action must not be overridden.</em>
|
|
|
|
|
2005-02-18 16:22:52 -05:00
|
|
|
Other controller actions (actions that aren't the target of a +web_service+ call)
|
2005-02-18 18:55:29 -05:00
|
|
|
are ignored for ActionWebService purposes, and can do normal action tasks.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
|
2005-02-27 16:21:40 -05:00
|
|
|
=== Layered dispatching
|
|
|
|
|
|
|
|
This mode can be turned on by setting the +web_service_dispatching_mode+ option
|
|
|
|
in a controller to <tt>:layered</tt>.
|
|
|
|
|
|
|
|
This mode is similar to _delegated_ mode, in that multiple web service objects
|
|
|
|
can be attached to one controller, however, all protocol requests are sent to a
|
|
|
|
single endpoint.
|
|
|
|
|
2005-04-05 17:37:48 -04:00
|
|
|
Use this mode when you want to share code between XML-RPC and SOAP clients,
|
|
|
|
for APIs where the XML-RPC method names have prefixes. An example of such
|
|
|
|
a method name would be <tt>blogger.newPost</tt>.
|
2005-02-27 16:21:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
==== Layered dispatching example
|
|
|
|
|
|
|
|
|
|
|
|
class ApiController < ApplicationController
|
|
|
|
web_service_dispatching_mode :layered
|
|
|
|
|
|
|
|
web_service :mt, MovableTypeService.new
|
|
|
|
web_service :blogger, BloggerService.new
|
|
|
|
web_service :metaWeblog, MetaWeblogService.new
|
|
|
|
end
|
|
|
|
|
|
|
|
class MovableTypeService < ActionWebService::Base
|
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
class BloggerService < ActionWebService::Base
|
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
class MetaWeblogService < ActionWebService::API::Base
|
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-04-05 17:37:48 -04:00
|
|
|
For this example, an XML-RPC call for a method with a name like
|
|
|
|
<tt>mt.getCategories</tt> will be sent to the <tt>getCategories</tt>
|
2005-02-27 16:21:40 -05:00
|
|
|
method on the <tt>:mt</tt> service.
|
|
|
|
|
|
|
|
|
2005-06-25 02:27:39 -04:00
|
|
|
== Customizing WSDL generation
|
|
|
|
|
|
|
|
You can customize the names used for the SOAP bindings in the generated
|
|
|
|
WSDL by using the wsdl_service_name option in a controller:
|
|
|
|
|
|
|
|
class WsController < ApplicationController
|
|
|
|
wsdl_service_name 'MyApp'
|
|
|
|
end
|
|
|
|
|
|
|
|
You can also customize the namespace used in the generated WSDL for
|
|
|
|
custom types and message definition types:
|
|
|
|
|
|
|
|
class WsController < ApplicationController
|
|
|
|
wsdl_namespace 'http://my.company.com/app/wsapi'
|
|
|
|
end
|
|
|
|
|
|
|
|
The default namespace used is 'urn:ActionWebService', if you don't supply
|
|
|
|
one.
|
|
|
|
|
2005-03-29 07:31:39 -05:00
|
|
|
== Testing your APIs
|
|
|
|
|
|
|
|
|
|
|
|
=== Functional testing
|
|
|
|
|
|
|
|
You can perform testing of your APIs by creating a functional test for the
|
|
|
|
controller dispatching the API, and calling #invoke in the test case to
|
|
|
|
perform the invocation.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
class PersonApiControllerTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@controller = PersonController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add
|
|
|
|
result = invoke :remove, 1
|
|
|
|
assert_equal true, result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
This example invokes the API method <tt>test</tt>, defined on
|
|
|
|
the PersonController, and returns the result.
|
|
|
|
|
|
|
|
|
|
|
|
=== Scaffolding
|
|
|
|
|
|
|
|
You can also test your APIs with a web browser by attaching scaffolding
|
|
|
|
to the controller.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
class PersonController
|
|
|
|
web_service_scaffold :invocation
|
|
|
|
end
|
|
|
|
|
|
|
|
This creates an action named <tt>invocation</tt> on the PersonController.
|
|
|
|
|
|
|
|
Navigating to this action lets you select the method to invoke, supply the parameters,
|
|
|
|
and view the result of the invocation.
|
|
|
|
|
|
|
|
|
2005-02-18 05:35:25 -05:00
|
|
|
== Using the client support
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
Action Web Service includes client classes that can use the same API
|
2005-02-18 05:35:25 -05:00
|
|
|
definition as the server. The advantage of this approach is that your client
|
|
|
|
will have the same support for Active Record and structured types as the
|
|
|
|
server, and can just use them directly, and rely on the marshaling to Do The
|
|
|
|
Right Thing.
|
|
|
|
|
|
|
|
*Note*: The client support is intended for communication between Ruby on Rails
|
2005-02-18 18:55:29 -05:00
|
|
|
applications that both use Action Web Service. It may work with other servers, but
|
2005-02-18 05:35:25 -05:00
|
|
|
that is not its intended use, and interoperability can't be guaranteed, especially
|
|
|
|
not for .NET web services.
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
Web services protocol specifications are complex, and Action Web Service client
|
2005-02-18 16:22:52 -05:00
|
|
|
support can only be guaranteed to work with a subset.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
==== Factory created client example
|
|
|
|
|
|
|
|
class BlogManagerController < ApplicationController
|
2005-02-18 16:22:52 -05:00
|
|
|
web_client_api :blogger, :xmlrpc, 'http://url/to/blog/api/RPC2', :handler_name => 'blogger'
|
2005-02-18 05:35:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class SearchingController < ApplicationController
|
2005-02-18 16:22:52 -05:00
|
|
|
web_client_api :google, :soap, 'http://url/to/blog/api/beta', :service_name => 'GoogleSearch'
|
2005-02-18 05:35:25 -05:00
|
|
|
end
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
See ActionWebService::API::ActionController::ClassMethods for more details.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
==== Manually created client example
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
class PersonAPI < ActionWebService::API::Base
|
2005-02-18 05:35:25 -05:00
|
|
|
api_method :find_all, :returns => [[Person]]
|
|
|
|
end
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
soap_client = ActionWebService::Client::Soap.new(PersonAPI, "http://...")
|
2005-02-18 05:35:25 -05:00
|
|
|
persons = soap_client.find_all
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
class BloggerAPI < ActionWebService::API::Base
|
2005-02-18 05:35:25 -05:00
|
|
|
inflect_names false
|
|
|
|
api_method :getRecentPosts, :returns => [[Blog::Post]]
|
|
|
|
end
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
blog = ActionWebService::Client::XmlRpc.new(BloggerAPI, "http://.../xmlrpc", :handler_name => "blogger")
|
2005-02-18 05:35:25 -05:00
|
|
|
posts = blog.getRecentPosts
|
|
|
|
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
See ActionWebService::Client::Soap and ActionWebService::Client::XmlRpc for more details.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
== Dependencies
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
Action Web Service requires that the Action Pack and Active Record are either
|
2005-02-18 05:35:25 -05:00
|
|
|
available to be required immediately or are accessible as GEMs.
|
|
|
|
|
|
|
|
It also requires a version of Ruby that includes SOAP support in the standard
|
|
|
|
library. At least version 1.8.2 final (2004-12-25) of Ruby is recommended, this
|
|
|
|
is the version tested against.
|
|
|
|
|
|
|
|
|
|
|
|
== Download
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
The latest Action Web Service version can be downloaded from
|
2005-02-18 05:35:25 -05:00
|
|
|
http://rubyforge.org/projects/actionservice
|
|
|
|
|
|
|
|
|
|
|
|
== Installation
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
You can install Action Web Service with the following command.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
% [sudo] ruby setup.rb
|
|
|
|
|
|
|
|
|
|
|
|
== License
|
|
|
|
|
2005-02-18 18:55:29 -05:00
|
|
|
Action Web Service is released under the MIT license.
|
2005-02-18 05:35:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
== Support
|
|
|
|
|
|
|
|
The Ruby on Rails mailing list
|
|
|
|
|
|
|
|
Or, to contact the author, send mail to bitserf@gmail.com
|
|
|
|
|