add currency codes endpoint

This commit is contained in:
Asim Aslam
2021-06-16 18:04:20 +01:00
parent c8122b2be6
commit 8a8a72a1d1
4 changed files with 353 additions and 76 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
@@ -18,6 +19,54 @@ type Currency struct {
Cache *cache.Cache
}
func (c *Currency) Codes(ctx context.Context, req *pb.CodesRequest, rsp *pb.CodesResponse) error {
// try the cache
if codes, ok := c.Cache.Get("codes"); ok {
rsp.Codes = codes.([]*pb.Code)
return nil
}
resp, err := http.Get(c.Api + "/codes")
if err != nil {
logger.Errorf("Failed to get codes: %v\n", err)
return errors.InternalServerError("currency.codes", "failed to get codes")
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
logger.Errorf("Failed to get codes (non 200): %d %v\n", resp.StatusCode, string(b))
return errors.InternalServerError("currency.codes", "failed to get codes")
}
var respBody map[string]interface{}
if err := json.Unmarshal(b, &respBody); err != nil {
logger.Errorf("Failed to unmarshal codes: %v\n", err)
return errors.InternalServerError("currency.codes", "failed to get codes")
}
codes, ok := respBody["supported_codes"].([]interface{})
if !ok {
logger.Errorf("Failed to convert rates to map[string]interface{}: %v\n", ok)
return errors.InternalServerError("currency.rates", "failed to get rates")
}
for _, code := range codes {
c := code.([]interface{})
rsp.Codes = append(rsp.Codes, &pb.Code{
Symbol: c[0].(string),
Name: c[1].(string),
})
}
// set for a period of time
c.Cache.Set("codes", rsp.Codes, time.Hour)
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")

View File

@@ -20,20 +20,163 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type Code struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// e.g USD
Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"`
// e.g United States Dollar
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
}
func (x *Code) Reset() {
*x = Code{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_currency_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Code) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Code) ProtoMessage() {}
func (x *Code) ProtoReflect() protoreflect.Message {
mi := &file_proto_currency_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 Code.ProtoReflect.Descriptor instead.
func (*Code) Descriptor() ([]byte, []int) {
return file_proto_currency_proto_rawDescGZIP(), []int{0}
}
func (x *Code) GetSymbol() string {
if x != nil {
return x.Symbol
}
return ""
}
func (x *Code) GetName() string {
if x != nil {
return x.Name
}
return ""
}
// Codes returns the supported currency codes for the API
type CodesRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
}
func (x *CodesRequest) Reset() {
*x = CodesRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_currency_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *CodesRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CodesRequest) ProtoMessage() {}
func (x *CodesRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_currency_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 CodesRequest.ProtoReflect.Descriptor instead.
func (*CodesRequest) Descriptor() ([]byte, []int) {
return file_proto_currency_proto_rawDescGZIP(), []int{1}
}
type CodesResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Codes []*Code `protobuf:"bytes,1,rep,name=codes,proto3" json:"codes,omitempty"`
}
func (x *CodesResponse) Reset() {
*x = CodesResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_currency_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *CodesResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CodesResponse) ProtoMessage() {}
func (x *CodesResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_currency_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 CodesResponse.ProtoReflect.Descriptor instead.
func (*CodesResponse) Descriptor() ([]byte, []int) {
return file_proto_currency_proto_rawDescGZIP(), []int{2}
}
func (x *CodesResponse) GetCodes() []*Code {
if x != nil {
return x.Codes
}
return nil
}
// Rates returns the currency rates for a given code e.g USD
type RatesRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// The currency code to get rates for
// The currency code to get rates for e.g USD
Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
}
func (x *RatesRequest) Reset() {
*x = RatesRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_currency_proto_msgTypes[0]
mi := &file_proto_currency_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -46,7 +189,7 @@ func (x *RatesRequest) String() string {
func (*RatesRequest) ProtoMessage() {}
func (x *RatesRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_currency_proto_msgTypes[0]
mi := &file_proto_currency_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -59,7 +202,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{0}
return file_proto_currency_proto_rawDescGZIP(), []int{3}
}
func (x *RatesRequest) GetCode() string {
@@ -74,15 +217,16 @@ type RatesResponse struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// The code requested e.g USD
Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
// The rates for the given code
// The rates for the given code as key-value pairs code:rate
Rates map[string]float64 `protobuf:"bytes,2,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 *RatesResponse) Reset() {
*x = RatesResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_currency_proto_msgTypes[1]
mi := &file_proto_currency_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -95,7 +239,7 @@ func (x *RatesResponse) String() string {
func (*RatesResponse) ProtoMessage() {}
func (x *RatesResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_currency_proto_msgTypes[1]
mi := &file_proto_currency_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -108,7 +252,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{1}
return file_proto_currency_proto_rawDescGZIP(), []int{4}
}
func (x *RatesResponse) GetCode() string {
@@ -131,18 +275,18 @@ type ConvertRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// base code to convert from
// base code to convert from e.g USD
From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"`
// target code to convert to
// target code to convert to e.g GBP
To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"`
// optional amoun to convert
// optional amount to convert e.g 10.0
Amount float64 `protobuf:"fixed64,3,opt,name=amount,proto3" json:"amount,omitempty"`
}
func (x *ConvertRequest) Reset() {
*x = ConvertRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_currency_proto_msgTypes[2]
mi := &file_proto_currency_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -155,7 +299,7 @@ func (x *ConvertRequest) String() string {
func (*ConvertRequest) ProtoMessage() {}
func (x *ConvertRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_currency_proto_msgTypes[2]
mi := &file_proto_currency_proto_msgTypes[5]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -168,7 +312,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{2}
return file_proto_currency_proto_rawDescGZIP(), []int{5}
}
func (x *ConvertRequest) GetFrom() string {
@@ -197,20 +341,20 @@ type ConvertResponse struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// the base code
// the base code e.g USD
From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"`
// the target code
// the target code e.g GBP
To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"`
// conversion rate
// conversion rate e.g 0.71
Rate float64 `protobuf:"fixed64,3,opt,name=rate,proto3" json:"rate,omitempty"`
// converted amount if specified
// converted amount e.g 7.10
Amount float64 `protobuf:"fixed64,4,opt,name=amount,proto3" json:"amount,omitempty"`
}
func (x *ConvertResponse) Reset() {
*x = ConvertResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_currency_proto_msgTypes[3]
mi := &file_proto_currency_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
@@ -223,7 +367,7 @@ func (x *ConvertResponse) String() string {
func (*ConvertResponse) ProtoMessage() {}
func (x *ConvertResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_currency_proto_msgTypes[3]
mi := &file_proto_currency_proto_msgTypes[6]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
@@ -236,7 +380,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{3}
return file_proto_currency_proto_rawDescGZIP(), []int{6}
}
func (x *ConvertResponse) GetFrom() string {
@@ -272,40 +416,51 @@ var File_proto_currency_proto protoreflect.FileDescriptor
var file_proto_currency_proto_rawDesc = []byte{
0x0a, 0x14, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79,
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,
0x88, 0x01, 0x0a, 0x08, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x63, 0x79, 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, 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,
0x22, 0x32, 0x0a, 0x04, 0x43, 0x6f, 0x64, 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, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
0x6e, 0x61, 0x6d, 0x65, 0x22, 0x0e, 0x0a, 0x0c, 0x43, 0x6f, 0x64, 0x65, 0x73, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x22, 0x35, 0x0a, 0x0d, 0x43, 0x6f, 0x64, 0x65, 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, 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,
}
var (
@@ -320,25 +475,31 @@ func file_proto_currency_proto_rawDescGZIP() []byte {
return file_proto_currency_proto_rawDescData
}
var file_proto_currency_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_proto_currency_proto_msgTypes = make([]protoimpl.MessageInfo, 8)
var file_proto_currency_proto_goTypes = []interface{}{
(*RatesRequest)(nil), // 0: currency.RatesRequest
(*RatesResponse)(nil), // 1: currency.RatesResponse
(*ConvertRequest)(nil), // 2: currency.ConvertRequest
(*ConvertResponse)(nil), // 3: currency.ConvertResponse
nil, // 4: currency.RatesResponse.RatesEntry
(*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
}
var file_proto_currency_proto_depIdxs = []int32{
4, // 0: currency.RatesResponse.rates:type_name -> currency.RatesResponse.RatesEntry
0, // 1: currency.Currency.Rates:input_type -> currency.RatesRequest
2, // 2: currency.Currency.Convert:input_type -> currency.ConvertRequest
1, // 3: currency.Currency.Rates:output_type -> currency.RatesResponse
3, // 4: currency.Currency.Convert:output_type -> currency.ConvertResponse
3, // [3:5] is the sub-list for method output_type
1, // [1:3] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
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
}
func init() { file_proto_currency_proto_init() }
@@ -348,7 +509,7 @@ func file_proto_currency_proto_init() {
}
if !protoimpl.UnsafeEnabled {
file_proto_currency_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*RatesRequest); i {
switch v := v.(*Code); i {
case 0:
return &v.state
case 1:
@@ -360,7 +521,7 @@ func file_proto_currency_proto_init() {
}
}
file_proto_currency_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*RatesResponse); i {
switch v := v.(*CodesRequest); i {
case 0:
return &v.state
case 1:
@@ -372,7 +533,7 @@ func file_proto_currency_proto_init() {
}
}
file_proto_currency_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ConvertRequest); i {
switch v := v.(*CodesResponse); i {
case 0:
return &v.state
case 1:
@@ -384,6 +545,42 @@ func file_proto_currency_proto_init() {
}
}
file_proto_currency_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*RatesRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_currency_proto_msgTypes[4].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[5].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[6].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ConvertResponse); i {
case 0:
return &v.state
@@ -402,7 +599,7 @@ func file_proto_currency_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_currency_proto_rawDesc,
NumEnums: 0,
NumMessages: 5,
NumMessages: 8,
NumExtensions: 0,
NumServices: 1,
},

View File

@@ -42,6 +42,7 @@ func NewCurrencyEndpoints() []*api.Endpoint {
// Client API for Currency service
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)
}
@@ -58,6 +59,16 @@ func NewCurrencyService(name string, c client.Client) CurrencyService {
}
}
func (c *currencyService) Codes(ctx context.Context, in *CodesRequest, opts ...client.CallOption) (*CodesResponse, error) {
req := c.c.NewRequest(c.name, "Currency.Codes", in)
out := new(CodesResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *currencyService) Rates(ctx context.Context, in *RatesRequest, opts ...client.CallOption) (*RatesResponse, error) {
req := c.c.NewRequest(c.name, "Currency.Rates", in)
out := new(RatesResponse)
@@ -81,12 +92,14 @@ func (c *currencyService) Convert(ctx context.Context, in *ConvertRequest, opts
// 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
}
func RegisterCurrencyHandler(s server.Server, hdlr CurrencyHandler, opts ...server.HandlerOption) error {
type currency interface {
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
}
@@ -101,6 +114,10 @@ type currencyHandler struct {
CurrencyHandler
}
func (h *currencyHandler) Codes(ctx context.Context, in *CodesRequest, out *CodesResponse) error {
return h.CurrencyHandler.Codes(ctx, in, out)
}
func (h *currencyHandler) Rates(ctx context.Context, in *RatesRequest, out *RatesResponse) error {
return h.CurrencyHandler.Rates(ctx, in, out)
}

View File

@@ -5,10 +5,25 @@ package currency;
option go_package = "./proto;currency";
service Currency {
rpc Codes(CodesRequest) returns (CodesResponse) {}
rpc Rates(RatesRequest) returns (RatesResponse) {}
rpc Convert(ConvertRequest) returns (ConvertResponse) {}
}
message Code {
// e.g USD
string symbol = 1;
// e.g United States Dollar
string name = 2;
}
// Codes returns the supported currency codes for the API
message CodesRequest {}
message CodesResponse {
repeated Code codes = 1;
}
// Rates returns the currency rates for a given code e.g USD
message RatesRequest {
// The currency code to get rates for e.g USD
@@ -22,7 +37,6 @@ message RatesResponse {
map<string, double> rates = 2;
}
// Convert returns the currency conversion rate between two pairs e.g USD/GBP
message ConvertRequest {
// base code to convert from e.g USD