Skip to content

Commit

Permalink
Fix lint and vet comments
Browse files Browse the repository at this point in the history
- Use fmt.Println() rather than a \n at the end of another fmt.Println() in examples
- Made as many methods in finance.go unpublished as possible
- Changed method signatures to return error as last parameter
- Made the proper calls in examples.
  • Loading branch information
joce committed Jun 29, 2023
1 parent 16d57a3 commit 169724a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
37 changes: 25 additions & 12 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package main

import (
"fmt"

"github.com/piquette/finance-go/chart"
"github.com/piquette/finance-go/crypto"
"github.com/piquette/finance-go/datetime"
"github.com/piquette/finance-go/equity"
"github.com/piquette/finance-go/etf"
"github.com/piquette/finance-go/forex"
"github.com/piquette/finance-go/future"
"github.com/piquette/finance-go/index"
"github.com/piquette/finance-go/mutualfund"
"github.com/piquette/finance-go/options"
"github.com/piquette/finance-go/quote"
Expand All @@ -20,7 +23,8 @@ func main() {
// Basic options example.
// --------------------
{
fmt.Println("Options stradle example\n====================\n")
fmt.Println("Options stradle example\n====================")
fmt.Println()
iter := options.GetStraddle("AAPL")

fmt.Println(iter.Meta())
Expand All @@ -37,7 +41,8 @@ func main() {
// Basic quote example.
// --------------------
{
fmt.Println("Quote example\n====================\n")
fmt.Println("Quote example\n====================")
fmt.Println()
q, err := quote.Get("GOOG")
if err != nil {
fmt.Println(err)
Expand All @@ -50,7 +55,8 @@ func main() {
// Basic chart example.
// --------------------
{
fmt.Println("Chart example\n====================\n")
fmt.Println("Chart example\n====================")
fmt.Println()
params := &chart.Params{
Symbol: "GOOG",
Interval: datetime.OneHour,
Expand All @@ -71,7 +77,8 @@ func main() {
// Basic crypto example.
// --------------------
{
fmt.Println("Crypto example\n====================\n")
fmt.Println("Crypto example\n====================")
fmt.Println()
q, err := crypto.Get("BTC-USD")

if err != nil {
Expand All @@ -85,7 +92,8 @@ func main() {
// Basic equity example.
// --------------------
{
fmt.Println("Equity example\n====================\n")
fmt.Println("Equity example\n====================")
fmt.Println()
symbols := []string{"AAPL", "GOOG", "MSFT"}
iter := equity.List(symbols)

Expand All @@ -104,7 +112,8 @@ func main() {
// Basic ETF example.
// --------------------
{
fmt.Println("ETF example\n====================\n")
fmt.Println("ETF example\n====================")
fmt.Println()
q, err := etf.Get("SPY")

if err != nil {
Expand All @@ -118,7 +127,8 @@ func main() {
// Basic forex example.
// --------------------
{
fmt.Println("Forex example\n====================\n")
fmt.Println("Forex example\n====================")
fmt.Println()
q, err := forex.Get("CADUSD=X")

if err != nil {
Expand All @@ -132,8 +142,9 @@ func main() {
// Basic future example.
// --------------------
{
fmt.Println("Future example\n====================\n")
q, err := forex.Get("CL=F")
fmt.Println("Future example\n====================")
fmt.Println()
q, err := future.Get("CL=F")

if err != nil {
fmt.Println(err)
Expand All @@ -146,8 +157,9 @@ func main() {
// Basic index example.
// --------------------
{
fmt.Println("Index example\n====================\n")
q, err := forex.Get("^DJI")
fmt.Println("Index example\n====================")
fmt.Println()
q, err := index.Get("^DJI")

if err != nil {
fmt.Println(err)
Expand All @@ -160,7 +172,8 @@ func main() {
// Basic mutual fund example.
// --------------------
{
fmt.Println("Mutual fund example\n====================\n")
fmt.Println("Mutual fund example\n====================")
fmt.Println()
q, err := mutualfund.Get("FMAGX")

if err != nil {
Expand Down
53 changes: 27 additions & 26 deletions finance.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ type BackendConfiguration struct {
HTTPClient *http.Client
}

type YahooConfiguration struct {
// yahooConfiguration is a specialization that includes a crumb and cookies for the yahoo API
type yahooConfiguration struct {
BackendConfiguration
expiry time.Time
cookies string
Expand All @@ -106,7 +107,7 @@ func SetHTTPClient(client *http.Client) {
// should only need to use this for testing purposes or on App Engine.
func NewBackends(httpClient *http.Client) *Backends {
return &Backends{
YFin: &YahooConfiguration{
YFin: &yahooConfiguration{
BackendConfiguration{YFinBackend, YFinURL, httpClient},
time.Time{},
"",
Expand All @@ -130,7 +131,7 @@ func GetBackend(backend SupportedBackend) Backend {
}
backends.mu.Lock()
defer backends.mu.Unlock()
backends.YFin = &YahooConfiguration{
backends.YFin = &yahooConfiguration{
BackendConfiguration{YFinBackend, YFinURL, httpClient},
time.Time{},
"",
Expand Down Expand Up @@ -163,11 +164,11 @@ func SetBackend(backend SupportedBackend, b Backend) {
}
}

func fetchCookies() (error, string, time.Time) {
func fetchCookies() (string, time.Time, error) {
client := http.Client{}
request, err := http.NewRequest("GET", cookieURL, nil)
if err != nil {
return err, "", time.Time{}
return "", time.Time{}, err
}

request.Header = http.Header{
Expand All @@ -187,7 +188,7 @@ func fetchCookies() (error, string, time.Time) {

response, err := client.Do(request)
if err != nil {
return err, "", time.Time{}
return "", time.Time{}, err
}
defer response.Body.Close()

Expand All @@ -212,14 +213,14 @@ func fetchCookies() (error, string, time.Time) {
}
}
result = strings.TrimSuffix(result, "; ")
return nil, result, expiry
return result, expiry, nil
}

func fetchCrumb(cookies string) (error, string) {
func fetchCrumb(cookies string) (string, error) {
client := http.Client{}
request, err := http.NewRequest("GET", crumbURL, nil)
if err != nil {
return err, ""
return "", err
}

request.Header = http.Header{
Expand All @@ -239,25 +240,25 @@ func fetchCrumb(cookies string) (error, string) {

response, err := client.Do(request)
if err != nil {
return err, ""
return "", err
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
return err, ""
return "", err
}

return nil, string(body[:])
return string(body[:]), nil
}

func (s *YahooConfiguration) refreshCrumb() error {
err, cookies, expiry := fetchCookies()
func (s *yahooConfiguration) refreshCrumb() error {
cookies, expiry, err := fetchCookies()
if err != nil {
return err
}

err, crumb := fetchCrumb(cookies)
crumb, err := fetchCrumb(cookies)
if err != nil {
return err
}
Expand All @@ -268,7 +269,8 @@ func (s *YahooConfiguration) refreshCrumb() error {
return nil
}

func (s *YahooConfiguration) Call(path string, form *form.Values, ctx *context.Context, v interface{}) error {
// Call is the Backend.Call implementation for invoking market data APIs, using the Yahoo specialization
func (s *yahooConfiguration) Call(path string, form *form.Values, ctx *context.Context, v interface{}) error {
// Check if the cookies have expired.
if s.expiry.Before(time.Now()) {
// Refresh the cookies and crumb.
Expand All @@ -286,12 +288,12 @@ func (s *YahooConfiguration) Call(path string, form *form.Values, ctx *context.C
path += "?" + form.Encode()
}

req, err := s.NewRequest("GET", path, ctx)
req, err := s.newRequest("GET", path, ctx)
if err != nil {
return err
}

if err := s.Do(req, v); err != nil {
if err := s.do(req, v); err != nil {
return err
}

Expand All @@ -305,20 +307,20 @@ func (s *BackendConfiguration) Call(path string, form *form.Values, ctx *context
path += "?" + form.Encode()
}

req, err := s.NewRequest("GET", path, ctx)
req, err := s.newRequest("GET", path, ctx)
if err != nil {
return err
}

if err := s.Do(req, v); err != nil {
if err := s.do(req, v); err != nil {
return err
}

return nil
}

func (s *YahooConfiguration) NewRequest(method, path string, ctx *context.Context) (*http.Request, error) {
req, err := s.BackendConfiguration.NewRequest(method, path, ctx)
func (s *yahooConfiguration) newRequest(method, path string, ctx *context.Context) (*http.Request, error) {
req, err := s.BackendConfiguration.newRequest(method, path, ctx)

if err != nil {
return nil, err
Expand All @@ -343,8 +345,7 @@ func (s *YahooConfiguration) NewRequest(method, path string, ctx *context.Contex
return req, nil
}

// NewRequest is used by Call to generate an http.Request.
func (s *BackendConfiguration) NewRequest(method, path string, ctx *context.Context) (*http.Request, error) {
func (s *BackendConfiguration) newRequest(method, path string, ctx *context.Context) (*http.Request, error) {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
Expand All @@ -365,10 +366,10 @@ func (s *BackendConfiguration) NewRequest(method, path string, ctx *context.Cont
return req, nil
}

// Do is used by Call to execute an API request and parse the response. It uses
// do is used by Call to execute an API request and parse the response. It uses
// the backend's HTTP client to execute the request and unmarshals the response
// into v. It also handles unmarshaling errors returned by the API.
func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
func (s *BackendConfiguration) do(req *http.Request, v interface{}) error {
if LogLevel > 1 {
Logger.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path)
}
Expand Down
4 changes: 2 additions & 2 deletions form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,14 @@ func (f *Values) Get(key string) []string {
// a cohesive way. For example, an array of maps in Rack is encoded with a
// string like:
//
// arr[][foo]=foo0&arr[][bar]=bar0&arr[][foo]=foo1&arr[][bar]=bar1
// arr[][foo]=foo0&arr[][bar]=bar0&arr[][foo]=foo1&arr[][bar]=bar1
//
// Because url.Values is a map, values will be handled in a way that's grouped
// by their key instead of in the order they were added. Therefore the above
// may by encoded to something like (maps are unordered so the actual result is
// somewhat non-deterministic):
//
// arr[][foo]=foo0&arr[][foo]=foo1&arr[][bar]=bar0&arr[][bar]=bar1
// arr[][foo]=foo0&arr[][foo]=foo1&arr[][bar]=bar0&arr[][bar]=bar1
//
// And thus result in an incorrect request.
func (f *Values) ToValues() url.Values {
Expand Down

0 comments on commit 169724a

Please sign in to comment.