Remote Sites + load balancing = a Force.com gotcha?

My favorite aspect of developing on the Force.com platform is how quickly you can get something up and running. Recently, I’ve been chewing on an idea for how to better the integrate the photos I share on Twitter into my personal blog on Tumblr, though it would require a little bit of coding. I had a little downtime today and got almost all the way there.

Background

As I mentioned, I keep my personal blog on Tumblr. It’s a neat tool, and I’ve always liked the way that it can pull in content from other sources (usually via RSS) and aggregate everything you’re sharing online. I’m active on Twitter, so naturally I configured my tumblog to read in the RSS feed of my tweets. This works great for short text blurbs, which is the heart and soul of Twitter.

A picture is worth a thousand words, of course, and being an amateur photographer I often post quick snapshots on Twitter via yFrog, one of several services that extends Twitter to provide photo sharing. These tweets will include a shortened URL to the photo, which makes perfect sense in your Twitter feed but leaves room for improvement when that same tweet is copied onto a blog. Tumblr includes a native post type for photos, so my idea is to have a piece of code sweep through my Tumblr posts looking for links to “yfrog.com” and replace those posts with full-fledged photo posts.

As luck would have it, both Tumblr and yFrog offer REST APIs that allow this type of thing to be straightforward. The flow of the code is basically as follows:

  1. Read in recent Tumblr text posts (via the Tumblr API), and keep a list of those that include links to yFrog
  2. Run through that list and use the yFrog API to determine the URL to the full size image for each
  3. Create a Tumblr photo post for each that links to the URL from step 2 (rather than uploading an actual image)
  4. Delete the original Tumblr text posts that would then be a duplicate

Unfortunately, I ran into a problem at step 2.

The Issue

Force.com requires any sites that your code will access via HTTP to be specifically whitelisted under Setup → Security Controls → Remote Site Settings. In the case of Tumblr, blog entries are created by sending an HTTP POST with various parameters to the following URL:

http://www.tumblr.com/api/write

The screenshot below shows the Remote Site configuration used for that example:

Remote Site configuration
Remote Site configuration for Tumblr

Accessing the image metadata via the yFrog API is conceptually similar, except the HTTP GET request is redirected to a different URL (presumably for load balancing purposes). Here’s a screenshot from HTTP Client that shows the actual response:

Image API call redirected
HTTP response being redirected

The Location header provides the URL that now accesses this resource. I changed my Apex code to account for a possible redirect, but then I ran into a problem with the Remote Site:

Remote Site error with dynamic subdomain
Remote Site error with dynamic subdomain

The part of the URL underlined in red is the part that would generally need to be configured as the Remote Site URL. However, this isn’t practical, since each time the initial GET is performed, the redirected URL has a different subdomain. Again, this is understandable as a load balancing mechanism, but unfortunately puts the brakes on my mashup.

Possible solutions

I can appreciate that the Remote Site provides a layer of security, though perhaps a compromise would be to allow wildcards in this configuration, as in:

http://*.imageshack.us

Alternatively, the platform’s native HTTP mechanism could automatically follow those redirects, similar to how this is handled in Google App Engine. That would eliminate the need for the Apex code to handle redirects.

As a workaround, I’ve started sharing images with a service called Twitgoo (instead of yFrog). They have an API as well, and calling the methods does not result in a redirect.

GET http://twitgoo.com/api/message/info/3spr5

Here’s an example response:

<?xml version="1.0" encoding="UTF-8"?> 
<rsp status="ok"> 
   <created_at>2009-11-09T09:29:41-07:00</created_at> 
   <username>twitgoo_test</username> 
   <avatar>http://a3.twimg.com/profile_images/107548761/bad_rooster_normal.jpg</avatar>
   <statusid>4417243089</statusid> 
   <mediaid>3spr5</mediaid> 
   <mediaurl>http://twitgoo.com/3spr5</mediaurl> 
   <text>Awesome Denver city lights</text> 
   <imageurl>http://i35.tinypic.com/17tzde.jpg</imageurl> 
   <thumburl>http://i35.tinypic.com/17tzde_th.jpg</thumburl> 
   <viewcount>31</viewcount></rsp> 
</rsp>

Crisis averted. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s