Permalink
Browse files

cert and api_version

  • Loading branch information...
1 parent ad9882e commit 63fd098419bfd8a328b26adad2691f3db89160c0 @ryanfaerman committed Mar 4, 2012
Showing with 53 additions and 26 deletions.
  1. +1 −1 package.json
  2. +9 −3 zen_test.coffee
  3. +25 −10 zencoder.coffee
  4. +18 −12 zencoder.js
View
@@ -16,5 +16,5 @@
, "devDependencies": {
"coffee-script": "*"
}
- , "version": "0.0.1"
+ , "version": "0.0.2"
}
View
@@ -1,6 +1,11 @@
{Zencoder} = require './zencoder'
Zencoder::api_key = ''
+
+###
+Zencoder::api_version = 1
+###
+
###
Zencoder::Job.create
input: 's3://timelesstorah.com/010308.mp3'
@@ -109,12 +114,12 @@ Zencoder::Account.create account, (res) ->
console.log res.body
###
-###
+
Zencoder::Account.details (res) ->
console.log "GOT A RESPONSE IN MAH CALLBACK"
console.log res.code
console.log res.body
-###
+
###
Zencoder::Account.integration (res) ->
console.log "GOT A RESPONSE IN MAH CALLBACK"
@@ -128,9 +133,10 @@ Zencoder::Account.live (res) ->
console.log res.body
###
+###
Zencoder::Reports.minutes (res) ->
console.log "GOT A RESPONSE IN MAH CALLBACK"
console.log res.code
console.log res.body
-
+###
View
@@ -6,20 +6,29 @@ querystring = require 'querystring'
class exports.Zencoder
- base_url: 'https://app.zencoder.com/api/v2'
+ # These are our environment settings for the most part
+ base_url: 'https://app.zencoder.com/api'
api_key: ''
+ api_version: 2
cert: fs.readFileSync 'zencoder_ca_chain.crt'
- version: '0.0.1'
+ # Version of the library
+ version: '0.0.2'
+
default_options:
timeout: 10000
+ api_version: @::api_version
headers:
'Accept': 'application/json'
'Content-Type': 'application/json'
'User-Agent': "node-zencoder v-#{@::version}"
+
+ # Response object
@Response =
code: 0
+ # JSON of the body
body: ''
+ # Raw Body String
raw_body: ''
Job:
@@ -54,7 +63,7 @@ class exports.Zencoder
Account:
create: (params, options, cb)=>
- @post '/account', params, options
+ @post '/account', params, options, cb
details: (cb)=>
@get '/account', cb
integration: (cb) =>
@@ -67,30 +76,36 @@ class exports.Zencoder
@get '/reports/minutes', options, cb
process = (method, request_url, body = {}, options = {}, cb = () ->) =>
+ # Find the callback
cb = body if typeof body is 'function'
cb = options if typeof options is 'function'
+ # Force method name to be consistent
method = method.toUpperCase()
- request_url = url.parse @::base_url + request_url
- request_url.protocol = request_url.protocol.replace ':', ''
-
- body = _.extend api_key: @::api_key, body
payload = JSON.stringify(body) unless method is 'GET'
-
options = _.extend @::default_options, options
headers = options.headers
+ headers = _.extend('Zencoder-Api-Key': @::api_key, headers)
headers = _.extend('Content-Length': payload.length, headers) unless method is 'GET'
+
+ use_version = options.api_version || @::api_version
+ api_version = ''
+ if use_version > 1
+ api_version = "/v#{use_version}"
+
+ request_url = url.parse @::base_url + api_version + request_url
+ request_url.protocol = request_url.protocol.replace ':', ''
http_options =
host: request_url.hostname
port: request_url.port
path: request_url.path
method: method
headers: headers
- cert: @::cert
+ ca: @::cert
if method is 'GET' then http_options.path += "?#{querystring.stringify body}"
response = _.clone @Response
@@ -114,7 +129,7 @@ class exports.Zencoder
req.write "#{payload}\n"
req.end()
-
+ # Helper methods to make most of the methods clearer
@post = (request_url, body = {}, options = {}, cb = () ->) =>
process 'POST', request_url, body, options
@get = (request_url, body = {}, options = {}, cb = () ->) =>
View
@@ -12,21 +12,24 @@
querystring = require('querystring');
exports.Zencoder = (function() {
- var process;
- var _this = this;
+ var process,
+ _this = this;
function Zencoder() {}
- Zencoder.prototype.base_url = 'https://app.zencoder.com/api/v2';
+ Zencoder.prototype.base_url = 'https://app.zencoder.com/api';
Zencoder.prototype.api_key = '';
+ Zencoder.prototype.api_version = 2;
+
Zencoder.prototype.cert = fs.readFileSync('zencoder_ca_chain.crt');
- Zencoder.prototype.version = '0.0.1';
+ Zencoder.prototype.version = '0.0.2';
Zencoder.prototype.default_options = {
timeout: 10000,
+ api_version: Zencoder.prototype.api_version,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
@@ -87,7 +90,7 @@
Zencoder.prototype.Account = {
create: function(params, options, cb) {
- return Zencoder.post('/account', params, options);
+ return Zencoder.post('/account', params, options, cb);
},
details: function(cb) {
return Zencoder.get('/account', cb);
@@ -107,33 +110,36 @@
};
process = function(method, request_url, body, options, cb) {
- var headers, http_options, payload, req, response;
+ var api_version, headers, http_options, payload, req, response, use_version;
if (body == null) body = {};
if (options == null) options = {};
if (cb == null) cb = function() {};
if (typeof body === 'function') cb = body;
if (typeof options === 'function') cb = options;
method = method.toUpperCase();
- request_url = url.parse(Zencoder.prototype.base_url + request_url);
- request_url.protocol = request_url.protocol.replace(':', '');
- body = _.extend({
- api_key: Zencoder.prototype.api_key
- }, body);
if (method !== 'GET') payload = JSON.stringify(body);
options = _.extend(Zencoder.prototype.default_options, options);
headers = options.headers;
+ headers = _.extend({
+ 'Zencoder-Api-Key': Zencoder.prototype.api_key
+ }, headers);
if (method !== 'GET') {
headers = _.extend({
'Content-Length': payload.length
}, headers);
}
+ use_version = options.api_version || Zencoder.prototype.api_version;
+ api_version = '';
+ if (use_version > 1) api_version = "/v" + use_version;
+ request_url = url.parse(Zencoder.prototype.base_url + api_version + request_url);
+ request_url.protocol = request_url.protocol.replace(':', '');
http_options = {
host: request_url.hostname,
port: request_url.port,
path: request_url.path,
method: method,
headers: headers,
- cert: Zencoder.prototype.cert
+ ca: Zencoder.prototype.cert
};
if (method === 'GET') {
http_options.path += "?" + (querystring.stringify(body));

0 comments on commit 63fd098

Please sign in to comment.