| 1 |
= Action Web Service -- Serving APIs on rails |
|---|
| 2 |
|
|---|
| 3 |
Action Web Service provides a way to publish interoperable web service APIs with |
|---|
| 4 |
Rails without spending a lot of time delving into protocol details. |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
== Features |
|---|
| 8 |
|
|---|
| 9 |
* SOAP RPC protocol support |
|---|
| 10 |
* Dynamic WSDL generation for APIs |
|---|
| 11 |
* XML-RPC protocol support |
|---|
| 12 |
* Clients that use the same API definitions as the server for |
|---|
| 13 |
easy interoperability with other Action Web Service based applications |
|---|
| 14 |
* Type signature hints to improve interoperability with static languages |
|---|
| 15 |
* Active Record model class support in signatures |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
== Defining your APIs |
|---|
| 19 |
|
|---|
| 20 |
You specify the methods you want to make available as API methods in an |
|---|
| 21 |
ActionWebService::API::Base derivative, and then specify this API |
|---|
| 22 |
definition class wherever you want to use that API. |
|---|
| 23 |
|
|---|
| 24 |
The implementation of the methods is done separately from the API |
|---|
| 25 |
specification. |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
==== Method name inflection |
|---|
| 29 |
|
|---|
| 30 |
Action Web Service will camelcase the method names according to Rails Inflector |
|---|
| 31 |
rules for the API visible to public callers. What this means, for example, |
|---|
| 32 |
is that the method names in generated WSDL will be camelcased, and callers will |
|---|
| 33 |
have to supply the camelcased name in their requests for the request to |
|---|
| 34 |
succeed. |
|---|
| 35 |
|
|---|
| 36 |
If you do not desire this behaviour, you can turn it off with the |
|---|
| 37 |
ActionWebService::API::Base +inflect_names+ option. |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
==== Inflection examples |
|---|
| 41 |
|
|---|
| 42 |
:add => Add |
|---|
| 43 |
:find_all => FindAll |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
==== Disabling inflection |
|---|
| 47 |
|
|---|
| 48 |
class PersonAPI < ActionWebService::API::Base |
|---|
| 49 |
inflect_names false |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
==== API definition example |
|---|
| 54 |
|
|---|
| 55 |
class PersonAPI < ActionWebService::API::Base |
|---|
| 56 |
api_method :add, :expects => [:string, :string, :bool], :returns => [:int] |
|---|
| 57 |
api_method :remove, :expects => [:int], :returns => [:bool] |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
==== API usage example |
|---|
| 61 |
|
|---|
| 62 |
class PersonController < ActionController::Base |
|---|
| 63 |
web_service_api PersonAPI |
|---|
| 64 |
|
|---|
| 65 |
def add |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
def remove |
|---|
| 69 |
end |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
== Publishing your APIs |
|---|
| 74 |
|
|---|
| 75 |
Action Web Service uses Action Pack to process protocol requests. There are two |
|---|
| 76 |
modes of dispatching protocol requests, _Direct_, and _Delegated_. |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
=== Direct dispatching |
|---|
| 80 |
|
|---|
| 81 |
This is the default mode. In this mode, public controller instance methods |
|---|
| 82 |
implement the API methods, and parameters are passed through to the methods in |
|---|
| 83 |
accordance with the API specification. |
|---|
| 84 |
|
|---|
| 85 |
The return value of the method is sent back as the return value to the |
|---|
| 86 |
caller. |
|---|
| 87 |
|
|---|
| 88 |
In this mode, a special <tt>api</tt> action is generated in the target |
|---|
| 89 |
controller to unwrap the protocol request, forward it on to the relevant method |
|---|
| 90 |
and send back the wrapped return value. <em>This action must not be |
|---|
| 91 |
overridden.</em> |
|---|
| 92 |
|
|---|
| 93 |
==== Direct dispatching example |
|---|
| 94 |
|
|---|
| 95 |
class PersonController < ApplicationController |
|---|
| 96 |
web_service_api PersonAPI |
|---|
| 97 |
|
|---|
| 98 |
def add |
|---|
| 99 |
end |
|---|
| 100 |
|
|---|
| 101 |
def remove |
|---|
| 102 |
end |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
class PersonAPI < ActionWebService::API::Base |
|---|
| 106 |
... |
|---|
| 107 |
end |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
For this example, protocol requests for +Add+ and +Remove+ methods sent to |
|---|
| 111 |
<tt>/person/api</tt> will be routed to the controller methods +add+ and +remove+. |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
=== Delegated dispatching |
|---|
| 115 |
|
|---|
| 116 |
This mode can be turned on by setting the +web_service_dispatching_mode+ option |
|---|
| 117 |
in a controller to <tt>:delegated</tt>. |
|---|
| 118 |
|
|---|
| 119 |
In this mode, the controller contains one or more web service objects (objects |
|---|
| 120 |
that implement an ActionWebService::API::Base definition). These web service |
|---|
| 121 |
objects are each mapped onto one controller action only. |
|---|
| 122 |
|
|---|
| 123 |
==== Delegated dispatching example |
|---|
| 124 |
|
|---|
| 125 |
class ApiController < ApplicationController |
|---|
| 126 |
web_service_dispatching_mode :delegated |
|---|
| 127 |
|
|---|
| 128 |
web_service :person, PersonService.new |
|---|
| 129 |
end |
|---|
| 130 |
|
|---|
| 131 |
class PersonService < ActionWebService::Base |
|---|
| 132 |
web_service_api PersonAPI |
|---|
| 133 |
|
|---|
| 134 |
def add |
|---|
| 135 |
end |
|---|
| 136 |
|
|---|
| 137 |
def remove |
|---|
| 138 |
end |
|---|
| 139 |
end |
|---|
| 140 |
|
|---|
| 141 |
class PersonAPI < ActionWebService::API::Base |
|---|
| 142 |
... |
|---|
| 143 |
end |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
For this example, all protocol requests for +PersonService+ are |
|---|
| 147 |
sent to the <tt>/api/person</tt> action. |
|---|
| 148 |
|
|---|
| 149 |
The <tt>/api/person</tt> action is generated when the +web_service+ |
|---|
| 150 |
method is called. <em>This action must not be overridden.</em> |
|---|
| 151 |
|
|---|
| 152 |
Other controller actions (actions that aren't the target of a +web_service+ call) |
|---|
| 153 |
are ignored for ActionWebService purposes, and can do normal action tasks. |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
=== Layered dispatching |
|---|
| 157 |
|
|---|
| 158 |
This mode can be turned on by setting the +web_service_dispatching_mode+ option |
|---|
| 159 |
in a controller to <tt>:layered</tt>. |
|---|
| 160 |
|
|---|
| 161 |
This mode is similar to _delegated_ mode, in that multiple web service objects |
|---|
| 162 |
can be attached to one controller, however, all protocol requests are sent to a |
|---|
| 163 |
single endpoint. |
|---|
| 164 |
|
|---|
| 165 |
Use this mode when you want to share code between XML-RPC and SOAP clients, |
|---|
| 166 |
for APIs where the XML-RPC method names have prefixes. An example of such |
|---|
| 167 |
a method name would be <tt>blogger.newPost</tt>. |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
==== Layered dispatching example |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
class ApiController < ApplicationController |
|---|
| 174 |
web_service_dispatching_mode :layered |
|---|
| 175 |
|
|---|
| 176 |
web_service :mt, MovableTypeService.new |
|---|
| 177 |
web_service :blogger, BloggerService.new |
|---|
| 178 |
web_service :metaWeblog, MetaWeblogService.new |
|---|
| 179 |
end |
|---|
| 180 |
|
|---|
| 181 |
class MovableTypeService < ActionWebService::Base |
|---|
| 182 |
... |
|---|
| 183 |
end |
|---|
| 184 |
|
|---|
| 185 |
class BloggerService < ActionWebService::Base |
|---|
| 186 |
... |
|---|
| 187 |
end |
|---|
| 188 |
|
|---|
| 189 |
class MetaWeblogService < ActionWebService::API::Base |
|---|
| 190 |
... |
|---|
| 191 |
end |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
For this example, an XML-RPC call for a method with a name like |
|---|
| 195 |
<tt>mt.getCategories</tt> will be sent to the <tt>getCategories</tt> |
|---|
| 196 |
method on the <tt>:mt</tt> service. |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
== Customizing WSDL generation |
|---|
| 200 |
|
|---|
| 201 |
You can customize the names used for the SOAP bindings in the generated |
|---|
| 202 |
WSDL by using the wsdl_service_name option in a controller: |
|---|
| 203 |
|
|---|
| 204 |
class WsController < ApplicationController |
|---|
| 205 |
wsdl_service_name 'MyApp' |
|---|
| 206 |
end |
|---|
| 207 |
|
|---|
| 208 |
You can also customize the namespace used in the generated WSDL for |
|---|
| 209 |
custom types and message definition types: |
|---|
| 210 |
|
|---|
| 211 |
class WsController < ApplicationController |
|---|
| 212 |
wsdl_namespace 'http://my.company.com/app/wsapi' |
|---|
| 213 |
end |
|---|
| 214 |
|
|---|
| 215 |
The default namespace used is 'urn:ActionWebService', if you don't supply |
|---|
| 216 |
one. |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
== ActionWebService and UTF-8 |
|---|
| 220 |
|
|---|
| 221 |
If you're going to be sending back strings containing non-ASCII UTF-8 |
|---|
| 222 |
characters using the <tt>:string</tt> data type, you need to make sure that |
|---|
| 223 |
Ruby is using UTF-8 as the default encoding for its strings. |
|---|
| 224 |
|
|---|
| 225 |
The default in Ruby is to use US-ASCII encoding for strings, which causes a string |
|---|
| 226 |
validation check in the Ruby SOAP library to fail and your string to be sent |
|---|
| 227 |
back as a Base-64 value, which may confuse clients that expected strings |
|---|
| 228 |
because of the WSDL. |
|---|
| 229 |
|
|---|
| 230 |
Two ways of setting the default string encoding are: |
|---|
| 231 |
|
|---|
| 232 |
* Start Ruby using the <tt>-Ku</tt> command-line option to the Ruby executable |
|---|
| 233 |
* Set the <tt>$KCODE</tt> flag in <tt>config/environment.rb</tt> to the |
|---|
| 234 |
string <tt>'UTF8'</tt> |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
== Testing your APIs |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
=== Functional testing |
|---|
| 241 |
|
|---|
| 242 |
You can perform testing of your APIs by creating a functional test for the |
|---|
| 243 |
controller dispatching the API, and calling #invoke in the test case to |
|---|
| 244 |
perform the invocation. |
|---|
| 245 |
|
|---|
| 246 |
Example: |
|---|
| 247 |
|
|---|
| 248 |
class PersonApiControllerTest < Test::Unit::TestCase |
|---|
| 249 |
def setup |
|---|
| 250 |
@controller = PersonController.new |
|---|
| 251 |
@request = ActionController::TestRequest.new |
|---|
| 252 |
@response = ActionController::TestResponse.new |
|---|
| 253 |
end |
|---|
| 254 |
|
|---|
| 255 |
def test_add |
|---|
| 256 |
result = invoke :remove, 1 |
|---|
| 257 |
assert_equal true, result |
|---|
| 258 |
end |
|---|
| 259 |
end |
|---|
| 260 |
|
|---|
| 261 |
This example invokes the API method <tt>test</tt>, defined on |
|---|
| 262 |
the PersonController, and returns the result. |
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
=== Scaffolding |
|---|
| 266 |
|
|---|
| 267 |
You can also test your APIs with a web browser by attaching scaffolding |
|---|
| 268 |
to the controller. |
|---|
| 269 |
|
|---|
| 270 |
Example: |
|---|
| 271 |
|
|---|
| 272 |
class PersonController |
|---|
| 273 |
web_service_scaffold :invocation |
|---|
| 274 |
end |
|---|
| 275 |
|
|---|
| 276 |
This creates an action named <tt>invocation</tt> on the PersonController. |
|---|
| 277 |
|
|---|
| 278 |
Navigating to this action lets you select the method to invoke, supply the parameters, |
|---|
| 279 |
and view the result of the invocation. |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
== Using the client support |
|---|
| 283 |
|
|---|
| 284 |
Action Web Service includes client classes that can use the same API |
|---|
| 285 |
definition as the server. The advantage of this approach is that your client |
|---|
| 286 |
will have the same support for Active Record and structured types as the |
|---|
| 287 |
server, and can just use them directly, and rely on the marshaling to Do The |
|---|
| 288 |
Right Thing. |
|---|
| 289 |
|
|---|
| 290 |
*Note*: The client support is intended for communication between Ruby on Rails |
|---|
| 291 |
applications that both use Action Web Service. It may work with other servers, but |
|---|
| 292 |
that is not its intended use, and interoperability can't be guaranteed, especially |
|---|
| 293 |
not for .NET web services. |
|---|
| 294 |
|
|---|
| 295 |
Web services protocol specifications are complex, and Action Web Service client |
|---|
| 296 |
support can only be guaranteed to work with a subset. |
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
==== Factory created client example |
|---|
| 300 |
|
|---|
| 301 |
class BlogManagerController < ApplicationController |
|---|
| 302 |
web_client_api :blogger, :xmlrpc, 'http://url/to/blog/api/RPC2', :handler_name => 'blogger' |
|---|
| 303 |
end |
|---|
| 304 |
|
|---|
| 305 |
class SearchingController < ApplicationController |
|---|
| 306 |
web_client_api :google, :soap, 'http://url/to/blog/api/beta', :service_name => 'GoogleSearch' |
|---|
| 307 |
end |
|---|
| 308 |
|
|---|
| 309 |
See ActionWebService::API::ActionController::ClassMethods for more details. |
|---|
| 310 |
|
|---|
| 311 |
==== Manually created client example |
|---|
| 312 |
|
|---|
| 313 |
class PersonAPI < ActionWebService::API::Base |
|---|
| 314 |
api_method :find_all, :returns => [[Person]] |
|---|
| 315 |
end |
|---|
| 316 |
|
|---|
| 317 |
soap_client = ActionWebService::Client::Soap.new(PersonAPI, "http://...") |
|---|
| 318 |
persons = soap_client.find_all |
|---|
| 319 |
|
|---|
| 320 |
class BloggerAPI < ActionWebService::API::Base |
|---|
| 321 |
inflect_names false |
|---|
| 322 |
api_method :getRecentPosts, :returns => [[Blog::Post]] |
|---|
| 323 |
end |
|---|
| 324 |
|
|---|
| 325 |
blog = ActionWebService::Client::XmlRpc.new(BloggerAPI, "http://.../xmlrpc", :handler_name => "blogger") |
|---|
| 326 |
posts = blog.getRecentPosts |
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
See ActionWebService::Client::Soap and ActionWebService::Client::XmlRpc for more details. |
|---|
| 330 |
|
|---|
| 331 |
== Dependencies |
|---|
| 332 |
|
|---|
| 333 |
Action Web Service requires that the Action Pack and Active Record are either |
|---|
| 334 |
available to be required immediately or are accessible as GEMs. |
|---|
| 335 |
|
|---|
| 336 |
It also requires a version of Ruby that includes SOAP support in the standard |
|---|
| 337 |
library. At least version 1.8.2 final (2004-12-25) of Ruby is recommended; this |
|---|
| 338 |
is the version tested against. |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
== Download |
|---|
| 342 |
|
|---|
| 343 |
The latest Action Web Service version can be downloaded from |
|---|
| 344 |
http://rubyforge.org/projects/actionservice |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
== Installation |
|---|
| 348 |
|
|---|
| 349 |
You can install Action Web Service with the following command. |
|---|
| 350 |
|
|---|
| 351 |
% [sudo] ruby setup.rb |
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
== License |
|---|
| 355 |
|
|---|
| 356 |
Action Web Service is released under the MIT license. |
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
== Support |
|---|
| 360 |
|
|---|
| 361 |
The Ruby on Rails mailing list |
|---|
| 362 |
|
|---|
| 363 |
Or, to contact the author, send mail to bitserf@gmail.com |
|---|
| 364 |
|
|---|