Permalink
Browse files

[api test] Commit directory structure and initial test

  • Loading branch information...
1 parent 830edf2 commit 287b4043e7e63a6d42ffccde578c9b58d654c658 @indexzero committed Jun 16, 2012
View
@@ -12,4 +12,6 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
+
+test/config.json
View
@@ -0,0 +1,12 @@
+/*
+ * asana-api.js: Top-level include for the `asana-api` module.
+ *
+ * (C) 2012 Charlie Robbins
+ *
+ */
+
+var Client = require('./asana-api/client').Client;
+
+exports.createClient = function (options) {
+ return new Client(options);
+};
@@ -0,0 +1,108 @@
+/*
+ * client.js: Client for the RESTful provisioner service.
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var events = require('events'),
+ errs = require('errs'),
+ utile = require('utile'),
+ request = require('request'),
+ base64 = utile.base64;
+
+//
+// ### function Client (options)
+// #### @options {Object} Options to use for this instance.
+// Constructor function for the Client to the Nodejitsu provisioner server.
+//
+var Client = exports.Client = function (options) {
+ var self = this;
+
+ events.EventEmitter.call(this);
+
+ this.url = 'https://app.asana.com/api/1.0';
+ this.apiKey = options.apiKey;
+ this.auth = 'Basic ' + base64.encode([options.apiKey, ''].join(':'));
+
+ ['projects', 'stories', 'tasks', 'users', 'workspaces'].forEach(function (key) {
+ utile.mixin(self, require('./' + key));
+ });
+};
+
+//
+// Inherit from `events.EventEmitter`
+//
+utile.inherits(Client, events.EventEmitter);
+
+// Failure HTTP Response codes based
+// off of `/lib/conservatory/provisioner/service.js`
+Client.prototype.failCodes = {
+ 400: 'Bad Request',
+ 401: 'Not authorized',
+ 403: 'Forbidden',
+ 404: 'Item not found',
+ 500: 'Internal Server Error'
+};
+
+// Success HTTP Response codes based
+// off of `/lib/conservatory/provisioner/service.js`
+Client.prototype.successCodes = {
+ 200: 'OK',
+ 201: 'Created'
+};
+
+//
+// ### @private _request (method, uri, [body], callback, success)
+// #### @options {Object} Outgoing request options.
+// #### @callback {function} Continuation to short-circuit to if request is unsuccessful.
+// #### @success {function} Continuation to call if the request is successful
+// Core method for making requests against the haibu Drone API. Flexible with respect
+// to continuation passing given success and callback.
+//
+Client.prototype.request = function (options, callback, success) {
+ var self = this;
+
+ if (typeof options === 'string') {
+ options = { path: options };
+ }
+
+ options.method = options.method || 'GET';
+ options.uri = this.url + options.path;
+ options.headers = options.headers || {};
+ options.headers['content-type'] = options.headers['content-type'] || 'application/json';
+ options.headers['authorization'] = options.headers['authorization'] || this.auth;
+
+ console.dir(this.auth);
+ if (options.headers['content-type'] === 'application/json'
+ && options.body) {
+ options.body = JSON.stringify(options.body);
+ }
+
+ return request(options, function (err, response, body) {
+ if (err) {
+ return callback(err);
+ }
+
+ var statusCode = response.statusCode.toString(),
+ result,
+ error;
+
+ try {
+ result = JSON.parse(body);
+ }
+ catch (ex) {
+ // Ignore Errors
+ }
+
+ if (Object.keys(self.failCodes).indexOf(statusCode) !== -1) {
+ return callback(errs.create({
+ message: 'Asana Error (' + statusCode + '): ' + self.failCodes[statusCode],
+ result: result,
+ status: statusCode
+ }));
+ }
+
+ success(response, result);
+ });
+};
No changes.
No changes.
No changes.
@@ -0,0 +1,26 @@
+/*
+ * users.js: Methods for the users resource.
+ *
+ * (C) 2012 Charlie Robbins.
+ *
+ */
+
+exports.getUser = function (id, callback) {
+ if (!callback && typeof id === 'function') {
+ callback = id;
+ id = null;
+ }
+
+ id = id || '';
+ this.request('/users/' + id, callback, function (res, result) {
+ callback(null, result.data);
+ });
+};
+
+exports.me = function (callback) {
+ this.getUser('me', callback);
+};
+
+exports.listUsers = function (callback) {
+ this.getUser(callback);
+};
No changes.
View
@@ -0,0 +1,22 @@
+{
+ "name": "asana-api",
+ "description": "A nodejs client implementation for Asana API",
+ "version": "0.0.0",
+ "author": "Charlie Robbins <charlie.robbins@gmail.com>",
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/indexzero/node-asana-api.git"
+ },
+ "keywords": ["asana", "api", "project management"],
+ "dependencies": {
+ "errs": "0.2.x",
+ "request": "2.9.x",
+ "utile": "0.0.x"
+ },
+ "devDependencies": {
+ "vows": "0.6.x"
+ },
+ "main": "./lib/asana-api",
+ "scripts": { "test": "vows test/*-test.js --spec" },
+ "engines": { "node": ">= 0.6.0" }
+}
@@ -0,0 +1,3 @@
+{
+ "apiKey": "YOUR-SECRET-API-KEYZ"
+}
View
@@ -0,0 +1,23 @@
+/*
+ * users-test.js: Tests for the user resources of the Asana API.
+ *
+ * (C) 2012 Charlie Robbins
+ *
+ */
+
+var vows = require('vows'),
+ asana = require('../lib/asana-api');
+
+vows.describe('asana-api/users').addBatch({
+ "When using an instance of asana.Client": {
+ topic: asana.createClient(require('./config.json')),
+ "the me() method": {
+ topic: function (client) {
+ client.me(this.callback);
+ },
+ "should respond with the current user": function (_, user) {
+ console.dir(user);
+ }
+ }
+ }
+}).export(module);

0 comments on commit 287b404

Please sign in to comment.