Questrade API sample code with nodejs and request

Questrade released an API to retrieve account information, market data, and even making trades. That’s awesome! They have helpful documentation and even a developer console to make the API calls. Here is some sample code to use their API. Remember to paste your own refresh token (their Getting Started page provides instructions on how to generate it)

var request = require('request');

var refresh_token='YOUR TOKEN HERE';

// This gets us the access token for authorizing subsequent requests
console.log("getting access token");
request.get(
	{url:'https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=' + refresh_token},
	function(err, response, body){
		if(err){
			console.log(err);
			return;
		}

		console.log(response.statusCode);

		if(response.statusCode != 200){
			console.log(body); // If we get here, it is likely to be a 'bad request' because we are using the same refresh_token more than once
			return;
		}

		b = JSON.parse(body)
		var access_token = b.access_token;
		var api_server = b.api_server;

		// Let's try to get our account information
		if(response.statusCode == 200)
		{
			console.log('Make a request');
			request.get(
				{
					url: api_server + 'v1/accounts',
					auth:{
						bearer : access_token
					}
				},
				function(err, response, body){
					if(err){
						console.log(err);
						return;
					}
					console.log(response.statusCode);
					console.log(body);
				}
			);
		}
	}
);

This code redeems the refresh token for an access token, and then use the access token to make a get request to v1/accounts to get account information. You should be able to reuse the access token for other requests; however you can only use the refresh token once. If you use it more than once, you will get a 400 bad request.

Advertisements

Mesos framework example in Python

The example I am presenting here is built on this blog post by James J Porter. James’ example runs a shell command to echo Hello World. Now, let’s say we want to do some computation and the framework needs to tell the slaves to get the binaries/data from somewhere. How do we do that?

The answer lies in the mesos.proto file:

/**
 * Describes a command, executed via: '/bin/sh -c value'. Any URIs specified
 * are fetched before executing the command.  If the executable field for an
 * uri is set, executable file permission is set on the downloaded file.
 * Otherwise, if the downloaded file has a recognized archive extension
 * (currently [compressed] tar and zip) it is extracted into the executor's
 * working directory. This extraction can be disabled by setting `extract` to
 * false. In addition, any environment variables are set before executing
 * the command (so they can be used to "parameterize" your command).
 */
message CommandInfo {
  message URI {
    required string value = 1;
    optional bool executable = 2;
    optional bool extract = 3 [default = true];
  }

Mesos take cares of downloading and extracting the files; all we need is state where to get it from. Therefore compose the task message like this (in Python):


task = new_task(offer)
uri = task.command.uris.add()
uri.value = "path-to-file"
task.command.value = "command to-run-something"

What exactly do we put as the uri? Looking at the Mesos code here, turns out the URI can point to a local file, HDFS, http, ftp, etc.

Base on these new found knowledge, I put bells and whistles on the hello world framework (actually I took the hello world part away). The example framework here is going to tell the slaves to talk to a web service to get a Python script, use the script to sum up a few numbers, then send the results to the web service. The web service is going to print out the results. When it is done, the framework will stop.

Continue reading