From b6a914aba4c7b3a3766ca8d6aeed9948be9c11e5 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Thu, 17 Jun 2021 11:13:01 +0100 Subject: [PATCH] add history to currency --- currency/handler/currency.go | 74 +++++++ currency/proto/currency.pb.go | 316 +++++++++++++++++++++------- currency/proto/currency.pb.micro.go | 17 ++ currency/proto/currency.proto | 18 ++ 4 files changed, 353 insertions(+), 72 deletions(-) diff --git a/currency/handler/currency.go b/currency/handler/currency.go index 1c1394e..17832f1 100644 --- a/currency/handler/currency.go +++ b/currency/handler/currency.go @@ -7,6 +7,8 @@ import ( "io/ioutil" "net/http" "time" + "regexp" + "strings" "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/logger" @@ -14,6 +16,10 @@ import ( "github.com/patrickmn/go-cache" ) +var ( + re = regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) +) + type Currency struct { Api string Cache *cache.Cache @@ -67,6 +73,74 @@ func (c *Currency) Codes(ctx context.Context, req *pb.CodesRequest, rsp *pb.Code return nil } +func (c *Currency) History(ctx context.Context, req *pb.HistoryRequest, rsp *pb.HistoryResponse) error { + if len(req.Code) == 0 { + return errors.BadRequest("currency.rates", "missing code") + } + if len(req.Code) != 3 { + return errors.BadRequest("currency.rates", "code is invalid") + } + + if len(req.Date) == 0 { + return errors.BadRequest("currency.history", "missing date") + } + + if !re.MatchString(req.Date) { + return errors.BadRequest("currency.history", "invalid date") + } + + // try the cache + if rates, ok := c.Cache.Get("history:" + req.Code+req.Date); ok { + rsp.Code = req.Code + rsp.Date = req.Date + rsp.Rates = rates.(map[string]float64) + return nil + } + + parts := strings.Split(req.Date, "-") + + resp, err := http.Get(fmt.Sprintf("%s/history/%s/%s/%s/%s", c.Api, req.Code, parts[0], parts[1], parts[2])) + if err != nil { + logger.Errorf("Failed to get historic rates: %v\n", err) + return errors.InternalServerError("currency.history", "failed to get history") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get historic rates (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("currency.history", "failed to get history") + } + + var respBody map[string]interface{} + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal historic rates: %v\n", err) + return errors.InternalServerError("currency.history", "failed to get history") + } + + rates, ok := respBody["conversion_rates"].(map[string]interface{}) + if !ok { + logger.Errorf("Failed to convert historic rates to map[string]interface{}: %v\n", ok) + return errors.InternalServerError("currency.history", "failed to get history") + } + + rsp.Code = req.Code + rsp.Date = req.Date + rsp.Rates = make(map[string]float64) + + for code, rate := range rates { + rsp.Rates[code], _ = rate.(float64) + } + + // set for a period of time + c.Cache.Set("history:"+req.Code+req.Date, rsp.Rates, time.Hour * 24) + + return nil +} + + func (c *Currency) Rates(ctx context.Context, req *pb.RatesRequest, rsp *pb.RatesResponse) error { if len(req.Code) == 0 { return errors.BadRequest("currency.rates", "missing code") diff --git a/currency/proto/currency.pb.go b/currency/proto/currency.pb.go index b6be026..2414183 100644 --- a/currency/proto/currency.pb.go +++ b/currency/proto/currency.pb.go @@ -163,6 +163,130 @@ func (x *CodesResponse) GetCodes() []*Code { return nil } +// Returns the historic rates for a currency on a given date +type HistoryRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // currency code e.g USD + Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` + // date formatted 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_currency_proto_msgTypes[3] + 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_currency_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 HistoryRequest.ProtoReflect.Descriptor instead. +func (*HistoryRequest) Descriptor() ([]byte, []int) { + return file_proto_currency_proto_rawDescGZIP(), []int{3} +} + +func (x *HistoryRequest) GetCode() string { + if x != nil { + return x.Code + } + 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 code of the request + Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` + // The date requested + Date string `protobuf:"bytes,2,opt,name=date,proto3" json:"date,omitempty"` + // The rate for the day as code:rate + Rates map[string]float64 `protobuf:"bytes,3,rep,name=rates,proto3" json:"rates,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"fixed64,2,opt,name=value,proto3"` +} + +func (x *HistoryResponse) Reset() { + *x = HistoryResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_currency_proto_msgTypes[4] + 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_currency_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 HistoryResponse.ProtoReflect.Descriptor instead. +func (*HistoryResponse) Descriptor() ([]byte, []int) { + return file_proto_currency_proto_rawDescGZIP(), []int{4} +} + +func (x *HistoryResponse) GetCode() string { + if x != nil { + return x.Code + } + return "" +} + +func (x *HistoryResponse) GetDate() string { + if x != nil { + return x.Date + } + return "" +} + +func (x *HistoryResponse) GetRates() map[string]float64 { + if x != nil { + return x.Rates + } + return nil +} + // Rates returns the currency rates for a given code e.g USD type RatesRequest struct { state protoimpl.MessageState @@ -176,7 +300,7 @@ type RatesRequest struct { func (x *RatesRequest) Reset() { *x = RatesRequest{} if protoimpl.UnsafeEnabled { - mi := &file_proto_currency_proto_msgTypes[3] + mi := &file_proto_currency_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -189,7 +313,7 @@ func (x *RatesRequest) String() string { func (*RatesRequest) ProtoMessage() {} func (x *RatesRequest) ProtoReflect() protoreflect.Message { - mi := &file_proto_currency_proto_msgTypes[3] + mi := &file_proto_currency_proto_msgTypes[5] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -202,7 +326,7 @@ func (x *RatesRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use RatesRequest.ProtoReflect.Descriptor instead. func (*RatesRequest) Descriptor() ([]byte, []int) { - return file_proto_currency_proto_rawDescGZIP(), []int{3} + return file_proto_currency_proto_rawDescGZIP(), []int{5} } func (x *RatesRequest) GetCode() string { @@ -226,7 +350,7 @@ type RatesResponse struct { func (x *RatesResponse) Reset() { *x = RatesResponse{} if protoimpl.UnsafeEnabled { - mi := &file_proto_currency_proto_msgTypes[4] + mi := &file_proto_currency_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -239,7 +363,7 @@ func (x *RatesResponse) String() string { func (*RatesResponse) ProtoMessage() {} func (x *RatesResponse) ProtoReflect() protoreflect.Message { - mi := &file_proto_currency_proto_msgTypes[4] + mi := &file_proto_currency_proto_msgTypes[6] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -252,7 +376,7 @@ func (x *RatesResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use RatesResponse.ProtoReflect.Descriptor instead. func (*RatesResponse) Descriptor() ([]byte, []int) { - return file_proto_currency_proto_rawDescGZIP(), []int{4} + return file_proto_currency_proto_rawDescGZIP(), []int{6} } func (x *RatesResponse) GetCode() string { @@ -286,7 +410,7 @@ type ConvertRequest struct { func (x *ConvertRequest) Reset() { *x = ConvertRequest{} if protoimpl.UnsafeEnabled { - mi := &file_proto_currency_proto_msgTypes[5] + mi := &file_proto_currency_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -299,7 +423,7 @@ func (x *ConvertRequest) String() string { func (*ConvertRequest) ProtoMessage() {} func (x *ConvertRequest) ProtoReflect() protoreflect.Message { - mi := &file_proto_currency_proto_msgTypes[5] + mi := &file_proto_currency_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -312,7 +436,7 @@ func (x *ConvertRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use ConvertRequest.ProtoReflect.Descriptor instead. func (*ConvertRequest) Descriptor() ([]byte, []int) { - return file_proto_currency_proto_rawDescGZIP(), []int{5} + return file_proto_currency_proto_rawDescGZIP(), []int{7} } func (x *ConvertRequest) GetFrom() string { @@ -354,7 +478,7 @@ type ConvertResponse struct { func (x *ConvertResponse) Reset() { *x = ConvertResponse{} if protoimpl.UnsafeEnabled { - mi := &file_proto_currency_proto_msgTypes[6] + mi := &file_proto_currency_proto_msgTypes[8] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -367,7 +491,7 @@ func (x *ConvertResponse) String() string { func (*ConvertResponse) ProtoMessage() {} func (x *ConvertResponse) ProtoReflect() protoreflect.Message { - mi := &file_proto_currency_proto_msgTypes[6] + mi := &file_proto_currency_proto_msgTypes[8] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -380,7 +504,7 @@ func (x *ConvertResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use ConvertResponse.ProtoReflect.Descriptor instead. func (*ConvertResponse) Descriptor() ([]byte, []int) { - return file_proto_currency_proto_rawDescGZIP(), []int{6} + return file_proto_currency_proto_rawDescGZIP(), []int{8} } func (x *ConvertResponse) GetFrom() string { @@ -424,44 +548,62 @@ var file_proto_currency_proto_rawDesc = []byte{ 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x05, 0x63, 0x6f, 0x64, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x63, 0x6f, 0x64, 0x65, 0x73, 0x22, - 0x22, 0x0a, 0x0c, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, - 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, - 0x6f, 0x64, 0x65, 0x22, 0x97, 0x01, 0x0a, 0x0d, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x38, 0x0a, 0x05, 0x72, 0x61, 0x74, - 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, - 0x6e, 0x63, 0x79, 0x2e, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x2e, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x72, 0x61, - 0x74, 0x65, 0x73, 0x1a, 0x38, 0x0a, 0x0a, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, - 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, - 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x01, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x4c, 0x0a, - 0x0e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, - 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, - 0x72, 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x02, 0x74, 0x6f, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, - 0x01, 0x28, 0x01, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x61, 0x0a, 0x0f, 0x43, - 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, - 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x72, - 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, - 0x74, 0x6f, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x61, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, - 0x52, 0x04, 0x72, 0x61, 0x74, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, - 0x18, 0x04, 0x20, 0x01, 0x28, 0x01, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0xc4, - 0x01, 0x0a, 0x08, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x12, 0x3a, 0x0a, 0x05, 0x43, - 0x6f, 0x64, 0x65, 0x73, 0x12, 0x16, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, - 0x43, 0x6f, 0x64, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, - 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x73, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3a, 0x0a, 0x05, 0x52, 0x61, 0x74, 0x65, 0x73, - 0x12, 0x16, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x52, 0x61, 0x74, 0x65, + 0x38, 0x0a, 0x0e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x65, 0x22, 0xaf, 0x01, 0x0a, 0x0f, 0x48, 0x69, + 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, + 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x64, 0x61, 0x74, 0x65, 0x12, 0x3a, 0x0a, 0x05, 0x72, 0x61, 0x74, 0x65, 0x73, 0x18, 0x03, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, + 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, + 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x72, 0x61, 0x74, 0x65, + 0x73, 0x1a, 0x38, 0x0a, 0x0a, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, + 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, + 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, + 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x22, 0x0a, 0x0c, 0x52, + 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x63, + 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x22, + 0x97, 0x01, 0x0a, 0x0d, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x38, 0x0a, 0x05, 0x72, 0x61, 0x74, 0x65, 0x73, 0x18, 0x02, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, + 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x52, 0x61, + 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x72, 0x61, 0x74, 0x65, 0x73, 0x1a, + 0x38, 0x0a, 0x0a, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, + 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, + 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x4c, 0x0a, 0x0e, 0x43, 0x6f, 0x6e, + 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x66, + 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x12, + 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x74, 0x6f, 0x12, + 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, + 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x61, 0x0a, 0x0f, 0x43, 0x6f, 0x6e, 0x76, 0x65, + 0x72, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x72, + 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x12, 0x0e, + 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x74, 0x6f, 0x12, 0x12, + 0x0a, 0x04, 0x72, 0x61, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x04, 0x72, 0x61, + 0x74, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x01, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0x86, 0x02, 0x0a, 0x08, 0x43, + 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x12, 0x3a, 0x0a, 0x05, 0x43, 0x6f, 0x64, 0x65, 0x73, + 0x12, 0x16, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, - 0x6e, 0x63, 0x79, 0x2e, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x22, 0x00, 0x12, 0x40, 0x0a, 0x07, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x12, 0x18, - 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, - 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, - 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x12, 0x5a, 0x10, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x3b, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x22, 0x00, 0x12, 0x3a, 0x0a, 0x05, 0x52, 0x61, 0x74, 0x65, 0x73, 0x12, 0x16, 0x2e, 0x63, + 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, + 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, + 0x40, 0x0a, 0x07, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x12, 0x18, 0x2e, 0x63, 0x75, 0x72, + 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, + 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x00, 0x12, 0x40, 0x0a, 0x07, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x12, 0x18, 0x2e, 0x63, + 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, + 0x79, 0x2e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x22, 0x00, 0x42, 0x12, 0x5a, 0x10, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x63, + 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -476,31 +618,37 @@ func file_proto_currency_proto_rawDescGZIP() []byte { return file_proto_currency_proto_rawDescData } -var file_proto_currency_proto_msgTypes = make([]protoimpl.MessageInfo, 8) +var file_proto_currency_proto_msgTypes = make([]protoimpl.MessageInfo, 11) var file_proto_currency_proto_goTypes = []interface{}{ (*Code)(nil), // 0: currency.Code (*CodesRequest)(nil), // 1: currency.CodesRequest (*CodesResponse)(nil), // 2: currency.CodesResponse - (*RatesRequest)(nil), // 3: currency.RatesRequest - (*RatesResponse)(nil), // 4: currency.RatesResponse - (*ConvertRequest)(nil), // 5: currency.ConvertRequest - (*ConvertResponse)(nil), // 6: currency.ConvertResponse - nil, // 7: currency.RatesResponse.RatesEntry + (*HistoryRequest)(nil), // 3: currency.HistoryRequest + (*HistoryResponse)(nil), // 4: currency.HistoryResponse + (*RatesRequest)(nil), // 5: currency.RatesRequest + (*RatesResponse)(nil), // 6: currency.RatesResponse + (*ConvertRequest)(nil), // 7: currency.ConvertRequest + (*ConvertResponse)(nil), // 8: currency.ConvertResponse + nil, // 9: currency.HistoryResponse.RatesEntry + nil, // 10: currency.RatesResponse.RatesEntry } var file_proto_currency_proto_depIdxs = []int32{ - 0, // 0: currency.CodesResponse.codes:type_name -> currency.Code - 7, // 1: currency.RatesResponse.rates:type_name -> currency.RatesResponse.RatesEntry - 1, // 2: currency.Currency.Codes:input_type -> currency.CodesRequest - 3, // 3: currency.Currency.Rates:input_type -> currency.RatesRequest - 5, // 4: currency.Currency.Convert:input_type -> currency.ConvertRequest - 2, // 5: currency.Currency.Codes:output_type -> currency.CodesResponse - 4, // 6: currency.Currency.Rates:output_type -> currency.RatesResponse - 6, // 7: currency.Currency.Convert:output_type -> currency.ConvertResponse - 5, // [5:8] is the sub-list for method output_type - 2, // [2:5] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 0, // 0: currency.CodesResponse.codes:type_name -> currency.Code + 9, // 1: currency.HistoryResponse.rates:type_name -> currency.HistoryResponse.RatesEntry + 10, // 2: currency.RatesResponse.rates:type_name -> currency.RatesResponse.RatesEntry + 1, // 3: currency.Currency.Codes:input_type -> currency.CodesRequest + 5, // 4: currency.Currency.Rates:input_type -> currency.RatesRequest + 7, // 5: currency.Currency.Convert:input_type -> currency.ConvertRequest + 3, // 6: currency.Currency.History:input_type -> currency.HistoryRequest + 2, // 7: currency.Currency.Codes:output_type -> currency.CodesResponse + 6, // 8: currency.Currency.Rates:output_type -> currency.RatesResponse + 8, // 9: currency.Currency.Convert:output_type -> currency.ConvertResponse + 4, // 10: currency.Currency.History:output_type -> currency.HistoryResponse + 7, // [7:11] is the sub-list for method output_type + 3, // [3:7] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_proto_currency_proto_init() } @@ -546,7 +694,7 @@ func file_proto_currency_proto_init() { } } file_proto_currency_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*RatesRequest); i { + switch v := v.(*HistoryRequest); i { case 0: return &v.state case 1: @@ -558,7 +706,7 @@ func file_proto_currency_proto_init() { } } file_proto_currency_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*RatesResponse); i { + switch v := v.(*HistoryResponse); i { case 0: return &v.state case 1: @@ -570,7 +718,7 @@ func file_proto_currency_proto_init() { } } file_proto_currency_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ConvertRequest); i { + switch v := v.(*RatesRequest); i { case 0: return &v.state case 1: @@ -582,6 +730,30 @@ func file_proto_currency_proto_init() { } } file_proto_currency_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RatesResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_currency_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ConvertRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_currency_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ConvertResponse); i { case 0: return &v.state @@ -600,7 +772,7 @@ func file_proto_currency_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_proto_currency_proto_rawDesc, NumEnums: 0, - NumMessages: 8, + NumMessages: 11, NumExtensions: 0, NumServices: 1, }, diff --git a/currency/proto/currency.pb.micro.go b/currency/proto/currency.pb.micro.go index 0d72b61..f9f372d 100644 --- a/currency/proto/currency.pb.micro.go +++ b/currency/proto/currency.pb.micro.go @@ -45,6 +45,7 @@ type CurrencyService interface { Codes(ctx context.Context, in *CodesRequest, opts ...client.CallOption) (*CodesResponse, error) Rates(ctx context.Context, in *RatesRequest, opts ...client.CallOption) (*RatesResponse, error) Convert(ctx context.Context, in *ConvertRequest, opts ...client.CallOption) (*ConvertResponse, error) + History(ctx context.Context, in *HistoryRequest, opts ...client.CallOption) (*HistoryResponse, error) } type currencyService struct { @@ -89,12 +90,23 @@ func (c *currencyService) Convert(ctx context.Context, in *ConvertRequest, opts return out, nil } +func (c *currencyService) History(ctx context.Context, in *HistoryRequest, opts ...client.CallOption) (*HistoryResponse, error) { + req := c.c.NewRequest(c.name, "Currency.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 Currency service type CurrencyHandler interface { Codes(context.Context, *CodesRequest, *CodesResponse) error Rates(context.Context, *RatesRequest, *RatesResponse) error Convert(context.Context, *ConvertRequest, *ConvertResponse) error + History(context.Context, *HistoryRequest, *HistoryResponse) error } func RegisterCurrencyHandler(s server.Server, hdlr CurrencyHandler, opts ...server.HandlerOption) error { @@ -102,6 +114,7 @@ func RegisterCurrencyHandler(s server.Server, hdlr CurrencyHandler, opts ...serv Codes(ctx context.Context, in *CodesRequest, out *CodesResponse) error Rates(ctx context.Context, in *RatesRequest, out *RatesResponse) error Convert(ctx context.Context, in *ConvertRequest, out *ConvertResponse) error + History(ctx context.Context, in *HistoryRequest, out *HistoryResponse) error } type Currency struct { currency @@ -125,3 +138,7 @@ func (h *currencyHandler) Rates(ctx context.Context, in *RatesRequest, out *Rate func (h *currencyHandler) Convert(ctx context.Context, in *ConvertRequest, out *ConvertResponse) error { return h.CurrencyHandler.Convert(ctx, in, out) } + +func (h *currencyHandler) History(ctx context.Context, in *HistoryRequest, out *HistoryResponse) error { + return h.CurrencyHandler.History(ctx, in, out) +} diff --git a/currency/proto/currency.proto b/currency/proto/currency.proto index 73f074a..592c12d 100644 --- a/currency/proto/currency.proto +++ b/currency/proto/currency.proto @@ -8,6 +8,7 @@ service Currency { rpc Codes(CodesRequest) returns (CodesResponse) {} rpc Rates(RatesRequest) returns (RatesResponse) {} rpc Convert(ConvertRequest) returns (ConvertResponse) {} + rpc History(HistoryRequest) returns (HistoryResponse) {} } message Code { @@ -24,6 +25,23 @@ message CodesResponse { repeated Code codes = 1; } +// Returns the historic rates for a currency on a given date +message HistoryRequest { + // currency code e.g USD + string code = 1; + // date formatted as YYYY-MM-DD + string date = 2; +} + +message HistoryResponse { + // The code of the request + string code = 1; + // The date requested + string date = 2; + // The rate for the day as code:rate + map rates = 3; +} + // Rates returns the currency rates for a given code e.g USD message RatesRequest { // The currency code to get rates for e.g USD