-
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
Showing
6 changed files
with
602 additions
and
6 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
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) 2024 Neomantra Corp | ||
|
||
package dbn_hist | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// A **half**-closed date interval with an inclusive start date and an exclusive end date. | ||
type DateRange struct { | ||
// The start date (inclusive). | ||
Start time.Time `json:"start"` | ||
// The end date (exclusive). | ||
End time.Time `json:"end"` | ||
} | ||
|
||
type RequestError struct { | ||
Case string `json:"case"` | ||
Message string `json:"message"` | ||
StatusCode int `json:"status_code"` | ||
Docs string `json:"docs,omitempty"` | ||
Payload string `json:"payload,omitempty"` | ||
} | ||
|
||
type RequestErrorResp struct { | ||
Detail RequestError `json:"detail"` | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
func databentoGetRequest(urlStr string, apiKey string) ([]byte, error) { | ||
apiUrl, err := url.Parse(urlStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req, err := http.NewRequest("GET", apiUrl.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
auth := base64.StdEncoding.EncodeToString([]byte(apiKey + ":")) | ||
req.Header.Add("Authorization", "Basic "+auth) | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
func databentoPostFormRequest(urlStr string, apiKey string, form url.Values) ([]byte, error) { | ||
apiUrl, err := url.Parse(urlStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
formBody := strings.NewReader(form.Encode()) | ||
req, err := http.NewRequest("POST", apiUrl.String(), formBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
auth := base64.StdEncoding.EncodeToString([]byte(apiKey + ":")) | ||
req.Header.Add("Authorization", "Basic "+auth) | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
badStatusCode := (resp.StatusCode != http.StatusOK) | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
if badStatusCode { | ||
return nil, fmt.Errorf("HTTP %d %s %s %w", resp.StatusCode, resp.Status, string(body), err) | ||
} | ||
return nil, err | ||
} | ||
|
||
if badStatusCode { | ||
return nil, fmt.Errorf("HTTP %d %s %s", resp.StatusCode, resp.Status, string(body)) | ||
} | ||
|
||
return body, nil | ||
} |
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,75 @@ | ||
// Copyright (c) 2024 Neomantra Corp | ||
|
||
package dbn_hist_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
dbn "github.com/NimbleMarkets/dbn-go" | ||
dbn_hist "github.com/NimbleMarkets/dbn-go/hist" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
// Test Launcher | ||
func TestDbnHist(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "dbn-go hist suite") | ||
} | ||
|
||
var databentoApiKey string | ||
|
||
var _ = BeforeSuite(func() { | ||
databentoApiKey = os.Getenv("DATABENTO_API_KEY") | ||
if databentoApiKey == "" { | ||
Fail("DATABENTO_API_KEY not set") | ||
} | ||
}) | ||
|
||
var _ = Describe("DbnHist", func() { | ||
Context("metadata", func() { | ||
It("should ListPublishers", func() { | ||
publishers, err := dbn_hist.ListPublishers(databentoApiKey) | ||
Expect(err).To(BeNil()) | ||
Expect(publishers).ToNot(BeEmpty()) | ||
}) | ||
It("should ListDatasets, ListSchemas, ListFields, ListUnitPrices", func() { | ||
datasets, err := dbn_hist.ListDatasets(databentoApiKey, dbn_hist.DateRange{}) | ||
Expect(err).To(BeNil()) | ||
Expect(datasets).ToNot(BeEmpty()) | ||
|
||
schemas, err := dbn_hist.ListSchemas(databentoApiKey, datasets[0]) | ||
Expect(err).To(BeNil()) | ||
Expect(schemas).ToNot(BeEmpty()) | ||
|
||
schema, err := dbn.SchemaFromString(schemas[0]) | ||
Expect(err).To(BeNil()) | ||
|
||
fields, err := dbn_hist.ListFields(databentoApiKey, dbn.Encoding_Dbn, schema) | ||
Expect(err).To(BeNil()) | ||
Expect(fields).ToNot(BeEmpty()) | ||
|
||
unitPrices, err := dbn_hist.ListUnitPrices(databentoApiKey, datasets[0]) | ||
Expect(err).To(BeNil()) | ||
Expect(unitPrices).ToNot(BeEmpty()) | ||
}) | ||
It("should ResolveSymbology", func() { | ||
resolveParams := dbn_hist.ResolveParams{ | ||
Dataset: "XNAS.ITCH", | ||
Symbols: []string{"AAPL"}, | ||
StypeIn: dbn.SType_RawSymbol, | ||
StypeOut: dbn.SType_InstrumentId, | ||
DateRange: dbn_hist.DateRange{ | ||
Start: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), | ||
End: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), | ||
}, | ||
} | ||
resolveResp, err := dbn_hist.SymbologyResolve(databentoApiKey, resolveParams) | ||
Expect(err).To(BeNil()) | ||
Expect(resolveResp).ToNot(BeNil()) | ||
Expect(resolveResp.Mappings).ToNot(BeEmpty()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.