2019-11-15 16:16:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-15 16:32:00 +00:00
|
|
|
"encoding/xml"
|
2019-11-15 16:16:56 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/mrjones/oauth"
|
2019-11-15 16:32:00 +00:00
|
|
|
"io/ioutil"
|
2019-11-26 20:35:50 +00:00
|
|
|
"log"
|
2019-11-15 16:16:56 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* The trading endpoint for Ally */
|
|
|
|
const endpoint string = "https://api.tradeking.com/v1/"
|
|
|
|
|
|
|
|
type AllyApi struct {
|
|
|
|
consumer *oauth.Consumer
|
|
|
|
Client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AllyApi) Initialize() {
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up our new oauth consumer
|
|
|
|
c.consumer = oauth.NewConsumer(
|
|
|
|
os.Getenv("CONSUMER_KEY"),
|
|
|
|
os.Getenv("CONSUMER_SECRET"),
|
|
|
|
oauth.ServiceProvider{
|
|
|
|
RequestTokenUrl: "https://developers.tradeking.com/oauth/request_token",
|
|
|
|
AuthorizeTokenUrl: "https://developers.tradeking.com/oauth/authorize",
|
|
|
|
AccessTokenUrl: "https://developers.tradeking.com/oauth/access_token",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
c.Client, err = c.consumer.MakeHttpClient(
|
|
|
|
&oauth.AccessToken{Token: os.Getenv("ACCESS_TOKEN"), Secret: os.Getenv("ACCESS_SECRET")})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:32:00 +00:00
|
|
|
func (c *AllyApi) get(path string) (resp *http.Response, err error) {
|
2019-11-15 16:16:56 +00:00
|
|
|
resp, err = c.Client.Get(fmt.Sprintf("%s\\%s", endpoint, path))
|
|
|
|
return
|
|
|
|
}
|
2019-11-15 16:32:00 +00:00
|
|
|
|
|
|
|
/* The /accounts endpoint of Ally */
|
2019-11-15 18:01:19 +00:00
|
|
|
func (c *AllyApi) Accounts() []AccountSummary {
|
2019-11-15 16:32:00 +00:00
|
|
|
b, err := c.get("accounts")
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
defer b.Body.Close()
|
|
|
|
raw, err := ioutil.ReadAll(b.Body)
|
2019-11-15 18:01:19 +00:00
|
|
|
fmt.Printf("%s\n", raw)
|
2019-11-15 16:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
2019-11-15 18:01:19 +00:00
|
|
|
var resp AccountResponse
|
2019-11-15 16:32:00 +00:00
|
|
|
err = xml.Unmarshal(raw, &resp)
|
|
|
|
if err != nil {
|
2019-11-20 01:26:56 +00:00
|
|
|
panic(err)
|
2019-11-15 16:32:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 18:01:19 +00:00
|
|
|
return resp.Accounts.Accountsummary
|
2019-11-15 16:32:00 +00:00
|
|
|
}
|
2019-11-26 20:35:50 +00:00
|
|
|
|
|
|
|
func (c *AllyApi) AccountBalances() (balances []AccountBalance) {
|
|
|
|
b, err := c.get("accounts/balances")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not get account balances")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer b.Body.Close()
|
|
|
|
|
|
|
|
var resp AccountBalanceResponse
|
|
|
|
|
|
|
|
}
|