Skip to content

tantock/GoQuestrade

Repository files navigation

GoQuestrade

An http client wrapper to make authenticated REST API calls to Questrade.

GoQuestrade follows the Authorization Code flow detailed in https://www.questrade.com/api/documentation/authorization.

Getting Started

Install this package by running

go get github.com/tantock/GoQuestrade

Authorization

Register a personal app at https://apphub.questrade.com/UI/UserApps.aspx

Create an authorization flow struct with the Consumer key of the above registered app as your client-id and Callback URL as the redirect-url.

Create a new QuestradeClient and call Authorize passing in the AuthorizationFlow created above.

authCodeStr := "oauthCodeStr"
auth = GoQuestrade.NewCodeAuthorizationFlow(authCodeStr, "my-client-id", "redirect-url", http.DefaultClient)

q := GoQuestrade.NewQuestradeClient()
err := q.Authorize(auth)

Example Usage

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/tantock/GoQuestrade"
	"github.com/tantock/GoQuestrade/dto"
)

func main() {
	var auth GoQuestrade.AuthorizationFlow

	authCodeStr := "oauthCodeStr"
	auth = GoQuestrade.NewCodeAuthorizationFlow(authCodeStr, "my-client-id", "redirect-url", http.DefaultClient)

	q := GoQuestrade.NewQuestradeClient()
	q.Authorize(auth)
	apiRequestBuilder := q.NewApiRequestBuilder()
	apiRequestBuilder.SetMethod(http.MethodGet)
	apiRequestBuilder.SetPath("/v1/markets/candles/48051109")
	apiRequestBuilder.AddQuery("startTime", time.Now().Add(-72*time.Hour).Format(time.RFC3339))
	apiRequestBuilder.AddQuery("endTime", time.Now().Format(time.RFC3339))
	apiRequestBuilder.AddQuery("interval", "OneMinute")

	candlestickResp := dto.CandlesticksResp{}

	_, err := apiRequestBuilder.RequestAndDecode(&candlestickResp)
	if err != nil {
		log.Println("failed to send http request:", err)
	}

	fmt.Printf("Got candlesticks:\n %+v\n", candlestickResp)
}

Error Handling

_, err := apiRequestBuilder.RequestAndDecode(&candlestickResp)

	//Error handling
	if err != nil {
		log.Println("failed to send http request:", err)
		if err.InternalError() != nil {
			log.Printf("Internal error: %v\n", err.InternalError())
		}
		if err.QuestradeError() != nil {
			log.Printf("Questrade Error: %v\n", err.QuestradeError().Error())
			log.Printf("Questrade Http Status Code: %v\n", err.QuestradeError().HttpStatusCode())
			log.Printf("Questrade Error Code: %v\n", err.QuestradeError().QuestradeErrCode())
		}
	}

About

A Questrade Client written in Golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages