-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61b95ff
commit 259e7d7
Showing
5 changed files
with
155 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/shopspring/decimal" | ||
) | ||
|
||
type HistoryEntry struct { | ||
Date time.Time `json:"date"` | ||
Close decimal.Decimal `json:"close"` | ||
High decimal.Decimal `json:"high"` | ||
Low decimal.Decimal `json:"low"` | ||
Volume uint64 `json:"volume"` | ||
Facevalue decimal.Decimal `json:"facevalue"` | ||
} | ||
|
||
type HistoryEntries []HistoryEntry | ||
|
||
func (entries HistoryEntries) MarshalBinary() ([]byte, error) { | ||
return json.Marshal(entries) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,108 @@ | ||
package api | ||
|
||
type SpbexAPI struct{} | ||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
custom_errors "github.com/kiberdruzhinnik/go-exchange-api/errors" | ||
"github.com/kiberdruzhinnik/go-exchange-api/utils" | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
type SpbexAPI struct { | ||
BaseURL string | ||
} | ||
|
||
type TimeRange struct { | ||
Start uint64 | ||
End uint64 | ||
} | ||
|
||
type SpbexSecurityJSON struct { | ||
Time []int `json:"t"` | ||
Open []float64 `json:"o"` | ||
High []float64 `json:"h"` | ||
Low []float64 `json:"l"` | ||
Close []float64 `json:"c"` | ||
Status string `json:"s"` | ||
} | ||
|
||
func NewSpbexAPI() SpbexAPI { | ||
return SpbexAPI{} | ||
return SpbexAPI{ | ||
BaseURL: "https://investcab.ru/api", | ||
} | ||
} | ||
|
||
func (api *SpbexAPI) GetTicker(ticker string) (HistoryEntries, error) { | ||
|
||
jsonHistory, err := api.getHistory(ticker) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
historyEntries := make(HistoryEntries, len(jsonHistory.Time)) | ||
for i := 0; i < len(jsonHistory.Time); i++ { | ||
|
||
entry := HistoryEntry{ | ||
Date: api.parseTime(int64(jsonHistory.Time[i])), | ||
Close: decimal.NewFromFloat(jsonHistory.Close[i]), | ||
High: decimal.NewFromFloat(jsonHistory.High[i]), | ||
Low: decimal.NewFromFloat(jsonHistory.Low[i]), | ||
Volume: 0, | ||
Facevalue: decimal.Zero, | ||
} | ||
historyEntries[i] = entry | ||
} | ||
|
||
return historyEntries, nil | ||
} | ||
|
||
func (api *SpbexAPI) GetTicker() {} | ||
func (api *SpbexAPI) parseTime(timestamp int64) time.Time { | ||
return time.Unix(timestamp, 0) | ||
} | ||
|
||
func (api *SpbexAPI) getHistory(ticker string) (SpbexSecurityJSON, error) { | ||
|
||
timeRange := api.getTimeRange() | ||
url := api.getUrl(ticker, "D", timeRange) | ||
|
||
log.Printf("Fetching history data from url %s for %s\n", url, ticker) | ||
data, err := utils.HttpGet(url) | ||
if err != nil { | ||
return SpbexSecurityJSON{}, err | ||
} | ||
|
||
var rawJson string | ||
err = json.Unmarshal(data, &rawJson) | ||
if err != nil { | ||
return SpbexSecurityJSON{}, err | ||
} | ||
|
||
var spbexSecurityJson SpbexSecurityJSON | ||
err = json.Unmarshal([]byte(rawJson), &spbexSecurityJson) | ||
if err != nil { | ||
return SpbexSecurityJSON{}, err | ||
} | ||
|
||
if len(spbexSecurityJson.Time) == 0 { | ||
return SpbexSecurityJSON{}, custom_errors.ErrorNotFound | ||
} | ||
|
||
return spbexSecurityJson, nil | ||
} | ||
|
||
func (api *SpbexAPI) getTimeRange() TimeRange { | ||
return TimeRange{ | ||
Start: 0, | ||
End: uint64(time.Now().Unix()), | ||
} | ||
} | ||
|
||
func (api *SpbexAPI) getUrl(ticker string, resolution string, timeRange TimeRange) string { | ||
return fmt.Sprintf( | ||
"%s/chistory?symbol=%s&resolution=%s&from=%d&to=%d", | ||
api.BaseURL, ticker, resolution, timeRange.Start, timeRange.End, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters