         .send options
         .set 'Accept', 'application/json'
         .end (res) ->
-            if res.ok
-                callback null, res.body
-            else
-                console.log "Error in activityCreate", options, res.body?.error
-                callback res.body, null
+            handleResponse res, callback, "activityCreate", options
 

        request.put "messages/batchFetch"
        .send target
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                if res.body?.name?
                    res.error = res.body
                callback res.error

    batchAddFlag: (target, flag, callback) ->
        body = _.extend {flag}, target
        request.put "messages/batchAddFlag"
        .send body
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                if res.body?.name?
                    res.error = res.body
                callback res.error

    batchRemoveFlag: (target, flag, callback) ->
        body = _.extend {flag}, target
        request.put "messages/batchRemoveFlag"
        .send body
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                if res.body?.name?
                    res.error = res.body
                callback res.error

    batchDelete: (target, callback) ->
        body = _.extend {}, target
        request.put "messages/batchTrash"
        .send target
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                if res.body?.name?
                    res.error = res.body
                callback res.error

    batchMove: (target, from, to, callback) ->
        body = _.extend {from, to}, target
        request.put "messages/batchMove"
        .send body
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                if res.body?.name?
                    res.error = res.body
                callback res.error

    createAccount: (account, callback) ->

        # TODO: validation & sanitization

        request.post 'account'
        .send account
        .set 'Accept', 'application/json'
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                console.log "Error in createAccount", account, res.body?.error
                callback res.body, null

    editAccount: (account, callback) ->


            if res.ok
                res.body.conversationLengths = {}
                res.body.conversationLengths[conversationID] = res.body.length

            handleResponse res, callback, "fetchConversation", conversationID

    fetchMessagesByFolder: (url, callback) ->
        request.get url
        .set 'Accept', 'application/json'
        .end (res) ->
            handleResponse res, callback, "fetchMessagesByFolder", url

    mailboxCreate: (mailbox, callback) ->
        request.post "mailbox"
        .send mailbox
        .set 'Accept', 'application/json'
        .end (res) ->
            handleResponse res, callback, "mailboxCreate", mailbox

    mailboxUpdate: (data, callback) ->
        request.put "mailbox/#{data.mailboxID}"
        .send data
        .set 'Accept', 'application/json'
        .end (res) ->
            handleResponse res, callback, "mailboxUpdate", data

    mailboxDelete: (data, callback) ->
        request.del "mailbox/#{data.mailboxID}"
        .set 'Accept', 'application/json'
        .end (res) ->
            handleResponse res, callback, "mailboxDelete", data

    mailboxExpunge: (data, callback) ->
        request.del "mailbox/#{data.mailboxID}/expunge"
        .set 'Accept', 'application/json'
        .end (res) ->
            handleResponse res, callback, "mailboxExpunge", data

    messageSend: (message, callback) ->
        req = request.post "message"
