-
Notifications
You must be signed in to change notification settings - Fork 1
/
calcbench.go
143 lines (120 loc) · 3.62 KB
/
calcbench.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package calcbench
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
//APIURL the base API URL for CalcBench API
const APIURL = "https://www.calcbench.com"
//CompaniesParameters specifies the companies to include
type CompaniesParameters struct {
CompanyIdentifiers []string `json:"companyIdentifiers"`
EntireUniverse bool `json:"entireUniverse,omitempty"`
}
//PeriodParameters specifies the periods to include
type PeriodParameters struct {
Year int32 `json:"year"`
Period int8 `json:"period"`
EndYear int `json:"endYear,omitempty"`
EndPeriod int `json:"endPeriod"`
AllHistory bool `json:"allHistory,omitempty"`
UpdateDate string `json:"updateDate,omitempty"`
}
//StandardizedDataPageParameters specifies the metrics to search for
type StandardizedDataPageParameters struct {
Metrics []string `json:"metrics"`
}
// StandardizedDataRequest for comparisons between companies
type StandardizedDataRequest struct {
CompaniesParameters CompaniesParameters `json:"companiesParameters"`
PeriodParameters PeriodParameters `json:"periodParameters"`
PageParameters StandardizedDataPageParameters `json:"pageParameters"`
}
// StandardizedDataResponseObject result object
type StandardizedDataResponseObject struct {
Ticker string `json:"ticker"`
CalendarYear int `json:"calendar_year"`
CalendarPeriod int `json:"calendar_period"`
Metric string `json:"metric"`
Value interface{} `json:"value"`
}
// StandardizedDataResponse is a slice of StandardizedDataResponseObject
type StandardizedDataResponse []StandardizedDataResponseObject
//CalcBench is the CalcBench API
type CalcBench struct {
Client *http.Client
}
// New creates a new API instance
func New() (*CalcBench, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
return &CalcBench{
Client: &http.Client{
Jar: cookieJar,
},
}, nil
}
// Login authenticates the API using https://www.calcbench.com/api#authentication
func (c CalcBench) Login(email, password string) (bool, error) {
data := url.Values{}
data.Set("email", email)
data.Set("password", password)
req, err := http.NewRequest("POST",
APIURL+"/account/LogOnAjax",
strings.NewReader(data.Encode()))
if err != nil {
return false, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.Client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return false, fmt.Errorf("invalid status code (%d)", resp.StatusCode)
}
buffer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
var result bool
if err := json.Unmarshal(buffer, &result); err != nil {
return false, err
}
return result, nil
}
//StandardizedData calls https://www.calcbench.com/api#normalizedData
func (c CalcBench) StandardizedData(Query StandardizedDataRequest) (StandardizedDataResponse, error) {
b, err := json.Marshal(Query)
if err != nil {
return nil, err
}
fmt.Printf("Query: %s\n", string(b))
req, err := http.NewRequest("POST", APIURL+"/api/mappedData", bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error: %s", string(contents))
}
var result StandardizedDataResponse
if err := json.Unmarshal(contents, &result); err != nil {
return nil, err
}
return result, nil
}