diff --git a/stock/.gitignore b/stock/.gitignore new file mode 100644 index 0000000..f8d3f92 --- /dev/null +++ b/stock/.gitignore @@ -0,0 +1,2 @@ + +stock diff --git a/stock/Dockerfile b/stock/Dockerfile new file mode 100644 index 0000000..733013b --- /dev/null +++ b/stock/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD stock /stock +ENTRYPOINT [ "/stock" ] diff --git a/stock/Makefile b/stock/Makefile new file mode 100644 index 0000000..9cfdd0e --- /dev/null +++ b/stock/Makefile @@ -0,0 +1,28 @@ + +GOPATH:=$(shell go env GOPATH) +.PHONY: init +init: + go get -u github.com/golang/protobuf/proto + go get -u github.com/golang/protobuf/protoc-gen-go + go get github.com/micro/micro/v3/cmd/protoc-gen-micro + go get github.com/micro/micro/v3/cmd/protoc-gen-openapi + +.PHONY: api +api: + protoc --openapi_out=. --proto_path=. proto/stock.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/stock.proto + +.PHONY: build +build: + go build -o stock *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t stock:latest diff --git a/stock/README.md b/stock/README.md new file mode 100644 index 0000000..d3ea416 --- /dev/null +++ b/stock/README.md @@ -0,0 +1,7 @@ +Live stock quotes and prices + +# Stock Service + +Get live stock quotes and prices for thousands of stocks. + +Powered by [Finage](https://finage.co.uk/) diff --git a/stock/examples.json b/stock/examples.json new file mode 100644 index 0000000..9380954 --- /dev/null +++ b/stock/examples.json @@ -0,0 +1,29 @@ + +{ + "price": [{ + "title": "Get a stock price", + "description": "Returns the last traded price of a stock", + "request": { + "symbol": "AAPL" + }, + "response": { + "symbol": "AAPL", + "price": 131.265 + } + }], + "quote": [{ + "title": "Get a stock quote", + "description": "Returns the last quote for a stock including bid and ask prices", + "request": { + "symbol": "AAPL" + }, + "response": { + "symbol": "AAPL", + "ask": 131.12, + "bid": 131.11, + "ask_size": 7, + "bid_size": 4, + "timestamp": "2021-06-18T13:49:23.678Z" + } + }] +} diff --git a/stock/generate.go b/stock/generate.go new file mode 100644 index 0000000..96f431a --- /dev/null +++ b/stock/generate.go @@ -0,0 +1,2 @@ +package main +//go:generate make proto diff --git a/stock/handler/stock.go b/stock/handler/stock.go new file mode 100644 index 0000000..d566381 --- /dev/null +++ b/stock/handler/stock.go @@ -0,0 +1,102 @@ +package handler + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "time" + + "github.com/patrickmn/go-cache" + "github.com/micro/micro/v3/service/errors" + "github.com/micro/micro/v3/service/logger" + pb "github.com/micro/services/stock/proto" +) + +type Stock struct{ + Api string + Key string + Cache *cache.Cache +} + +type Quote struct { + Symbol string + Ask float64 + Bid float64 + Asize int32 + Bsize int32 + Timestamp int64 +} + +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") + } + + uri := fmt.Sprintf("%slast/stock/%s?apikey=%s", s.Api, req.Symbol, s.Key) + + resp, err := http.Get(uri) + if err != nil { + logger.Errorf("Failed to get quote: %v\n", err) + return errors.InternalServerError("stock.quote", "failed to get quote") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get quote (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("stock.quote", "failed to get quote") + } + + var respBody Quote + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal quote: %v\n", err) + return errors.InternalServerError("stock.quote", "failed to get quote") + } + + rsp.Symbol = respBody.Symbol + rsp.Ask = respBody.Ask + rsp.Bid = respBody.Bid + rsp.AskSize = respBody.Asize + rsp.BidSize = respBody.Bsize + rsp.Timestamp = time.Unix(0, respBody.Timestamp * int64(time.Millisecond)).UTC().Format(time.RFC3339Nano) + + return nil +} + +func (s *Stock) Price(ctx context.Context, req *pb.PriceRequest, rsp *pb.PriceResponse) error { + if len(req.Symbol) < 0 || len(req.Symbol) > 5 { + return errors.BadRequest("stock.price", "invalid symbol") + } + + uri := fmt.Sprintf("%slast/trade/stock/%s?apikey=%s", s.Api, req.Symbol, s.Key) + + resp, err := http.Get(uri) + if err != nil { + logger.Errorf("Failed to get price: %v\n", err) + return errors.InternalServerError("stock.trade", "failed to get price") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get price (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("stock.quote", "failed to get price") + } + + var respBody map[string]interface{} + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal price: %v\n", err) + return errors.InternalServerError("stock.price", "failed to get price") + } + + rsp.Symbol = req.Symbol + rsp.Price = respBody["price"].(float64) + + return nil +} diff --git a/stock/main.go b/stock/main.go new file mode 100644 index 0000000..c3cad65 --- /dev/null +++ b/stock/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "time" + + "github.com/micro/services/stock/handler" + pb "github.com/micro/services/stock/proto" + + "github.com/patrickmn/go-cache" + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/config" + "github.com/micro/micro/v3/service/logger" +) + +func main() { + // Create service + srv := service.New( + service.Name("stock"), + service.Version("latest"), + ) + + v, err := config.Get("finage.api") + if err != nil { + logger.Fatalf("finage.api config not found: %v", err) + } + api := v.String("") + if len(api) == 0 { + logger.Fatal("finage.api config not found") + } + v, err = config.Get("finage.key") + if err != nil { + logger.Fatalf("finage.key config not found: %v", err) + } + key := v.String("") + if len(key) == 0 { + logger.Fatal("finage.key config not found") + } + + // Register handler + pb.RegisterStockHandler(srv.Server(), &handler.Stock{ + Api: api, + Key: key, + Cache: cache.New(5*time.Minute, 10*time.Minute), + }) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/stock/micro.mu b/stock/micro.mu new file mode 100644 index 0000000..424b9d9 --- /dev/null +++ b/stock/micro.mu @@ -0,0 +1 @@ +service stock diff --git a/stock/proto/stock.pb.go b/stock/proto/stock.pb.go new file mode 100644 index 0000000..97afebc --- /dev/null +++ b/stock/proto/stock.pb.go @@ -0,0 +1,410 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.15.6 +// source: proto/stock.proto + +package stock + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Get the last price for a given stock ticker +type PriceRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // stock symbol e.g AAPL + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` +} + +func (x *PriceRequest) Reset() { + *x = PriceRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_stock_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PriceRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PriceRequest) ProtoMessage() {} + +func (x *PriceRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_stock_proto_msgTypes[0] + 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 PriceRequest.ProtoReflect.Descriptor instead. +func (*PriceRequest) Descriptor() ([]byte, []int) { + return file_proto_stock_proto_rawDescGZIP(), []int{0} +} + +func (x *PriceRequest) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +type PriceResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the stock symbol e.g AAPL + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` + // the last price + Price float64 `protobuf:"fixed64,2,opt,name=price,proto3" json:"price,omitempty"` +} + +func (x *PriceResponse) Reset() { + *x = PriceResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_stock_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PriceResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PriceResponse) ProtoMessage() {} + +func (x *PriceResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_stock_proto_msgTypes[1] + 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 PriceResponse.ProtoReflect.Descriptor instead. +func (*PriceResponse) Descriptor() ([]byte, []int) { + return file_proto_stock_proto_rawDescGZIP(), []int{1} +} + +func (x *PriceResponse) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +func (x *PriceResponse) GetPrice() float64 { + if x != nil { + return x.Price + } + return 0 +} + +// Get the last quote for the stock +type QuoteRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the stock symbol e.g AAPL + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` +} + +func (x *QuoteRequest) Reset() { + *x = QuoteRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_stock_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *QuoteRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*QuoteRequest) ProtoMessage() {} + +func (x *QuoteRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_stock_proto_msgTypes[2] + 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 QuoteRequest.ProtoReflect.Descriptor instead. +func (*QuoteRequest) Descriptor() ([]byte, []int) { + return file_proto_stock_proto_rawDescGZIP(), []int{2} +} + +func (x *QuoteRequest) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +type QuoteResponse 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 asking price + Ask float64 `protobuf:"fixed64,2,opt,name=ask,proto3" json:"ask,omitempty"` + // the bidding price + Bid float64 `protobuf:"fixed64,3,opt,name=bid,proto3" json:"bid,omitempty"` + // the ask size + AskSize int32 `protobuf:"varint,4,opt,name=ask_size,json=askSize,proto3" json:"ask_size,omitempty"` + // the bid size + BidSize int32 `protobuf:"varint,5,opt,name=bid_size,json=bidSize,proto3" json:"bid_size,omitempty"` + // the UTC timestamp of the quote + Timestamp string `protobuf:"bytes,6,opt,name=timestamp,proto3" json:"timestamp,omitempty"` +} + +func (x *QuoteResponse) Reset() { + *x = QuoteResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_stock_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *QuoteResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*QuoteResponse) ProtoMessage() {} + +func (x *QuoteResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_stock_proto_msgTypes[3] + 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 QuoteResponse.ProtoReflect.Descriptor instead. +func (*QuoteResponse) Descriptor() ([]byte, []int) { + return file_proto_stock_proto_rawDescGZIP(), []int{3} +} + +func (x *QuoteResponse) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +func (x *QuoteResponse) GetAsk() float64 { + if x != nil { + return x.Ask + } + return 0 +} + +func (x *QuoteResponse) GetBid() float64 { + if x != nil { + return x.Bid + } + return 0 +} + +func (x *QuoteResponse) GetAskSize() int32 { + if x != nil { + return x.AskSize + } + return 0 +} + +func (x *QuoteResponse) GetBidSize() int32 { + if x != nil { + return x.BidSize + } + return 0 +} + +func (x *QuoteResponse) GetTimestamp() string { + if x != nil { + return x.Timestamp + } + return "" +} + +var File_proto_stock_proto protoreflect.FileDescriptor + +var file_proto_stock_proto_rawDesc = []byte{ + 0x0a, 0x11, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x22, 0x26, 0x0a, 0x0c, 0x50, 0x72, + 0x69, 0x63, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, + 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, + 0x6f, 0x6c, 0x22, 0x3d, 0x0a, 0x0d, 0x50, 0x72, 0x69, 0x63, 0x65, 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, 0x14, 0x0a, 0x05, 0x70, + 0x72, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x70, 0x72, 0x69, 0x63, + 0x65, 0x22, 0x26, 0x0a, 0x0c, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x22, 0x9f, 0x01, 0x0a, 0x0d, 0x51, 0x75, + 0x6f, 0x74, 0x65, 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, 0x10, 0x0a, 0x03, 0x61, 0x73, 0x6b, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, + 0x52, 0x03, 0x61, 0x73, 0x6b, 0x12, 0x10, 0x0a, 0x03, 0x62, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x01, 0x52, 0x03, 0x62, 0x69, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x61, 0x73, 0x6b, 0x5f, 0x73, + 0x69, 0x7a, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x61, 0x73, 0x6b, 0x53, 0x69, + 0x7a, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x62, 0x69, 0x64, 0x5f, 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, +} + +var ( + file_proto_stock_proto_rawDescOnce sync.Once + file_proto_stock_proto_rawDescData = file_proto_stock_proto_rawDesc +) + +func file_proto_stock_proto_rawDescGZIP() []byte { + file_proto_stock_proto_rawDescOnce.Do(func() { + file_proto_stock_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_stock_proto_rawDescData) + }) + return file_proto_stock_proto_rawDescData +} + +var file_proto_stock_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +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 +} +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 + 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 +} + +func init() { file_proto_stock_proto_init() } +func file_proto_stock_proto_init() { + if File_proto_stock_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_stock_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PriceRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_stock_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PriceResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_stock_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*QuoteRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_stock_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*QuoteResponse); 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{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_stock_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_stock_proto_goTypes, + DependencyIndexes: file_proto_stock_proto_depIdxs, + MessageInfos: file_proto_stock_proto_msgTypes, + }.Build() + File_proto_stock_proto = out.File + file_proto_stock_proto_rawDesc = nil + file_proto_stock_proto_goTypes = nil + file_proto_stock_proto_depIdxs = nil +} diff --git a/stock/proto/stock.pb.micro.go b/stock/proto/stock.pb.micro.go new file mode 100644 index 0000000..1b20622 --- /dev/null +++ b/stock/proto/stock.pb.micro.go @@ -0,0 +1,110 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/stock.proto + +package stock + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +import ( + context "context" + api "github.com/micro/micro/v3/service/api" + client "github.com/micro/micro/v3/service/client" + server "github.com/micro/micro/v3/service/server" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Reference imports to suppress errors if they are not otherwise used. +var _ api.Endpoint +var _ context.Context +var _ client.Option +var _ server.Option + +// Api Endpoints for Stock service + +func NewStockEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Stock service + +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) +} + +type stockService struct { + c client.Client + name string +} + +func NewStockService(name string, c client.Client) StockService { + return &stockService{ + c: c, + name: name, + } +} + +func (c *stockService) Quote(ctx context.Context, in *QuoteRequest, opts ...client.CallOption) (*QuoteResponse, error) { + req := c.c.NewRequest(c.name, "Stock.Quote", in) + out := new(QuoteResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *stockService) Price(ctx context.Context, in *PriceRequest, opts ...client.CallOption) (*PriceResponse, error) { + req := c.c.NewRequest(c.name, "Stock.Price", in) + out := new(PriceResponse) + 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 +} + +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 + } + type Stock struct { + stock + } + h := &stockHandler{hdlr} + return s.Handle(s.NewHandler(&Stock{h}, opts...)) +} + +type stockHandler struct { + StockHandler +} + +func (h *stockHandler) Quote(ctx context.Context, in *QuoteRequest, out *QuoteResponse) error { + return h.StockHandler.Quote(ctx, in, out) +} + +func (h *stockHandler) Price(ctx context.Context, in *PriceRequest, out *PriceResponse) error { + return h.StockHandler.Price(ctx, in, out) +} diff --git a/stock/proto/stock.proto b/stock/proto/stock.proto new file mode 100644 index 0000000..63d3262 --- /dev/null +++ b/stock/proto/stock.proto @@ -0,0 +1,45 @@ +syntax = "proto3"; + +package stock; + +option go_package = "./proto;stock"; + +service Stock { + rpc Quote(QuoteRequest) returns (QuoteResponse) {} + rpc Price(PriceRequest) returns (PriceResponse) {} +} + +// Get the last price for a given stock ticker +message PriceRequest { + // stock symbol e.g AAPL + string symbol = 1; +} + +message PriceResponse { + // the stock symbol e.g AAPL + string symbol = 1; + // the last price + double price = 2; +} + +// Get the last quote for the stock +message QuoteRequest { + // the stock symbol e.g AAPL + string symbol = 1; +} + +message QuoteResponse { + // the stock symbol + string symbol = 1; + // the asking price + double ask = 2; + // the bidding price + double bid = 3; + // the ask size + int32 ask_size = 4; + // the bid size + int32 bid_size = 5; + // the UTC timestamp of the quote + string timestamp = 6; +} + diff --git a/stock/publicapi.json b/stock/publicapi.json new file mode 100644 index 0000000..ba81e12 --- /dev/null +++ b/stock/publicapi.json @@ -0,0 +1,9 @@ +{ + "name": "stock", + "icon": "📈", + "category": "money", + "pricing": { + "Stock.Price": 20, + "Stock.Quote": 20 + } +}