Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Basic api requests and api token request done, including test

  • Loading branch information...
commit b06717f7f825ac26a441427fbf87038df63f5095 0 parents
@andzdroid authored
3  .gitignore
@@ -0,0 +1,3 @@
+*.swo
+*.swp
+tests/config.js
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/posterous');
87 lib/posterous.js
@@ -0,0 +1,87 @@
+var http = require('http');
+var qs = require('querystring');
+
+var HOST = 'posterous.com';
+var PATH = '/api/2/';
+
+
+var Posterous = function(user, pass) {
+ this._auth = 'Basic ' + new Buffer(user + ':' + pass).toString('base64');
+ this._token = null;
+};
+
+
+Posterous.prototype.request = function(method, api, args, callback) {
+ args = args || {};
+
+ //Add the api token
+ args.api_token = this._token;
+
+ //Set up request options
+ var path = PATH + api;
+ var body = null;
+ var headers = {
+ Authorization: this._auth
+ };
+
+ if (method != 'GET') {
+ body = qs.stringify(args);
+ headers['Content-Length'] = body.length;
+ } else {
+ path = path + '?' + qs.stringify(args);
+ }
+
+ var reqOptions = {
+ host: HOST,
+ port: 80,
+ method: method,
+ path: path,
+ headers: headers
+ };
+
+ //Make request to Posterous API
+ var req = http.request(reqOptions, function apiRequest(res) {
+ //Gather response
+ var response = '';
+ res.on('data', function apiResponse(d) {
+ response += d.toString();
+ });
+
+ res.on('end', function endResponse(){
+ var r = JSON.parse(response);
+
+ if (r.error) {
+ return callback(r.message || r.error);
+ }
+
+ callback(null, r);
+ });
+ });
+
+ if (body) {
+ req.write(body);
+ }
+
+ req.on('error', function reqError(e) {
+ return callback('Problem with request: ' + e);
+ });
+
+ req.end();
+};
+
+
+//Get api token
+Posterous.prototype.auth = function getAuthKey(callback) {
+ this.request('GET', 'auth/token', null, function(err, result) {
+ if (err) {
+ return callback(err);
+ }
+
+ //Save api token
+ this._token = result.api_token;
+
+ callback(null, result.api_token);
+ });
+};
+
+module.exports = Posterous;
29 package.json
@@ -0,0 +1,29 @@
+{
+ "name": "posterous",
+ "version": "0.0.1",
+ "description": "Package for the Posterous API",
+ "keywords": [
+ "posterous",
+ "api",
+ ],
+ "contributors": [
+ {
+ "name": "Chun-hao Hu",
+ "email": "hu.chunhao@gmail.com",
+ "web": "http://blog.huchunhao.com"
+ }
+ ],
+ "bugs": {
+ "web": "https://github.com/andzdroid/node-posterous/issues"
+ },
+ "repositories": [
+ {
+ "type": "git",
+ "url": "git@github.com:andzdroid/node-posterous.git"
+ }
+ ],
+ "dependencies": {
+ },
+ "main": "index",
+ "engines": ["node"]
+}
33 tests/auth.js
@@ -0,0 +1,33 @@
+var Posterous = require('../index');
+var config = require('./config');
+
+module.exports['API Token'] = function(test) {
+ //To test, add your posterous email/password
+ var posterous = new Posterous(config.user, config.pass);
+
+ posterous.auth(function(err, result) {
+ //Don't expect an error
+ test.ok(!err, 'An error occurred: ' + err);
+
+ //Expect a result
+ test.ok(result, 'No api token was returned.');
+ console.log('API token: ' + result);
+
+ test.done();
+ });
+};
+
+module.exports['Bad Auth'] = function(test) {
+ //Make a bad api token request with no username/password
+ var posterous = new Posterous('', '');
+
+ posterous.auth(function(err, result) {
+ //Expect an error
+ test.ok(err);
+
+ //Don't expect a result
+ test.ok(!result);
+
+ test.done();
+ });
+};
7 tests/config.sample.js
@@ -0,0 +1,7 @@
+//To run the tests, rename this file to config.js
+//and fill in your posterous account details
+
+module.exports = {
+ user: 'your email',
+ pass: 'your password'
+};
Please sign in to comment.
Something went wrong with that request. Please try again.