Last active
August 29, 2015 14:04
-
-
Save jason-s13r/542f067143918c237834 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('BitNZ', []).factory('Api', ['$http', '$log', function ($http, $log) { | |
'use strict'; | |
var api = {}; | |
var host = 'https://bitnz.com/api/0'; | |
var config = { | |
username: '', | |
key: '', | |
secret: '' | |
}; | |
var sign = function(parameters){ | |
parameters = parameters || {}; | |
var d = new Date(); | |
parameters.nonce = d.getTime() + ''+ d.getMilliseconds(); | |
parameters.key = config.key; | |
parameters.signature = CryptoJS.algo.HMAC | |
.create(CryptoJS.algo.SHA256, config.secret) | |
.update(parameters.nonce + config.username + config.key) | |
.finalize() | |
.toString(CryptoJS.enc.Hex) | |
.toUpperCase(); | |
return parameters; | |
}; | |
var serialize = function(parameters) { | |
var params = []; | |
Object.keys(parameters).forEach(function(key){ | |
this.push(key + '=' + parameters[key]); | |
}, params); | |
return params.join('&'); | |
}; | |
var request = function(method, action, parameters){ | |
var config = { | |
method: method, | |
url: host + action, | |
data: serialize(parameters), | |
headers: {} | |
}; | |
if (method === 'GET') { | |
config.params = serialize(parameters); | |
} | |
if (method === 'POST') { | |
config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; | |
} | |
return $http(config); | |
}; | |
var get = function(action, parameters){ | |
return request('GET', action, parameters); | |
}; | |
var post = function(action, parameters){ | |
return request('POST', '/private' + action, sign(parameters)); | |
}; | |
// Save keys: | |
api.authorize = function(username, key, secret){ | |
if (username.key !== undefined) { | |
key = username.key; | |
secret = username.secret; | |
username = username.username; | |
} | |
config = { | |
username: username, | |
key: key, | |
secret: secret | |
}; | |
}; | |
api.sign = sign; | |
// Open/Public API Calls: | |
api.ticker = function(){ | |
return get('/ticker', {}); | |
}; | |
api.trades = function(lastTrade, fromDate) { | |
return get('/trades', { | |
'since': lastTrade || 0, | |
'since_date': fromDate || 0 | |
}); | |
}; | |
api.chart = function(fromDate, width, height, background) { | |
return host + '/trades_chart?' + serialize({ | |
'since_date': fromDate || 0, | |
'width': width || 600, | |
'height': height || 400, | |
'bgcolor': background || '' | |
}); | |
}; | |
api.orderbook = function() { | |
return get('/orderbook', {}); | |
}; | |
// Private API Calls: | |
api.balance = function() { | |
return post('/balance', {}); | |
}; | |
api.orders_buy_open = function() { | |
return post('/orders/buy/open', {}); | |
}; | |
api.orders_sell_open = function() { | |
return post('/orders/sell/open', {}); | |
}; | |
api.orders_buy_closed = function(offset, limit) { | |
return post('/orders/buy/closed', { | |
offset: offset || 0, | |
limit: limit || 0 | |
}); | |
}; | |
api.orders_sell_closed = function(offset, limit) { | |
return post('/orders/sell/closed', { | |
offset: offset || 0, | |
limit: limit || 0 | |
}); | |
}; | |
api.orders_buy_cancel = function(id) { | |
return post('/orders/buy/cancel', { | |
id: id | |
}); | |
}; | |
api.orders_sell_cancel = function(id) { | |
return post('/orders/sell/cancel', { | |
id: id | |
}); | |
}; | |
api.orders_buy_create = function(amount, price) { | |
return post('/orders/buy/create', { | |
amount: amount, | |
price: price | |
}); | |
}; | |
api.orders_sell_create = function(amount, price) { | |
return post('/orders/sell/create', { | |
amount: amount, | |
price: price | |
}); | |
}; | |
api.btc_deposit_address = function() { | |
return post('/btc/address', {}); | |
}; | |
api.btc_withdraw = function(address, amount) { | |
return post('/btc/withdraw', { | |
address: address, | |
amount: amount | |
}); | |
}; | |
return api; | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha256.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js"></script> | |
<script src="BitNZService.js"></script> | |
<script src="PlayApp.js"></script> | |
</head> | |
<body ng-app="Play"> | |
<div ng-controller="Example"> | |
<div> | |
<input ng-model="config.username" placeholder="Username" /> | |
<input ng-model="config.key" placeholder="Key" /> | |
<input ng-model="config.secret" placeholder="Secret" /> | |
<button ng-click="Authorize()">Authorize</button> | |
</div> | |
<button ng-click="GetTicker()">Ticker</button> | |
<button ng-click="OrderBook()">OrderBook</button> | |
<button ng-click="Balance()">Balance</button> | |
<pre style="margin-top: 50px; border: solid 1px; background: #f1f1f1; padding: 10px;"> | |
Config: {{ config }} | |
</pre> | |
<img ng-src="{{ GetChart() }}" alt="trade chart" /> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('Play', ['BitNZ']); | |
app.controller('Example', ['$scope', '$log', 'Api', function($scope, $log, bitnz){ | |
$log.log('bitNZ', bitnz); | |
$scope.config = { | |
username: '', | |
key: '', | |
secret: '' | |
}; | |
$scope.results = []; | |
$scope.Authorize = function(){ | |
bitnz.authorize($scope.config); | |
}; | |
$scope.GetTicker = function(){ | |
if (!bitnz) { | |
$log.log('Uh oh!'); | |
return; | |
} | |
bitnz.ticker().success(function(data, status){ | |
$log.log('ticker', data, status); | |
}); | |
}; | |
$scope.GetChart = function() { | |
$scope.imageUrl = bitnz.chart(); | |
return $scope.imageUrl; | |
}; | |
$scope.OrderBook = function(){ | |
bitnz.orderbook().success(function(data) { | |
$log.log('orderbook', data); | |
}); | |
}; | |
$scope.Balance = function(){ | |
bitnz.balance().success(function(data) { | |
$log.log('balance', data); | |
}).error(function(){ | |
$log.log('error', arguments); | |
}); | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Send POST with Content-Type: application/x-www-form-urlencoded