Calling Trello API from Meteor
I just ported the Trello client API to a Meteor package, in order to be able to authorize my application before making actual API calls from the server side (let’s just avoid the hassle of cross-site scripting).
The following snippet shows my client side flow for creating a Trello board; first I authorize against Trello, then I call a Meteor server method to perform the corresponding Trello REST API call:
Session.set("isWaiting", true)
# MuzHack's Trello app key
Trello.setKey(Meteor.settings.public.trelloKey)
# Have the Trello client lib display a popup wherein the user authorizes MuzHack's read-write access
Trello.authorize({
type: "popup"
name: "MuzHack"
scope: { read: true, "write": true }
success: ->
logger.info("Trello authorization succeeded")
token = Trello.token()
Meteor.call('createTrelloBoard', inputValues.name, inputValues.desc, inputValues.org,
token, (error, result) ->
Session.set("isWaiting", false)
if error?
logger.warn("Server failed to create Trello board:", error)
notificationService.warn("Error",
"Server failed to create Trello board: #{error.reason}.")
else
logger.debug("Server was able to successfully create Trello board")
)
error: ->
logger.warn("Trello authorization failed")
Session.set("isWaiting", false)
})
For completeness' sake, the implementation of the server-side method:
createTrelloBoard: (name, description, organization, token) ->
user = getUser(@)
params = R.pickBy(((value, key) ->
value?
), {
name: name
desc: description
idOrganization: organization
prefs_permissionLevel: "public"
})
logger.debug("Creating Trello board, params:", params)
appKey = Meteor.settings.public.trelloKey
try
result = HTTP.post("https://api.trello.com/1/boards?key=#{appKey}&token=#{token}", {
params: params
})
catch error
logger.warn("Failed to create Meteor board with the following parameters:", params)
logger.warn("Reason for error: '#{error.message}'")
throw new Meteor.Error("trello-create", "Failed to create Trello board '#{params.name}'")
logger.debug("Created Trello board successfully, inserting into database")
data = result.data
TrelloBoards.insert({
username: user.username
name: data.name
url: data.url
description: data.desc
organization: data.idOrganization
})