Skip to content

Commit

Permalink
refactor: add ctx param to scraper interfaces and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmethakanbesel committed Jan 13, 2024
1 parent 5c94573 commit 35e3fbc
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ import (
)

func main() {
ctx := context.Background()

tefasScraper := tefas.NewScraper(
tefas.WithWorkers(5),
)

// get last year's data for the given fund
tefasData, err := tefasScraper.GetSymbolData("FUNDCODE", time.Now().AddDate(-1, 0, 0), time.Now())
tefasData, err := tefasScraper.GetSymbolData(ctx, "FUNDCODE", time.Now().AddDate(-1, 0, 0), time.Now())
if err != nil {
// handle error
}
Expand All @@ -130,7 +132,7 @@ func main() {
)

// get last year's data for the given symbol
yahooData, err := yahooScraper.GetSymbolData("SYMBOLCODE", time.Now().AddDate(-1, 0, 0), time.Now())
yahooData, err := yahooScraper.GetSymbolData(ctx, "SYMBOLCODE", time.Now().AddDate(-1, 0, 0), time.Now())
if err != nil {
// handle error
}
Expand Down
3 changes: 2 additions & 1 deletion scraper/scraper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scraper

import (
"context"
"time"
)

Expand All @@ -9,5 +10,5 @@ const (
)

type Scraper interface {
GetSymbolData(symbol string, startDate, endDate time.Time) (<-chan *SymbolPrice, error)
GetSymbolData(ctx context.Context, symbol string, startDate, endDate time.Time) (<-chan *SymbolPrice, error)
}
4 changes: 3 additions & 1 deletion tefas/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (a *Api) SetupRoutes(g *echo.Group) {
}

func (a *Api) getFundData(c echo.Context) error {
ctx := c.Request().Context()

symbol := strings.ToUpper(c.PathParam("symbol"))
if len(symbol) < 3 {
return c.String(http.StatusBadRequest, "symbol must be at least 3 characters")
Expand Down Expand Up @@ -56,7 +58,7 @@ func (a *Api) getFundData(c echo.Context) error {
return c.String(http.StatusBadRequest, "currency must be either TRY or USD")
}

records, err := a.service.GetAndSaveFundData(symbol, currency, startDate, endDate)
records, err := a.service.GetAndSaveFundData(ctx, symbol, currency, startDate, endDate)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
Expand Down
3 changes: 2 additions & 1 deletion tefas/scraper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tefas

import (
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -68,7 +69,7 @@ type (
}
)

func (s *Scraper) GetSymbolData(symbol string, startDate, endDate time.Time) (<-chan *scraper.SymbolPrice, error) {
func (s *Scraper) GetSymbolData(ctx context.Context, symbol string, startDate, endDate time.Time) (<-chan *scraper.SymbolPrice, error) {
if symbol == "" {
return nil, fmt.Errorf("fund code cannot be empty")
}
Expand Down
13 changes: 7 additions & 6 deletions tefas/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tefas

import (
"context"
"database/sql"
"errors"
"log/slog"
Expand All @@ -18,8 +19,8 @@ const (
)

type Service interface {
GetAndSaveFundData(fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error)
GetScrape(fundCode, currenct string, startDate, endDate time.Time) (*models.Record, error)
GetAndSaveFundData(ctx context.Context, fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error)
GetScrape(ctx context.Context, fundCode, currenct string, startDate, endDate time.Time) (*models.Record, error)
}

type service struct {
Expand All @@ -37,7 +38,7 @@ func NewService(app *pocketbase.PocketBase, scraper scraper.Scraper) Service {
}
}

func (s *service) GetScrape(fundCode, currency string, startDate, endDate time.Time) (*models.Record, error) {
func (s *service) GetScrape(ctx context.Context, fundCode, currency string, startDate, endDate time.Time) (*models.Record, error) {
record, err := s.app.Dao().FindFirstRecordByFilter(
"scrapes",
"source = {:source} && symbol = {:symbol} && startDate = {:startDate} && endDate = {:endDate} && currency = {:currency}",
Expand All @@ -53,16 +54,16 @@ func (s *service) GetScrape(fundCode, currency string, startDate, endDate time.T
return record, err
}

func (s *service) GetAndSaveFundData(fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error) {
func (s *service) GetAndSaveFundData(ctx context.Context, fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error) {
dao := s.app.Dao()

record, err := s.GetScrape(fundCode, currency, startDate, endDate)
record, err := s.GetScrape(ctx, fundCode, currency, startDate, endDate)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, err
}

if record == nil {
dataChan, err := s.scraper.GetSymbolData(fundCode, startDate, endDate)
dataChan, err := s.scraper.GetSymbolData(ctx, fundCode, startDate, endDate)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion yahoo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (a *Api) SetupRoutes(g *echo.Group) {
}

func (a *Api) getSymbolData(c echo.Context) error {
ctx := c.Request().Context()

symbol := strings.ToUpper(c.PathParam("symbol"))
if len(symbol) < 3 {
return c.String(http.StatusBadRequest, "symbol must be at least 3 characters")
Expand Down Expand Up @@ -56,7 +58,7 @@ func (a *Api) getSymbolData(c echo.Context) error {
return c.String(http.StatusBadRequest, "currency must be either TRY or USD")
}

records, err := a.service.GetAndSaveSymbolData(symbol, currency, startDate, endDate)
records, err := a.service.GetAndSaveSymbolData(ctx, symbol, currency, startDate, endDate)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
Expand Down
3 changes: 2 additions & 1 deletion yahoo/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package yahoo

import (
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
Expand Down Expand Up @@ -50,7 +51,7 @@ type (
}
)

func (s *Scraper) GetSymbolData(symbol string, startDate, endDate time.Time) (<-chan *scraper.SymbolPrice, error) {
func (s *Scraper) GetSymbolData(ctx context.Context, symbol string, startDate, endDate time.Time) (<-chan *scraper.SymbolPrice, error) {
if symbol == "" {
return nil, fmt.Errorf("symbol cannot be empty")
}
Expand Down
13 changes: 7 additions & 6 deletions yahoo/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package yahoo

import (
"context"
"database/sql"
"errors"
"log/slog"
Expand All @@ -18,8 +19,8 @@ const (
)

type Service interface {
GetAndSaveSymbolData(fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error)
GetScrape(fundCode, currenct string, startDate, endDate time.Time) (*models.Record, error)
GetAndSaveSymbolData(ctx context.Context, fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error)
GetScrape(ctx context.Context, fundCode, currenct string, startDate, endDate time.Time) (*models.Record, error)
}

type service struct {
Expand All @@ -37,7 +38,7 @@ func NewService(app *pocketbase.PocketBase, scraper scraper.Scraper) Service {
}
}

func (s *service) GetScrape(fundCode, currency string, startDate, endDate time.Time) (*models.Record, error) {
func (s *service) GetScrape(ctx context.Context, fundCode, currency string, startDate, endDate time.Time) (*models.Record, error) {
record, err := s.app.Dao().FindFirstRecordByFilter(
"scrapes",
"source = {:source} && symbol = {:symbol} && startDate = {:startDate} && endDate = {:endDate} && currency = {:currency}",
Expand All @@ -53,16 +54,16 @@ func (s *service) GetScrape(fundCode, currency string, startDate, endDate time.T
return record, err
}

func (s *service) GetAndSaveSymbolData(fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error) {
func (s *service) GetAndSaveSymbolData(ctx context.Context, fundCode, currency string, startDate, endDate time.Time) ([]*models.Record, error) {
dao := s.app.Dao()

record, err := s.GetScrape(fundCode, currency, startDate, endDate)
record, err := s.GetScrape(ctx, fundCode, currency, startDate, endDate)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, err
}

if record == nil {
dataChan, err := s.scraper.GetSymbolData(fundCode, startDate, endDate)
dataChan, err := s.scraper.GetSymbolData(ctx, fundCode, startDate, endDate)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 35e3fbc

Please sign in to comment.