Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Allow custom port and path to be set as option #168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions awssum.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ function stringifyQuery(params) {
// function protocol() -> string (the default protocol for the HTTP request, https or http)
// function method() -> string (the default method for the HTTP request)
// function host() -> string (the host for this service/region)
// function port() -> number (the port for this service/region)
// function path() -> string (the default path for this service)
// function addExtras() -> side effect, adds extra whatever
// function addCommonOptions(options, args) -> side effect, adds the common headers/params for this service
Expand All @@ -305,6 +306,16 @@ var AwsSum = function(opts) {
self._protocol = opts.protocol;
}

// if we have been given a port, use that one
if ( opts.port ) {
self._port = opts.port;
}

// if we have been given a path, use that one
if ( opts.path ) {
self._path = opts.path;
}

return self;
};

Expand All @@ -319,13 +330,28 @@ AwsSum.prototype.protocol = function() {
return 'https';
};

AwsSum.prototype.port = function() {
var self = this;
if ( this._port ) {
return this._port;
}
if ( self.protocol() === 'http' ) {
return 80;
} else {
return 443;
}
};

AwsSum.prototype.method = function() {
return 'GET';
};

// AwsSum.prototype.host // no default

AwsSum.prototype.path = function() {
if ( this._path ) {
return this._path;
}
return '/';
};

Expand Down Expand Up @@ -503,6 +529,23 @@ AwsSum.prototype.send = function(operation, args, opts, callback) {

// ---

// build the port
options.port = self.port();
if ( operation.port) {
if ( typeof operation.port === 'function' ) {
options.port = operation.port.apply(self, [ options, args ]);
}
else if ( typeof operation.port === 'number' ) {
options.port = operation.port;
}
else {
// since this is a program error, we're gonna throw this one
throw 'Unknown operation.port : ' + typeof operation.port;
}
}

// ---

// build the path
options.path = self.path();
if ( operation.path ) {
Expand Down Expand Up @@ -721,6 +764,7 @@ AwsSum.prototype.send = function(operation, args, opts, callback) {
console.log('- method : ', options.method);
console.log('- protocol : ', options.protocol);
console.log('- host : ', options.host);
console.log('- port : ', options.port);
console.log('- path : ', options.path);
console.log('- params : ', options.params);
console.log('- headers : ', options.headers);
Expand Down Expand Up @@ -779,6 +823,7 @@ AwsSum.prototype.send = function(operation, args, opts, callback) {
return;
}
}

// save the whole result in here
var result = {};

Expand Down Expand Up @@ -939,6 +984,7 @@ AwsSum.prototype.send = function(operation, args, opts, callback) {
//
// * options.method
// * options.host
// * options.port
// * options.path
// * options.params
// * options.headers
Expand All @@ -956,6 +1002,7 @@ AwsSum.prototype.request = function(options, callback) {
var reqOptions = {
method : options.method,
host : options.host,
port : options.port,
path : options.path,
headers : options.headers
};
Expand Down
13 changes: 13 additions & 0 deletions test/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ test(function(t) {
var awssum = new AwsSum({});
var http = new AwsSum({
protocol : 'http',
path : '/somepath',
port : 8080
});
var https = new AwsSum({
protocol : 'https',
path : '/somepath',
port : 8443
});

t.equal('https', awssum.protocol(), 'Default is https');
t.equal('http', http.protocol(), 'http works fine');
t.equal('https', https.protocol(), 'https works fine');

t.equal(443, awssum.port(), 'Default is 443');
t.equal(8080, http.port(), 'http port works fine');
t.equal(8443, https.port(), 'https port works fine');

t.equal('/', awssum.path(), 'Default is "/"');
t.equal('/somepath', http.path(), 'http path works fine');
t.equal('/somepath', https.path(), 'https path works fine');

t.end();
});

Expand Down