Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial library code without tests.

  • Loading branch information...
commit ce0ad0b32940676026dcab499ebce041d82aa152 1 parent 46b8789
@kushkella kushkella authored
Showing with 155 additions and 0 deletions.
  1. +12 −0 .project
  2. +79 −0 client.js
  3. +58 −0 main.js
  4. +6 −0 package.json
View
12 .project
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>pagify-client-node</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ </buildSpec>
+ <natures>
+ <nature>com.aptana.projects.webnature</nature>
+ </natures>
+</projectDescription>
View
79 client.js
@@ -0,0 +1,79 @@
+var crypto = require('crypto');
+var http = require("http");
+
+pad = function(n) {
+ if (n < 10) {
+ return "0" + n;
+ } else {
+ return n;
+ }
+};
+
+timestamp = function() {
+ var day, dow, hours, minutes, month, now, seconds, year;
+ now = new Date();
+ year = now.getUTCFullYear();
+ month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now.getUTCMonth()];
+ dow = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][now.getUTCDay()];
+ day = pad(now.getUTCDate());
+ hours = pad(now.getUTCHours());
+ minutes = pad(now.getUTCMinutes());
+ seconds = pad(now.getUTCSeconds());
+ return "" + dow + ", " + day + " " + month + " " + year + " " + hours + ":" + minutes + ":" + seconds + " GMT";
+};
+
+sign_request = function(http_options, key, secret) {
+ var canonical_string, hmac_signature, hmac_signature_base64, _base, _ref, _ref2;
+ if (( _ref = http_options['headers']) == null) {
+ http_options['headers'] = {};
+ }
+ if (( _ref2 = (_base = http_options['headers'])['DATE']) == null) {
+ _base['DATE'] = timestamp();
+ }
+ canonical_string = (http_options['method'] || 'GET');
+ canonical_string += (http_options['headers']['Content-Type'] || '');
+ canonical_string += (http_options['headers']['CONTENT_MD5'] || '');
+ canonical_string += (http_options['headers']['Content-Length'] || '0');
+ canonical_string += http_options['headers']['DATE'];
+ canonical_string += (http_options['path'] || '');
+ hmac_signature = crypto.createHmac("sha1", secret);
+ hmac_signature.update(canonical_string);
+ hmac_signature_base64 = hmac_signature.digest("base64");
+ return http_options['headers']['AUTHENTICATION'] = key + ":" + hmac_signature_base64;
+};
+
+exports.request = function(options, data, appId, appSecret, callback) {
+ var requestBody = JSON.stringify(data);
+
+ var http_options = {
+ "hostname" : options.hostName,
+ "port" : options.port,
+ "method" : options.method,
+ "path" : options.path,
+ "headers" : {
+ "CONTENT_MD5" : '',
+ "Content-Type" : "application/json",
+ "Content-Length" : requestBody.length,
+ "Accept" : options.acceptType
+ }
+ };
+
+ sign_request(http_options, appId, appSecret);
+
+ var req = http.request(http_options, function(res) {
+ if (options.acceptType == "application/pdf")
+ res.setEncoding('binary');
+
+ var body = '';
+ res.on('data', function(chunk) {
+ body += chunk;
+ });
+
+ res.on('end', function() {
+ callback(body);
+ });
+ });
+
+ req.write(data);
+ req.end();
+}
View
58 main.js
@@ -0,0 +1,58 @@
+var client = require("./client");
+var fs = require("fs");
+
+
+var options = {
+ "hostName": "http://127.0.0.1",
+ "port": 3000,
+ "method": "",
+ "method": "POST",
+ "acceptType": ""
+}
+var appId = "";
+var appSecret = "";
+
+exports.init = function(id, secret) {
+ appId = id;
+ appSecret = secret;
+}
+
+exports.generatePDF = function (templateID, data, callback) {
+ var requestData = {
+ "template_id" : templateID,
+ "data": data
+ };
+ options.path = "/api/generate_pdf";
+ options.acceptType = "application/pdf";
+ client.request(options, requestData, appId, appSecret, callback);
+}
+
+exports.listTemplates = function (callback) {
+ options.path = "/api/list_templates";
+ options.acceptType = "application/json";
+ client.request(options, {}, appId, appSecret, callback);
+}
+
+exports.newTemplate = function (callback) {
+ options.path = "/api/new_template";
+ options.acceptType = "application/json";
+ client.request(options, {}, appId, appSecret, callback);
+}
+
+exports.editTemplate = function (templateID, callback) {
+ var requestData = {
+ "template_id" : templateID
+ };
+ options.path = "/api/edit_template";
+ options.acceptType = "application/json";
+ client.request(options, requestData, appId, appSecret, callback);
+}
+
+exports.deleteTemplate = function (templateID, callback) {
+ var requestData = {
+ "template_id" : templateID
+ };
+ options.path = "/api/delete_template";
+ options.acceptType = "application/json";
+ client.request(options, requestData, appId, appSecret, callback);
+}
View
6 package.json
@@ -0,0 +1,6 @@
+{
+ "name": "pagify-client-node",
+ "version": "0.1.0",
+ "git": "https://github.com/pagify/pagify-client-node",
+ "main": "./main"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.