From 93d4415fb8fe925796b4815ef18e50afb561f43d Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 15 Jun 2021 20:35:40 +0100 Subject: [PATCH] add the currency service --- currency/.gitignore | 2 + currency/Dockerfile | 3 + currency/Makefile | 28 ++ currency/README.md | 6 + currency/config.md | 8 + currency/generate.go | 3 + currency/handler/currency.go | 105 +++++++ currency/main.go | 45 +++ currency/micro.mu | 1 + currency/proto/currency.pb.go | 417 ++++++++++++++++++++++++++++ currency/proto/currency.pb.micro.go | 110 ++++++++ currency/proto/currency.proto | 44 +++ currency/publicapi.json | 9 + 13 files changed, 781 insertions(+) create mode 100644 currency/.gitignore create mode 100644 currency/Dockerfile create mode 100644 currency/Makefile create mode 100644 currency/README.md create mode 100644 currency/config.md create mode 100644 currency/generate.go create mode 100644 currency/handler/currency.go create mode 100644 currency/main.go create mode 100644 currency/micro.mu create mode 100644 currency/proto/currency.pb.go create mode 100644 currency/proto/currency.pb.micro.go create mode 100644 currency/proto/currency.proto create mode 100644 currency/publicapi.json diff --git a/currency/.gitignore b/currency/.gitignore new file mode 100644 index 0000000..3cbe4c0 --- /dev/null +++ b/currency/.gitignore @@ -0,0 +1,2 @@ + +currency diff --git a/currency/Dockerfile b/currency/Dockerfile new file mode 100644 index 0000000..91ef815 --- /dev/null +++ b/currency/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD currency /currency +ENTRYPOINT [ "/currency" ] diff --git a/currency/Makefile b/currency/Makefile new file mode 100644 index 0000000..c0eedc0 --- /dev/null +++ b/currency/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/currency.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/currency.proto + +.PHONY: build +build: + go build -o currency *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t currency:latest diff --git a/currency/README.md b/currency/README.md new file mode 100644 index 0000000..d48d29c --- /dev/null +++ b/currency/README.md @@ -0,0 +1,6 @@ +Currency conversion powered by ExchangeRate-API + +# Currency Service + +Fast currency rates and conversion powered by [ExchangeRate-API](https://www.exchangerate-api.com/) + diff --git a/currency/config.md b/currency/config.md new file mode 100644 index 0000000..cf75fc1 --- /dev/null +++ b/currency/config.md @@ -0,0 +1,8 @@ +# Currency Config + +We currently depend on [ExchangeRate-API](https://www.exchangerate-api.com/). The config required is: + +- `exchangerate.api` as "https://v6.exchangerate-api.com/v6/" +- `exchangerate.key` as the api key + + diff --git a/currency/generate.go b/currency/generate.go new file mode 100644 index 0000000..7d9db91 --- /dev/null +++ b/currency/generate.go @@ -0,0 +1,3 @@ +package main + +//go:generate make proto diff --git a/currency/handler/currency.go b/currency/handler/currency.go new file mode 100644 index 0000000..a34913d --- /dev/null +++ b/currency/handler/currency.go @@ -0,0 +1,105 @@ +package handler + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "github.com/micro/micro/v3/service/errors" + "github.com/micro/micro/v3/service/logger" + pb "github.com/micro/services/currency/proto" +) + +type Currency struct { + Api string +} + +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") + } + if len(req.Code) != 3 { + return errors.BadRequest("currency.rates", "code is invalid") + } + + resp, err := http.Get(c.Api + "/latest/" + req.Code) + if err != nil { + logger.Errorf("Failed to get rates: %v\n", err) + return errors.InternalServerError("currency.rates", "failed to get rates") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get rates (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("currency.rates", "failed to get rates") + } + + var respBody map[string]interface{} + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal rates: %v\n", err) + return errors.InternalServerError("currency.rates", "failed to get rates") + } + + rates, ok := respBody["conversion_rates"].(map[string]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") + } + + rsp.Code = req.Code + rsp.Rates = make(map[string]float64) + + for code, rate := range rates { + rsp.Rates[code], _ = rate.(float64) + } + + return nil +} + +func (c *Currency) Convert(ctx context.Context, req *pb.ConvertRequest, rsp *pb.ConvertResponse) error { + if len(req.From) != 3 { + return errors.BadRequest("currency.convert", "invalid from code") + } + if len(req.To) != 3 { + return errors.BadRequest("currency.convert", "invalid to code") + } + + uri := fmt.Sprintf("%s/pair/%s/%s", c.Api, req.From, req.To) + + if req.Amount > 0.0 { + uri = fmt.Sprintf("%s/%v", uri, req.Amount) + } + + resp, err := http.Get(uri) + if err != nil { + logger.Errorf("Failed to convert: %v\n", err) + return errors.InternalServerError("currency.convert", "failed to convert") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get convert (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("currency.convert", "failed to convert") + } + + var respBody map[string]interface{} + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal conversion: %v\n", err) + return errors.InternalServerError("currency.convet", "failed to convert") + } + + rsp.From = req.From + rsp.To = req.To + rsp.Rate, _ = respBody["conversion_rate"].(float64) + rsp.Amount, _ = respBody["conversion_result"].(float64) + + return nil +} diff --git a/currency/main.go b/currency/main.go new file mode 100644 index 0000000..c541792 --- /dev/null +++ b/currency/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "github.com/micro/services/currency/handler" + pb "github.com/micro/services/currency/proto" + + "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("currency"), + service.Version("latest"), + ) + + v, err := config.Get("exchangerate.api") + if err != nil { + logger.Fatalf("exchangerate.api config not found: %v", err) + } + api := v.String("") + if len(api) == 0 { + logger.Fatal("exchangerate.api config not found") + } + v, err = config.Get("exchangerate.key") + if err != nil { + logger.Fatalf("exchangerate.key config not found: %v", err) + } + key := v.String("") + if len(key) == 0 { + logger.Fatal("exchangerate.key config not found") + } + + // Register handler + pb.RegisterCurrencyHandler(srv.Server(), &handler.Currency{ + Api: api + key, + }) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/currency/micro.mu b/currency/micro.mu new file mode 100644 index 0000000..d7f90dc --- /dev/null +++ b/currency/micro.mu @@ -0,0 +1 @@ +service currency diff --git a/currency/proto/currency.pb.go b/currency/proto/currency.pb.go new file mode 100644 index 0000000..ce93079 --- /dev/null +++ b/currency/proto/currency.pb.go @@ -0,0 +1,417 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.15.6 +// source: proto/currency.proto + +package currency + +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) +) + +// 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 + 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] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RatesRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RatesRequest) ProtoMessage() {} + +func (x *RatesRequest) 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 RatesRequest.ProtoReflect.Descriptor instead. +func (*RatesRequest) Descriptor() ([]byte, []int) { + return file_proto_currency_proto_rawDescGZIP(), []int{0} +} + +func (x *RatesRequest) GetCode() string { + if x != nil { + return x.Code + } + return "" +} + +type RatesResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` + // The rates for the given code + 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] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RatesResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RatesResponse) ProtoMessage() {} + +func (x *RatesResponse) 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 RatesResponse.ProtoReflect.Descriptor instead. +func (*RatesResponse) Descriptor() ([]byte, []int) { + return file_proto_currency_proto_rawDescGZIP(), []int{1} +} + +func (x *RatesResponse) GetCode() string { + if x != nil { + return x.Code + } + return "" +} + +func (x *RatesResponse) GetRates() map[string]float64 { + if x != nil { + return x.Rates + } + return nil +} + +// Convert returns the currency conversion rate between two pairs e.g USD/GBP +type ConvertRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // base code to convert from + From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"` + // target code to convert to + To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"` + // optional amoun to convert + 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] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ConvertRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ConvertRequest) ProtoMessage() {} + +func (x *ConvertRequest) 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 ConvertRequest.ProtoReflect.Descriptor instead. +func (*ConvertRequest) Descriptor() ([]byte, []int) { + return file_proto_currency_proto_rawDescGZIP(), []int{2} +} + +func (x *ConvertRequest) GetFrom() string { + if x != nil { + return x.From + } + return "" +} + +func (x *ConvertRequest) GetTo() string { + if x != nil { + return x.To + } + return "" +} + +func (x *ConvertRequest) GetAmount() float64 { + if x != nil { + return x.Amount + } + return 0 +} + +type ConvertResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the base code + From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"` + // the target code + To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"` + // conversion rate + Rate float64 `protobuf:"fixed64,3,opt,name=rate,proto3" json:"rate,omitempty"` + // converted amount if specified + 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] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ConvertResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ConvertResponse) ProtoMessage() {} + +func (x *ConvertResponse) 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 ConvertResponse.ProtoReflect.Descriptor instead. +func (*ConvertResponse) Descriptor() ([]byte, []int) { + return file_proto_currency_proto_rawDescGZIP(), []int{3} +} + +func (x *ConvertResponse) GetFrom() string { + if x != nil { + return x.From + } + return "" +} + +func (x *ConvertResponse) GetTo() string { + if x != nil { + return x.To + } + return "" +} + +func (x *ConvertResponse) GetRate() float64 { + if x != nil { + return x.Rate + } + return 0 +} + +func (x *ConvertResponse) GetAmount() float64 { + if x != nil { + return x.Amount + } + return 0 +} + +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, +} + +var ( + file_proto_currency_proto_rawDescOnce sync.Once + file_proto_currency_proto_rawDescData = file_proto_currency_proto_rawDesc +) + +func file_proto_currency_proto_rawDescGZIP() []byte { + file_proto_currency_proto_rawDescOnce.Do(func() { + file_proto_currency_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_currency_proto_rawDescData) + }) + return file_proto_currency_proto_rawDescData +} + +var file_proto_currency_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +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 +} +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 +} + +func init() { file_proto_currency_proto_init() } +func file_proto_currency_proto_init() { + if File_proto_currency_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_currency_proto_msgTypes[0].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[1].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[2].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[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ConvertResponse); 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_currency_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_currency_proto_goTypes, + DependencyIndexes: file_proto_currency_proto_depIdxs, + MessageInfos: file_proto_currency_proto_msgTypes, + }.Build() + File_proto_currency_proto = out.File + file_proto_currency_proto_rawDesc = nil + file_proto_currency_proto_goTypes = nil + file_proto_currency_proto_depIdxs = nil +} diff --git a/currency/proto/currency.pb.micro.go b/currency/proto/currency.pb.micro.go new file mode 100644 index 0000000..d7db985 --- /dev/null +++ b/currency/proto/currency.pb.micro.go @@ -0,0 +1,110 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/currency.proto + +package currency + +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 Currency service + +func NewCurrencyEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Currency service + +type CurrencyService interface { + Rates(ctx context.Context, in *RatesRequest, opts ...client.CallOption) (*RatesResponse, error) + Convert(ctx context.Context, in *ConvertRequest, opts ...client.CallOption) (*ConvertResponse, error) +} + +type currencyService struct { + c client.Client + name string +} + +func NewCurrencyService(name string, c client.Client) CurrencyService { + return ¤cyService{ + c: c, + name: name, + } +} + +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) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *currencyService) Convert(ctx context.Context, in *ConvertRequest, opts ...client.CallOption) (*ConvertResponse, error) { + req := c.c.NewRequest(c.name, "Currency.Convert", in) + out := new(ConvertResponse) + 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 { + 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 { + Rates(ctx context.Context, in *RatesRequest, out *RatesResponse) error + Convert(ctx context.Context, in *ConvertRequest, out *ConvertResponse) error + } + type Currency struct { + currency + } + h := ¤cyHandler{hdlr} + return s.Handle(s.NewHandler(&Currency{h}, opts...)) +} + +type currencyHandler struct { + CurrencyHandler +} + +func (h *currencyHandler) Rates(ctx context.Context, in *RatesRequest, out *RatesResponse) error { + return h.CurrencyHandler.Rates(ctx, in, out) +} + +func (h *currencyHandler) Convert(ctx context.Context, in *ConvertRequest, out *ConvertResponse) error { + return h.CurrencyHandler.Convert(ctx, in, out) +} diff --git a/currency/proto/currency.proto b/currency/proto/currency.proto new file mode 100644 index 0000000..2480b06 --- /dev/null +++ b/currency/proto/currency.proto @@ -0,0 +1,44 @@ +syntax = "proto3"; + +package currency; + +option go_package = "./proto;currency"; + +service Currency { + rpc Rates(RatesRequest) returns (RatesResponse) {} + rpc Convert(ConvertRequest) returns (ConvertResponse) {} +} + +// Rates returns the currency rates for a given code e.g USD +message RatesRequest { + // The currency code to get rates for + string code = 1; +} + +message RatesResponse { + string code = 1; + // The rates for the given code + map rates = 2; +} + + +// Convert returns the currency conversion rate between two pairs e.g USD/GBP +message ConvertRequest { + // base code to convert from + string from = 1; + // target code to convert to + string to = 2; + // optional amoun to convert + double amount = 3; +} + +message ConvertResponse { + // the base code + string from = 1; + // the target code + string to = 2; + // conversion rate + double rate = 3; + // converted amount if specified + double amount = 4; +} diff --git a/currency/publicapi.json b/currency/publicapi.json new file mode 100644 index 0000000..d40037a --- /dev/null +++ b/currency/publicapi.json @@ -0,0 +1,9 @@ +{ + "name": "currency", + "icon": "💱", + "category": "money", + "pricing": { + "Currency.Rates", 100, + "Currency.Convert", 100 + } +}