Several web service interfaces have been built and shared within the Sharendipity community including services for Facebook, Twitter, Flickr, Posterous, and Google Docs. Using these components you can quickly and easily access the data from these sites/services.
But what happens if you can’t find the service you’re looking for? Or if you want to improve on one of the existing services and make a better one yourself?
This post is here to help. We’ll walk you through some of the basic elements for building a new component that interfaces with an external web service. To get started, first launch the Sharendipity editor.
Create new object type
Creating a new type provides you with the ability to define custom assets that perform a particular job or store specific data. They don’t necessarily have to be a displayable actor inside your World. The definitions for these new assets are entirely up to you.
In the case of a web service, you’ll define the location of the service, describe inputs, and then define custom actions that let the users of your service interact with it.
Open the Explorer dialog and select New from the Types tab.
Name your new type. For example, “Twitter Service – public timeline”
Declare that your new type “is a REST Web Service” by clicking the + sign next to its name, and selecting the is a button. You can find the REST Web Service type by selecting Other… and navigating to the Local->Types tab in the Explorer that is launched. The following diagram highlights this navigation.
Default properties
Once you have the REST Web Service type defined, you must specify the details of your web service by initializing a couple of parameters.
URL : This property defines the location of the web service. You will need to consult the API documentation for the respective web service to find the URL for your respective web service type.
For example, Twitter’s public timeline is
http://twitter.com/statuses/public_timeline.xml
proxied : This property defines whether or not your web service calls will be routed through the Sharendipity server. You are required to turn this on for any web service that hasn’t granted domain access for your application. This is part of Flash’s security model, and you’ll find that some services like Flickr’s allow access, but others like Twitter do not.
result : This property is not intended to be initialized. When the web service is called, the data returned from the call will be placed in this property.
Custom properties
Many web services have optional and required parameters. For instance, the Posterous API lets you specify the subdomain of the site you are reading from. In order to define parameters, you must define custom properties in your type that are of type REST Input.
Each REST Input has a name and value pair. The name value should be the parameter name as defined by the web service API you are trying to access. In the Posterous example, the name field is ‘hostname’. The value field carries your parameter value. When the web service is invoked, each REST Input is automatically appended to the end of the URL property.
If you were to look at the full URL when the service is invoked, you’d see something like the following:
As you are making calls to the web service, you can simply manipulate the value parameter of the REST Input to change your query to the respective web service.
Web service actions
By default, there is a single method defined for all web service types called invoke web service. This is the mechanism by which you initiate a call to your web service. It can be called internally as well as externally by users of your component.
Just like other objects in the system, you can create your own behaviors, methods, and properties for your new type. For instance, you may want to create your own invoke method which takes parameters that are passed to the web service. For example, passing in a user ID to a Facebook web service to specify the user whose profile you would like to access.
Referencing the service
Once you’ve defined your web service type, you can share it with the rest of the community with the Save option found in the object builder. Otherwise, the web service will be saved along with the application you’ve defined it in.
Whether or not you share it, the new type can now be accessed from anywhere else in the application.
If you drag it into your World and drop it, you will be prompted to create a new custom property for the World.
You can drag it into a behavior or method and drop it to create a local variable inside your logic.
It will also be accessible from the Other tab when you define new properties anywhere within your application.
Just remember that if you don’t save the service by itself, you can only access is from the Local tab of the Explorer within the current application. If you do save it, it will appear in the Types tab for everyone.
Tips for better reuse
Creating an abstraction of the web services call is a nice feature by itself. However, it becomes most useful when you provide really easy access to the data that is returned. For instance, rather than simply providing the invoke web service call and letting the callers parse the data returned, try creating methods that do that parsing for them.
The Twitter service has a method that returns an array of Twitter Status types.
The Posterous service has a method that returns an array of Posterous Blog Post types.
The Facebook User service has a method that returns an array of Facebook User types.
Each of these services define new methods that call the invoke web service method internally and parse the return data automatically. This way, users of the services don’t have to worry about how any of the data is formatted.
For more details, including specifics for the Facebook integration, check out the web services page on the wiki.
Posted in Games, Tutorial by Dale Beermann
July 22, 2009
Ever since we released the new Flash version of the editor I’ve been amazed at how much faster I can create content in Sharendipity, even when creating it from scratch. To show you all just how easy it is to create the core functionality of a space shooter, we put together a little video. You can always search for content shared out by the community but if you’re in the mood to try things out yourself, check out the video and then head over to the editor to play around.