From 1b5740b3f07cc37143031f891b4c0482a06da56e Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 18 Jun 2021 15:21:41 +0100 Subject: [PATCH] add stock history --- stock/examples.json | 16 +++ stock/handler/stock.go | 57 ++++++++ stock/proto/stock.pb.go | 244 +++++++++++++++++++++++++++++++--- stock/proto/stock.pb.micro.go | 17 +++ stock/proto/stock.proto | 26 ++++ 5 files changed, 341 insertions(+), 19 deletions(-) diff --git a/stock/examples.json b/stock/examples.json index 9380954..c470781 100644 --- a/stock/examples.json +++ b/stock/examples.json @@ -25,5 +25,21 @@ "bid_size": 4, "timestamp": "2021-06-18T13:49:23.678Z" } + }], + "history": [{ + "title": "Get historic data", + "description": "Returns historic stock data for a given date", + "request": { + "symbol": "AAPL", + "date": "2020-10-01" + }, + "response": { + "symbol": "AAPL", + "open": 117.64, + "close": 116.79, + "high": 117.72, + "low": 115.83, + "date": "2020-10-01" + } }] } diff --git a/stock/handler/stock.go b/stock/handler/stock.go index a5dff9e..8c6fad6 100644 --- a/stock/handler/stock.go +++ b/stock/handler/stock.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "net/http" + "regexp" "time" "github.com/patrickmn/go-cache" @@ -14,6 +15,11 @@ import ( pb "github.com/micro/services/stock/proto" ) +var ( + re = regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) +) + + type Stock struct{ Api string Key string @@ -29,6 +35,57 @@ type Quote struct { Timestamp int64 } +type History struct { + Symbol string + Open float64 + High float64 + Low float64 + Close float64 + Volume int32 + From string +} + +func (s *Stock) History(ctx context.Context, req *pb.HistoryRequest, rsp *pb.HistoryResponse) error { + if len(req.Stock) < 0 || len(req.Stock) > 5 { + return errors.BadRequest("stock.history", "invalid symbol") + } + + if !re.MatchString(req.Date) { + return errors.BadRequest("stock.history", "invalid date") + } + + uri := fmt.Sprintf("%shistory/stock/open-close?stock=%s&date=%s&apikey=%s", s.Api, req.Stock, req.Date, s.Key) + + resp, err := http.Get(uri) + if err != nil { + logger.Errorf("Failed to get history: %v\n", err) + return errors.InternalServerError("stock.history", "failed to get history") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get history (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("stock.history", "failed to get history") + } + + var respBody History + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal history: %v\n", err) + return errors.InternalServerError("stock.history", "failed to get history") + } + + rsp.Symbol = respBody.Symbol + rsp.Open = respBody.Open + rsp.Close = respBody.Close + rsp.High = respBody.High + rsp.Low = respBody.Low + rsp.Date = respBody.From + + return nil +} func (s *Stock) Quote(ctx context.Context, req *pb.QuoteRequest, rsp *pb.QuoteResponse) error { if len(req.Symbol) < 0 || len(req.Symbol) > 5 { return errors.BadRequest("stock.quote", "invalid symbol") diff --git a/stock/proto/stock.pb.go b/stock/proto/stock.pb.go index e77b056..3e7cdbc 100644 --- a/stock/proto/stock.pb.go +++ b/stock/proto/stock.pb.go @@ -268,6 +268,166 @@ func (x *QuoteResponse) GetTimestamp() string { return "" } +// Get the historic open-close for a given day +type HistoryRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the stock symbol e.g AAPL + Stock string `protobuf:"bytes,1,opt,name=stock,proto3" json:"stock,omitempty"` + // date to retrieve as YYYY-MM-DD + Date string `protobuf:"bytes,2,opt,name=date,proto3" json:"date,omitempty"` +} + +func (x *HistoryRequest) Reset() { + *x = HistoryRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_stock_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HistoryRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HistoryRequest) ProtoMessage() {} + +func (x *HistoryRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_stock_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HistoryRequest.ProtoReflect.Descriptor instead. +func (*HistoryRequest) Descriptor() ([]byte, []int) { + return file_proto_stock_proto_rawDescGZIP(), []int{4} +} + +func (x *HistoryRequest) GetStock() string { + if x != nil { + return x.Stock + } + return "" +} + +func (x *HistoryRequest) GetDate() string { + if x != nil { + return x.Date + } + return "" +} + +type HistoryResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the stock symbol + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` + // the open price + Open float64 `protobuf:"fixed64,2,opt,name=open,proto3" json:"open,omitempty"` + // the close price + Close float64 `protobuf:"fixed64,3,opt,name=close,proto3" json:"close,omitempty"` + // the peak price + High float64 `protobuf:"fixed64,4,opt,name=high,proto3" json:"high,omitempty"` + // the low price + Low float64 `protobuf:"fixed64,5,opt,name=low,proto3" json:"low,omitempty"` + // the volume + Volume int32 `protobuf:"varint,6,opt,name=volume,proto3" json:"volume,omitempty"` + // the date + Date string `protobuf:"bytes,7,opt,name=date,proto3" json:"date,omitempty"` +} + +func (x *HistoryResponse) Reset() { + *x = HistoryResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_stock_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HistoryResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HistoryResponse) ProtoMessage() {} + +func (x *HistoryResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_stock_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HistoryResponse.ProtoReflect.Descriptor instead. +func (*HistoryResponse) Descriptor() ([]byte, []int) { + return file_proto_stock_proto_rawDescGZIP(), []int{5} +} + +func (x *HistoryResponse) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +func (x *HistoryResponse) GetOpen() float64 { + if x != nil { + return x.Open + } + return 0 +} + +func (x *HistoryResponse) GetClose() float64 { + if x != nil { + return x.Close + } + return 0 +} + +func (x *HistoryResponse) GetHigh() float64 { + if x != nil { + return x.High + } + return 0 +} + +func (x *HistoryResponse) GetLow() float64 { + if x != nil { + return x.Low + } + return 0 +} + +func (x *HistoryResponse) GetVolume() int32 { + if x != nil { + return x.Volume + } + return 0 +} + +func (x *HistoryResponse) GetDate() string { + if x != nil { + return x.Date + } + return "" +} + var File_proto_stock_proto protoreflect.FileDescriptor var file_proto_stock_proto_rawDesc = []byte{ @@ -293,15 +453,33 @@ var file_proto_stock_proto_rawDesc = []byte{ 0x73, 0x69, 0x7a, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x62, 0x69, 0x64, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, - 0x70, 0x32, 0x73, 0x0a, 0x05, 0x53, 0x74, 0x6f, 0x63, 0x6b, 0x12, 0x34, 0x0a, 0x05, 0x51, 0x75, - 0x6f, 0x74, 0x65, 0x12, 0x13, 0x2e, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x51, 0x75, 0x6f, 0x74, - 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x73, 0x74, 0x6f, 0x63, 0x6b, - 0x2e, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, - 0x12, 0x34, 0x0a, 0x05, 0x50, 0x72, 0x69, 0x63, 0x65, 0x12, 0x13, 0x2e, 0x73, 0x74, 0x6f, 0x63, - 0x6b, 0x2e, 0x50, 0x72, 0x69, 0x63, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, - 0x2e, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x50, 0x72, 0x69, 0x63, 0x65, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x3b, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x70, 0x22, 0x3a, 0x0a, 0x0e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x65, 0x22, 0xa5, 0x01, + 0x0a, 0x0f, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x12, 0x12, 0x0a, 0x04, 0x6f, 0x70, 0x65, + 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x04, 0x6f, 0x70, 0x65, 0x6e, 0x12, 0x14, 0x0a, + 0x05, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x63, 0x6c, + 0x6f, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x69, 0x67, 0x68, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x01, 0x52, 0x04, 0x68, 0x69, 0x67, 0x68, 0x12, 0x10, 0x0a, 0x03, 0x6c, 0x6f, 0x77, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x01, 0x52, 0x03, 0x6c, 0x6f, 0x77, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x6f, 0x6c, + 0x75, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x76, 0x6f, 0x6c, 0x75, 0x6d, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x64, 0x61, 0x74, 0x65, 0x32, 0xaf, 0x01, 0x0a, 0x05, 0x53, 0x74, 0x6f, 0x63, 0x6b, 0x12, + 0x34, 0x0a, 0x05, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x12, 0x13, 0x2e, 0x73, 0x74, 0x6f, 0x63, 0x6b, + 0x2e, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, + 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x05, 0x50, 0x72, 0x69, 0x63, 0x65, 0x12, 0x13, + 0x2e, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x50, 0x72, 0x69, 0x63, 0x65, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x50, 0x72, 0x69, 0x63, + 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3a, 0x0a, 0x07, 0x48, + 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x12, 0x15, 0x2e, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x48, + 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, + 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x3b, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -316,20 +494,24 @@ func file_proto_stock_proto_rawDescGZIP() []byte { return file_proto_stock_proto_rawDescData } -var file_proto_stock_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_proto_stock_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_proto_stock_proto_goTypes = []interface{}{ - (*PriceRequest)(nil), // 0: stock.PriceRequest - (*PriceResponse)(nil), // 1: stock.PriceResponse - (*QuoteRequest)(nil), // 2: stock.QuoteRequest - (*QuoteResponse)(nil), // 3: stock.QuoteResponse + (*PriceRequest)(nil), // 0: stock.PriceRequest + (*PriceResponse)(nil), // 1: stock.PriceResponse + (*QuoteRequest)(nil), // 2: stock.QuoteRequest + (*QuoteResponse)(nil), // 3: stock.QuoteResponse + (*HistoryRequest)(nil), // 4: stock.HistoryRequest + (*HistoryResponse)(nil), // 5: stock.HistoryResponse } var file_proto_stock_proto_depIdxs = []int32{ 2, // 0: stock.Stock.Quote:input_type -> stock.QuoteRequest 0, // 1: stock.Stock.Price:input_type -> stock.PriceRequest - 3, // 2: stock.Stock.Quote:output_type -> stock.QuoteResponse - 1, // 3: stock.Stock.Price:output_type -> stock.PriceResponse - 2, // [2:4] is the sub-list for method output_type - 0, // [0:2] is the sub-list for method input_type + 4, // 2: stock.Stock.History:input_type -> stock.HistoryRequest + 3, // 3: stock.Stock.Quote:output_type -> stock.QuoteResponse + 1, // 4: stock.Stock.Price:output_type -> stock.PriceResponse + 5, // 5: stock.Stock.History:output_type -> stock.HistoryResponse + 3, // [3:6] is the sub-list for method output_type + 0, // [0:3] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -389,6 +571,30 @@ func file_proto_stock_proto_init() { return nil } } + file_proto_stock_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HistoryRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_stock_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HistoryResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -396,7 +602,7 @@ func file_proto_stock_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_proto_stock_proto_rawDesc, NumEnums: 0, - NumMessages: 4, + NumMessages: 6, NumExtensions: 0, NumServices: 1, }, diff --git a/stock/proto/stock.pb.micro.go b/stock/proto/stock.pb.micro.go index 1b20622..0f238f0 100644 --- a/stock/proto/stock.pb.micro.go +++ b/stock/proto/stock.pb.micro.go @@ -44,6 +44,7 @@ func NewStockEndpoints() []*api.Endpoint { type StockService interface { Quote(ctx context.Context, in *QuoteRequest, opts ...client.CallOption) (*QuoteResponse, error) Price(ctx context.Context, in *PriceRequest, opts ...client.CallOption) (*PriceResponse, error) + History(ctx context.Context, in *HistoryRequest, opts ...client.CallOption) (*HistoryResponse, error) } type stockService struct { @@ -78,17 +79,29 @@ func (c *stockService) Price(ctx context.Context, in *PriceRequest, opts ...clie return out, nil } +func (c *stockService) History(ctx context.Context, in *HistoryRequest, opts ...client.CallOption) (*HistoryResponse, error) { + req := c.c.NewRequest(c.name, "Stock.History", in) + out := new(HistoryResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // Server API for Stock service type StockHandler interface { Quote(context.Context, *QuoteRequest, *QuoteResponse) error Price(context.Context, *PriceRequest, *PriceResponse) error + History(context.Context, *HistoryRequest, *HistoryResponse) error } func RegisterStockHandler(s server.Server, hdlr StockHandler, opts ...server.HandlerOption) error { type stock interface { Quote(ctx context.Context, in *QuoteRequest, out *QuoteResponse) error Price(ctx context.Context, in *PriceRequest, out *PriceResponse) error + History(ctx context.Context, in *HistoryRequest, out *HistoryResponse) error } type Stock struct { stock @@ -108,3 +121,7 @@ func (h *stockHandler) Quote(ctx context.Context, in *QuoteRequest, out *QuoteRe func (h *stockHandler) Price(ctx context.Context, in *PriceRequest, out *PriceResponse) error { return h.StockHandler.Price(ctx, in, out) } + +func (h *stockHandler) History(ctx context.Context, in *HistoryRequest, out *HistoryResponse) error { + return h.StockHandler.History(ctx, in, out) +} diff --git a/stock/proto/stock.proto b/stock/proto/stock.proto index 7947af3..7cacdd4 100644 --- a/stock/proto/stock.proto +++ b/stock/proto/stock.proto @@ -7,6 +7,7 @@ option go_package = "./proto;stock"; service Stock { rpc Quote(QuoteRequest) returns (QuoteResponse) {} rpc Price(PriceRequest) returns (PriceResponse) {} + rpc History(HistoryRequest) returns (HistoryResponse) {} } // Get the last price for a given stock ticker @@ -43,3 +44,28 @@ message QuoteResponse { string timestamp = 6; } + +// Get the historic open-close for a given day +message HistoryRequest { + // the stock symbol e.g AAPL + string stock = 1; + // date to retrieve as YYYY-MM-DD + string date = 2; +} + +message HistoryResponse { + // the stock symbol + string symbol = 1; + // the open price + double open = 2; + // the close price + double close = 3; + // the peak price + double high = 4; + // the low price + double low = 5; + // the volume + int32 volume = 6; + // the date + string date = 7; +}