-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented simple quote provider for ING
Can be useful for derivates. Fixes #165
- Loading branch information
Showing
5 changed files
with
200 additions
and
4 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
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,69 @@ | ||
// Copyright 2023 Christian Banse | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// This file is part of The Money Gopher. | ||
|
||
package securities | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
portfoliov1 "github.com/oxisto/money-gopher/gen" | ||
) | ||
|
||
const QuoteProviderING = "ing" | ||
|
||
type ing struct { | ||
http.Client | ||
} | ||
|
||
type header struct { | ||
Ask float32 `json:"ask"` | ||
AskDate time.Time `json:"askDate"` | ||
Bid float32 `json:"bid"` | ||
BidDate time.Time `json:"bidDate"` | ||
Currency string `json:"currency"` | ||
ISIN string `json:"isin"` | ||
HasBidAsk bool `json:"hasBidAsk"` | ||
Price float32 `json:"price"` | ||
PriceChangedDate time.Time `json:"priceChangeDate"` | ||
WKN string `json:"wkn"` | ||
} | ||
|
||
func (ing *ing) LatestQuote(ctx context.Context, ls *portfoliov1.ListedSecurity) (quote float32, t time.Time, err error) { | ||
var ( | ||
res *http.Response | ||
h header | ||
) | ||
|
||
res, err = ing.Get(fmt.Sprintf("https://component-api.wertpapiere.ing.de/api/v1/components/instrumentheader/%s", ls.SecurityName)) | ||
if err != nil { | ||
return 0, t, fmt.Errorf("could not fetch quote: %w", err) | ||
} | ||
|
||
err = json.NewDecoder(res.Body).Decode(&h) | ||
if err != nil { | ||
return 0, t, fmt.Errorf("could not decode JSON: %w", err) | ||
} | ||
|
||
if h.HasBidAsk { | ||
return h.Bid, h.BidDate, nil | ||
} else { | ||
return h.Price, h.PriceChangedDate, 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,119 @@ | ||
// Copyright 2023 Christian Banse | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// This file is part of The Money Gopher. | ||
|
||
package securities | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/oxisto/assert" | ||
portfoliov1 "github.com/oxisto/money-gopher/gen" | ||
) | ||
|
||
func Test_ing_LatestQuote(t *testing.T) { | ||
type fields struct { | ||
Client http.Client | ||
} | ||
type args struct { | ||
ctx context.Context | ||
ls *portfoliov1.ListedSecurity | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantQuote float32 | ||
wantTime time.Time | ||
wantErr assert.Want[error] | ||
}{ | ||
{ | ||
name: "http response error", | ||
fields: fields{ | ||
Client: newMockClient(func(req *http.Request) (res *http.Response, err error) { | ||
return nil, http.ErrNotSupported | ||
}), | ||
}, | ||
args: args{ | ||
ctx: context.TODO(), | ||
ls: &portfoliov1.ListedSecurity{ | ||
SecurityName: "My Security", | ||
Ticker: "TICK", | ||
Currency: "USD", | ||
}, | ||
}, | ||
wantErr: func(t *testing.T, err error) bool { | ||
return errors.Is(err, http.ErrNotSupported) | ||
}, | ||
}, | ||
{ | ||
name: "invalid JSON", | ||
fields: fields{ | ||
Client: newMockClient(func(req *http.Request) (res *http.Response, err error) { | ||
r := httptest.NewRecorder() | ||
r.WriteString(`{]`) | ||
return r.Result(), nil | ||
}), | ||
}, | ||
args: args{ | ||
ls: &portfoliov1.ListedSecurity{ | ||
SecurityName: "My Security", | ||
Ticker: "TICK", | ||
Currency: "USD", | ||
}, | ||
}, | ||
wantErr: func(t *testing.T, err error) bool { | ||
return strings.Contains(err.Error(), "invalid") | ||
}, | ||
}, | ||
{ | ||
name: "happy path", | ||
fields: fields{ | ||
Client: newMockClient(func(req *http.Request) (res *http.Response, err error) { | ||
r := httptest.NewRecorder() | ||
r.WriteString(`{"id":1,"name":"Call on My Security","bid": 100.0, "bidDate":"2023-05-04T20:00:00+00:00","hasBidAsk":true}`) | ||
return r.Result(), nil | ||
}), | ||
}, | ||
args: args{ | ||
ls: &portfoliov1.ListedSecurity{ | ||
SecurityName: "DE0000000001", | ||
Ticker: "", | ||
Currency: "EUR", | ||
}, | ||
}, | ||
wantQuote: float32(100.0), | ||
wantTime: time.Date(2023, 05, 04, 20, 0, 0, 0, time.UTC), | ||
wantErr: func(t *testing.T, err error) bool { return true }, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ing := &ing{ | ||
Client: tt.fields.Client, | ||
} | ||
gotQuote, gotTime, err := ing.LatestQuote(tt.args.ctx, tt.args.ls) | ||
assert.Equals(t, true, tt.wantErr(t, err)) | ||
assert.Equals(t, tt.wantQuote, gotQuote) | ||
assert.Equals(t, tt.wantTime.UTC(), gotTime.UTC()) | ||
}) | ||
} | ||
} |