add history to currency

This commit is contained in:
Asim Aslam
2021-06-17 11:13:01 +01:00
parent f8db6eb1a9
commit b6a914aba4
4 changed files with 353 additions and 72 deletions

View File

@@ -7,6 +7,8 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time" "time"
"regexp"
"strings"
"github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
@@ -14,6 +16,10 @@ import (
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
) )
var (
re = regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
)
type Currency struct { type Currency struct {
Api string Api string
Cache *cache.Cache Cache *cache.Cache
@@ -67,6 +73,74 @@ func (c *Currency) Codes(ctx context.Context, req *pb.CodesRequest, rsp *pb.Code
return nil 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 { func (c *Currency) Rates(ctx context.Context, req *pb.RatesRequest, rsp *pb.RatesResponse) error {
if len(req.Code) == 0 { if len(req.Code) == 0 {
return errors.BadRequest("currency.rates", "missing code") return errors.BadRequest("currency.rates", "missing code")

View File

@@ -163,6 +163,130 @@ func (x *CodesResponse) GetCodes() []*Code {
return nil 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 // Rates returns the currency rates for a given code e.g USD
type RatesRequest struct { type RatesRequest struct {
state protoimpl.MessageState state protoimpl.MessageState
@@ -176,7 +300,7 @@ type RatesRequest struct {
func (x *RatesRequest) Reset() { func (x *RatesRequest) Reset() {
*x = RatesRequest{} *x = RatesRequest{}
if protoimpl.UnsafeEnabled { 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 := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@@ -189,7 +313,7 @@ func (x *RatesRequest) String() string {
func (*RatesRequest) ProtoMessage() {} func (*RatesRequest) ProtoMessage() {}
func (x *RatesRequest) ProtoReflect() protoreflect.Message { 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 { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@@ -202,7 +326,7 @@ func (x *RatesRequest) ProtoReflect() protoreflect.Message {
// Deprecated: Use RatesRequest.ProtoReflect.Descriptor instead. // Deprecated: Use RatesRequest.ProtoReflect.Descriptor instead.
func (*RatesRequest) Descriptor() ([]byte, []int) { 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 { func (x *RatesRequest) GetCode() string {
@@ -226,7 +350,7 @@ type RatesResponse struct {
func (x *RatesResponse) Reset() { func (x *RatesResponse) Reset() {
*x = RatesResponse{} *x = RatesResponse{}
if protoimpl.UnsafeEnabled { 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 := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@@ -239,7 +363,7 @@ func (x *RatesResponse) String() string {
func (*RatesResponse) ProtoMessage() {} func (*RatesResponse) ProtoMessage() {}
func (x *RatesResponse) ProtoReflect() protoreflect.Message { 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 { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@@ -252,7 +376,7 @@ func (x *RatesResponse) ProtoReflect() protoreflect.Message {
// Deprecated: Use RatesResponse.ProtoReflect.Descriptor instead. // Deprecated: Use RatesResponse.ProtoReflect.Descriptor instead.
func (*RatesResponse) Descriptor() ([]byte, []int) { 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 { func (x *RatesResponse) GetCode() string {
@@ -286,7 +410,7 @@ type ConvertRequest struct {
func (x *ConvertRequest) Reset() { func (x *ConvertRequest) Reset() {
*x = ConvertRequest{} *x = ConvertRequest{}
if protoimpl.UnsafeEnabled { 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 := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@@ -299,7 +423,7 @@ func (x *ConvertRequest) String() string {
func (*ConvertRequest) ProtoMessage() {} func (*ConvertRequest) ProtoMessage() {}
func (x *ConvertRequest) ProtoReflect() protoreflect.Message { 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 { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@@ -312,7 +436,7 @@ func (x *ConvertRequest) ProtoReflect() protoreflect.Message {
// Deprecated: Use ConvertRequest.ProtoReflect.Descriptor instead. // Deprecated: Use ConvertRequest.ProtoReflect.Descriptor instead.
func (*ConvertRequest) Descriptor() ([]byte, []int) { 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 { func (x *ConvertRequest) GetFrom() string {
@@ -354,7 +478,7 @@ type ConvertResponse struct {
func (x *ConvertResponse) Reset() { func (x *ConvertResponse) Reset() {
*x = ConvertResponse{} *x = ConvertResponse{}
if protoimpl.UnsafeEnabled { 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 := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi) ms.StoreMessageInfo(mi)
} }
@@ -367,7 +491,7 @@ func (x *ConvertResponse) String() string {
func (*ConvertResponse) ProtoMessage() {} func (*ConvertResponse) ProtoMessage() {}
func (x *ConvertResponse) ProtoReflect() protoreflect.Message { 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 { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil { if ms.LoadMessageInfo() == nil {
@@ -380,7 +504,7 @@ func (x *ConvertResponse) ProtoReflect() protoreflect.Message {
// Deprecated: Use ConvertResponse.ProtoReflect.Descriptor instead. // Deprecated: Use ConvertResponse.ProtoReflect.Descriptor instead.
func (*ConvertResponse) Descriptor() ([]byte, []int) { 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 { 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, 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, 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, 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, 0x38, 0x0a, 0x0e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x6f, 0x64, 0x65, 0x22, 0x97, 0x01, 0x0a, 0x0d, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x65, 0x22, 0xaf, 0x01, 0x0a, 0x0f, 0x48, 0x69,
0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x38, 0x0a, 0x05, 0x72, 0x61, 0x74, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a,
0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64,
0x6e, 0x63, 0x79, 0x2e, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x65, 0x2e, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x72, 0x61, 0x04, 0x64, 0x61, 0x74, 0x65, 0x12, 0x3a, 0x0a, 0x05, 0x72, 0x61, 0x74, 0x65, 0x73, 0x18, 0x03,
0x74, 0x65, 0x73, 0x1a, 0x38, 0x0a, 0x0a, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e,
0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e,
0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x72, 0x61, 0x74, 0x65,
0x28, 0x01, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x4c, 0x0a, 0x73, 0x1a, 0x38, 0x0a, 0x0a, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12,
0x0e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65,
0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01,
0x72, 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x22, 0x0a, 0x0c, 0x52,
0x02, 0x74, 0x6f, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x63,
0x01, 0x28, 0x01, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x61, 0x0a, 0x0f, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x22,
0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x97, 0x01, 0x0a, 0x0d, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x72, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x38, 0x0a, 0x05, 0x72, 0x61, 0x74, 0x65, 0x73, 0x18, 0x02,
0x74, 0x6f, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x61, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e,
0x52, 0x04, 0x72, 0x61, 0x74, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x52, 0x61,
0x18, 0x04, 0x20, 0x01, 0x28, 0x01, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0xc4, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x05, 0x72, 0x61, 0x74, 0x65, 0x73, 0x1a,
0x01, 0x0a, 0x08, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x12, 0x3a, 0x0a, 0x05, 0x43, 0x38, 0x0a, 0x0a, 0x52, 0x61, 0x74, 0x65, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a,
0x6f, 0x64, 0x65, 0x73, 0x12, 0x16, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12,
0x43, 0x6f, 0x64, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05,
0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x73, 0x52, 0x65, 0x73, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x4c, 0x0a, 0x0e, 0x43, 0x6f, 0x6e,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3a, 0x0a, 0x05, 0x52, 0x61, 0x74, 0x65, 0x73, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x66,
0x12, 0x16, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x52, 0x61, 0x74, 0x65, 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, 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, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x64, 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, 0x65, 0x22, 0x00, 0x12, 0x3a, 0x0a, 0x05, 0x52, 0x61, 0x74, 0x65, 0x73, 0x12, 0x16, 0x2e, 0x63,
0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71,
0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e,
0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x52, 0x61, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12,
0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x12, 0x5a, 0x10, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x40, 0x0a, 0x07, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x12, 0x18, 0x2e, 0x63, 0x75, 0x72,
0x3b, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x2e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74, 0x52, 0x65, 0x71,
0x33, 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 ( var (
@@ -476,31 +618,37 @@ func file_proto_currency_proto_rawDescGZIP() []byte {
return file_proto_currency_proto_rawDescData 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{}{ var file_proto_currency_proto_goTypes = []interface{}{
(*Code)(nil), // 0: currency.Code (*Code)(nil), // 0: currency.Code
(*CodesRequest)(nil), // 1: currency.CodesRequest (*CodesRequest)(nil), // 1: currency.CodesRequest
(*CodesResponse)(nil), // 2: currency.CodesResponse (*CodesResponse)(nil), // 2: currency.CodesResponse
(*RatesRequest)(nil), // 3: currency.RatesRequest (*HistoryRequest)(nil), // 3: currency.HistoryRequest
(*RatesResponse)(nil), // 4: currency.RatesResponse (*HistoryResponse)(nil), // 4: currency.HistoryResponse
(*ConvertRequest)(nil), // 5: currency.ConvertRequest (*RatesRequest)(nil), // 5: currency.RatesRequest
(*ConvertResponse)(nil), // 6: currency.ConvertResponse (*RatesResponse)(nil), // 6: currency.RatesResponse
nil, // 7: currency.RatesResponse.RatesEntry (*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{ var file_proto_currency_proto_depIdxs = []int32{
0, // 0: currency.CodesResponse.codes:type_name -> currency.Code 0, // 0: currency.CodesResponse.codes:type_name -> currency.Code
7, // 1: currency.RatesResponse.rates:type_name -> currency.RatesResponse.RatesEntry 9, // 1: currency.HistoryResponse.rates:type_name -> currency.HistoryResponse.RatesEntry
1, // 2: currency.Currency.Codes:input_type -> currency.CodesRequest 10, // 2: currency.RatesResponse.rates:type_name -> currency.RatesResponse.RatesEntry
3, // 3: currency.Currency.Rates:input_type -> currency.RatesRequest 1, // 3: currency.Currency.Codes:input_type -> currency.CodesRequest
5, // 4: currency.Currency.Convert:input_type -> currency.ConvertRequest 5, // 4: currency.Currency.Rates:input_type -> currency.RatesRequest
2, // 5: currency.Currency.Codes:output_type -> currency.CodesResponse 7, // 5: currency.Currency.Convert:input_type -> currency.ConvertRequest
4, // 6: currency.Currency.Rates:output_type -> currency.RatesResponse 3, // 6: currency.Currency.History:input_type -> currency.HistoryRequest
6, // 7: currency.Currency.Convert:output_type -> currency.ConvertResponse 2, // 7: currency.Currency.Codes:output_type -> currency.CodesResponse
5, // [5:8] is the sub-list for method output_type 6, // 8: currency.Currency.Rates:output_type -> currency.RatesResponse
2, // [2:5] is the sub-list for method input_type 8, // 9: currency.Currency.Convert:output_type -> currency.ConvertResponse
2, // [2:2] is the sub-list for extension type_name 4, // 10: currency.Currency.History:output_type -> currency.HistoryResponse
2, // [2:2] is the sub-list for extension extendee 7, // [7:11] is the sub-list for method output_type
0, // [0:2] is the sub-list for field type_name 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() } 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{} { 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: case 0:
return &v.state return &v.state
case 1: 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{} { 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: case 0:
return &v.state return &v.state
case 1: 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{} { 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: case 0:
return &v.state return &v.state
case 1: 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{} { 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 { switch v := v.(*ConvertResponse); i {
case 0: case 0:
return &v.state return &v.state
@@ -600,7 +772,7 @@ func file_proto_currency_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_currency_proto_rawDesc, RawDescriptor: file_proto_currency_proto_rawDesc,
NumEnums: 0, NumEnums: 0,
NumMessages: 8, NumMessages: 11,
NumExtensions: 0, NumExtensions: 0,
NumServices: 1, NumServices: 1,
}, },

View File

@@ -45,6 +45,7 @@ type CurrencyService interface {
Codes(ctx context.Context, in *CodesRequest, opts ...client.CallOption) (*CodesResponse, error) Codes(ctx context.Context, in *CodesRequest, opts ...client.CallOption) (*CodesResponse, error)
Rates(ctx context.Context, in *RatesRequest, opts ...client.CallOption) (*RatesResponse, error) Rates(ctx context.Context, in *RatesRequest, opts ...client.CallOption) (*RatesResponse, error)
Convert(ctx context.Context, in *ConvertRequest, opts ...client.CallOption) (*ConvertResponse, 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 { type currencyService struct {
@@ -89,12 +90,23 @@ func (c *currencyService) Convert(ctx context.Context, in *ConvertRequest, opts
return out, nil 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 // Server API for Currency service
type CurrencyHandler interface { type CurrencyHandler interface {
Codes(context.Context, *CodesRequest, *CodesResponse) error Codes(context.Context, *CodesRequest, *CodesResponse) error
Rates(context.Context, *RatesRequest, *RatesResponse) error Rates(context.Context, *RatesRequest, *RatesResponse) error
Convert(context.Context, *ConvertRequest, *ConvertResponse) error Convert(context.Context, *ConvertRequest, *ConvertResponse) error
History(context.Context, *HistoryRequest, *HistoryResponse) error
} }
func RegisterCurrencyHandler(s server.Server, hdlr CurrencyHandler, opts ...server.HandlerOption) 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 Codes(ctx context.Context, in *CodesRequest, out *CodesResponse) error
Rates(ctx context.Context, in *RatesRequest, out *RatesResponse) error Rates(ctx context.Context, in *RatesRequest, out *RatesResponse) error
Convert(ctx context.Context, in *ConvertRequest, out *ConvertResponse) error Convert(ctx context.Context, in *ConvertRequest, out *ConvertResponse) error
History(ctx context.Context, in *HistoryRequest, out *HistoryResponse) error
} }
type Currency struct { type Currency struct {
currency 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 { func (h *currencyHandler) Convert(ctx context.Context, in *ConvertRequest, out *ConvertResponse) error {
return h.CurrencyHandler.Convert(ctx, in, out) 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)
}

View File

@@ -8,6 +8,7 @@ service Currency {
rpc Codes(CodesRequest) returns (CodesResponse) {} rpc Codes(CodesRequest) returns (CodesResponse) {}
rpc Rates(RatesRequest) returns (RatesResponse) {} rpc Rates(RatesRequest) returns (RatesResponse) {}
rpc Convert(ConvertRequest) returns (ConvertResponse) {} rpc Convert(ConvertRequest) returns (ConvertResponse) {}
rpc History(HistoryRequest) returns (HistoryResponse) {}
} }
message Code { message Code {
@@ -24,6 +25,23 @@ message CodesResponse {
repeated Code codes = 1; 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<string, double> rates = 3;
}
// Rates returns the currency rates for a given code e.g USD // Rates returns the currency rates for a given code e.g USD
message RatesRequest { message RatesRequest {
// The currency code to get rates for e.g USD // The currency code to get rates for e.g USD