From 5e6219dbde3aab4bb845781b5d34d06721552805 Mon Sep 17 00:00:00 2001 From: Kleonikos Kyriakis Date: Mon, 15 Apr 2024 18:09:43 +0300 Subject: [PATCH] Add support for Mint and Validation services - Bump protocol version to release-5 --- camino-messenger-bot.yaml.example | 2 ++ examples/rpc/partner-plugin/server.go | 48 ++++++++++++++++++++++++-- go.mod | 4 +-- go.sum | 10 +++--- internal/matrix/types.go | 8 +++++ internal/messaging/service.go | 36 +++++++++++++++++++ internal/messaging/service_registry.go | 7 ++++ internal/messaging/types.go | 21 +++++++++++ internal/rpc/server/server.go | 16 +++++++++ 9 files changed, 142 insertions(+), 10 deletions(-) diff --git a/camino-messenger-bot.yaml.example b/camino-messenger-bot.yaml.example index 86fc5d43..70ff537d 100644 --- a/camino-messenger-bot.yaml.example +++ b/camino-messenger-bot.yaml.example @@ -20,5 +20,7 @@ supported_request_types: - AccommodationSearchRequest - GetNetworkFeeRequest - GetPartnerConfigurationRequest + - MintRequest + - ValidationRequest - PingRequest - TransportSearchRequest \ No newline at end of file diff --git a/examples/rpc/partner-plugin/server.go b/examples/rpc/partner-plugin/server.go index bd04b02d..20116f9b 100644 --- a/examples/rpc/partner-plugin/server.go +++ b/examples/rpc/partner-plugin/server.go @@ -7,24 +7,27 @@ import ( "net" "os" "strconv" + "time" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/accommodation/v1alpha/accommodationv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/activity/v1alpha/activityv1alphagrpc" + "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/book/v1alpha/bookv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/network/v1alpha/networkv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/partner/v1alpha/partnerv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/ping/v1alpha/pingv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/transport/v1alpha/transportv1alphagrpc" + "github.com/chain4travel/camino-messenger-bot/internal/metadata" + "google.golang.org/grpc" + "google.golang.org/protobuf/types/known/timestamppb" accommodationv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/accommodation/v1alpha" activityv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/activity/v1alpha" + bookv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/book/v1alpha" networkv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/network/v1alpha" partnerv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/partner/v1alpha" pingv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/ping/v1alpha" transportv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/transport/v1alpha" typesv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/types/v1alpha" - - "github.com/chain4travel/camino-messenger-bot/internal/metadata" - "google.golang.org/grpc" ) type partnerPlugin struct { @@ -38,6 +41,43 @@ type partnerPlugin struct { transportv1alphagrpc.TransportSearchServiceServer } +func (p *partnerPlugin) Mint(ctx context.Context, request *bookv1alpha.MintRequest) (*bookv1alpha.MintResponse, error) { + md := metadata.Metadata{} + err := md.ExtractMetadata(ctx) + if err != nil { + log.Print("error extracting metadata") + } + md.Stamp(fmt.Sprintf("%s-%s", "ext-system", "response")) + log.Printf("Responding to request: %s", md.RequestID) + + response := bookv1alpha.MintResponse{ + MintId: md.RequestID, + BookingTimestamp: ×tamppb.Timestamp{ + Seconds: time.Now().Unix(), + }, + } + grpc.SendHeader(ctx, md.ToGrpcMD()) + return &response, nil +} + +func (p *partnerPlugin) Validation(ctx context.Context, request *bookv1alpha.ValidationRequest) (*bookv1alpha.ValidationResponse, error) { + md := metadata.Metadata{} + err := md.ExtractMetadata(ctx) + if err != nil { + log.Print("error extracting metadata") + } + md.Stamp(fmt.Sprintf("%s-%s", "ext-system", "response")) + log.Printf("Responding to request: %s", md.RequestID) + + response := bookv1alpha.ValidationResponse{ + Header: nil, + ValidationId: md.RequestID, + ValidationObject: nil, + } + grpc.SendHeader(ctx, md.ToGrpcMD()) + return &response, nil +} + func (p *partnerPlugin) ActivitySearch(ctx context.Context, request *activityv1alpha.ActivitySearchRequest) (*activityv1alpha.ActivitySearchResponse, error) { md := metadata.Metadata{} err := md.ExtractMetadata(ctx) @@ -175,6 +215,8 @@ func main() { accommodationv1alphagrpc.RegisterAccommodationSearchServiceServer(grpcServer, &partnerPlugin{}) networkv1alphagrpc.RegisterGetNetworkFeeServiceServer(grpcServer, &partnerPlugin{}) partnerv1alphagrpc.RegisterGetPartnerConfigurationServiceServer(grpcServer, &partnerPlugin{}) + bookv1alphagrpc.RegisterMintServiceServer(grpcServer, &partnerPlugin{}) + bookv1alphagrpc.RegisterValidationServiceServer(grpcServer, &partnerPlugin{}) pingv1alphagrpc.RegisterPingServiceServer(grpcServer, &partnerPlugin{}) transportv1alphagrpc.RegisterTransportSearchServiceServer(grpcServer, &partnerPlugin{}) port := 55555 diff --git a/go.mod b/go.mod index 57c9dc94..5332db2e 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/chain4travel/camino-messenger-bot go 1.20 require ( - buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go v1.3.0-20240412100111-3654d6c5a4fb.2 - buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.33.0-20240412100111-3654d6c5a4fb.1 + buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go v1.3.0-20240415140348-9f62f6fadb08.2 + buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.33.0-20240415140348-9f62f6fadb08.1 github.com/ava-labs/avalanchego v1.9.16 github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e github.com/google/uuid v1.4.0 diff --git a/go.sum b/go.sum index 2a65e3fc..5ff286f0 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ -buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go v1.3.0-20240412100111-3654d6c5a4fb.2 h1:AO0mgtrYSWAJOh5hoZ3Ex0bqEcAQPdSWWIKcy64Uwqc= -buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go v1.3.0-20240412100111-3654d6c5a4fb.2/go.mod h1:hq+qoNCKEnAmrIxy0SrDnr5oLzf6SciT/p/OX2DGJuE= -buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.28.1-20240412100111-3654d6c5a4fb.4/go.mod h1:2viX8eSuMFjoDrr8x3FYytCp81PVYkdgfB68aIcGW6c= -buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.33.0-20240412100111-3654d6c5a4fb.1 h1:Law6v5UCyz7Lj7Bghj3n0gWiur6tUBfoveRieT9nfQI= -buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.33.0-20240412100111-3654d6c5a4fb.1/go.mod h1:gsLG4/oOZhDfc11a7nNX2eBty0bQvCqxP6kKBgrJLvA= +buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go v1.3.0-20240415140348-9f62f6fadb08.2 h1:rYswZ4Jy+LT3qtOXad8GMu5bEYzXx1hRlkVI34S4EsQ= +buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go v1.3.0-20240415140348-9f62f6fadb08.2/go.mod h1:MPxvfuAEZSzxey6+rEi+XOWCNWqxWmKB+fSg+BKCCc8= +buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.28.1-20240415140348-9f62f6fadb08.4/go.mod h1:2viX8eSuMFjoDrr8x3FYytCp81PVYkdgfB68aIcGW6c= +buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.33.0-20240415140348-9f62f6fadb08.1 h1:rXq08DDGNYkhWqZKHFrtcN+CQojET2n+vKNaqSIQNcE= +buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go v1.33.0-20240415140348-9f62f6fadb08.1/go.mod h1:gsLG4/oOZhDfc11a7nNX2eBty0bQvCqxP6kKBgrJLvA= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= diff --git a/internal/matrix/types.go b/internal/matrix/types.go index 10129cbf..6a5e685c 100644 --- a/internal/matrix/types.go +++ b/internal/matrix/types.go @@ -53,6 +53,14 @@ func (m *CaminoMatrixMessage) UnmarshalContent(src []byte) error { return proto.Unmarshal(src, &m.Content.RequestContent.GetPartnerConfigurationRequest) case messaging.GetPartnerConfigurationResponse: return proto.Unmarshal(src, &m.Content.ResponseContent.GetPartnerConfigurationResponse) + case messaging.MintRequest: + return proto.Unmarshal(src, &m.Content.RequestContent.MintRequest) + case messaging.MintResponse: + return proto.Unmarshal(src, &m.Content.ResponseContent.MintResponse) + case messaging.ValidationRequest: + return proto.Unmarshal(src, &m.Content.RequestContent.ValidationRequest) + case messaging.ValidationResponse: + return proto.Unmarshal(src, &m.Content.ResponseContent.ValidationResponse) case messaging.PingRequest: return proto.Unmarshal(src, &m.Content.RequestContent.PingRequest) case messaging.PingResponse: diff --git a/internal/messaging/service.go b/internal/messaging/service.go index b7aba0b6..3069635e 100644 --- a/internal/messaging/service.go +++ b/internal/messaging/service.go @@ -7,6 +7,7 @@ package messaging import ( "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/activity/v1alpha/activityv1alphagrpc" + "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/book/v1alpha/bookv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/ping/v1alpha/pingv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/transport/v1alpha/transportv1alphagrpc" networkv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/network/v1alpha" @@ -24,6 +25,8 @@ var ( _ Service = (*accommodationProductInfoService)(nil) _ Service = (*accommodationProductListService)(nil) _ Service = (*accommodationService)(nil) + _ Service = (*mintService)(nil) + _ Service = (*validationService)(nil) _ Service = (*networkService)(nil) _ Service = (*partnerService)(nil) _ Service = (*pingService)(nil) @@ -114,6 +117,39 @@ func (s accommodationService) Call(ctx context.Context, request *RequestContent, return responseContent, AccommodationSearchResponse, err } +type mintService struct { + client *bookv1alphagrpc.MintServiceClient +} + +func (m mintService) Call(ctx context.Context, request *RequestContent, opts ...grpc.CallOption) (ResponseContent, MessageType, error) { + + if &request.MintRequest == nil { + return ResponseContent{}, "", ErrInvalidMessageType + } + response, err := (*m.client).Mint(ctx, &request.MintRequest, opts...) + responseContent := ResponseContent{} + if err == nil { + responseContent.MintResponse = *response // otherwise nil pointer dereference + } + return responseContent, MintResponse, err +} + +type validationService struct { + client *bookv1alphagrpc.ValidationServiceClient +} + +func (v validationService) Call(ctx context.Context, request *RequestContent, opts ...grpc.CallOption) (ResponseContent, MessageType, error) { + if &request.ValidationRequest == nil { + return ResponseContent{}, "", ErrInvalidMessageType + } + response, err := (*v.client).Validation(ctx, &request.ValidationRequest, opts...) + responseContent := ResponseContent{} + if err == nil { + responseContent.ValidationResponse = *response // otherwise nil pointer dereference + } + return responseContent, ValidationResponse, err +} + type networkService struct { } diff --git a/internal/messaging/service_registry.go b/internal/messaging/service_registry.go index 754728db..244fab99 100644 --- a/internal/messaging/service_registry.go +++ b/internal/messaging/service_registry.go @@ -8,6 +8,7 @@ package messaging import ( "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/accommodation/v1alpha/accommodationv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/activity/v1alpha/activityv1alphagrpc" + "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/book/v1alpha/bookv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/ping/v1alpha/pingv1alphagrpc" "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/transport/v1alpha/transportv1alphagrpc" "github.com/chain4travel/camino-messenger-bot/config" @@ -52,6 +53,12 @@ func (s *ServiceRegistry) RegisterServices(requestTypes config.SupportedRequestT service = networkService{} // this service does not talk to partner plugin case GetPartnerConfigurationRequest: service = partnerService{} // this service does not talk to partner plugin + case MintRequest: + c := bookv1alphagrpc.NewMintServiceClient(s.rpcClient.ClientConn) + service = mintService{client: &c} + case ValidationRequest: + c := bookv1alphagrpc.NewValidationServiceClient(s.rpcClient.ClientConn) + service = validationService{client: &c} case PingRequest: c := pingv1alphagrpc.NewPingServiceClient(s.rpcClient.ClientConn) service = pingService{client: &c} diff --git a/internal/messaging/types.go b/internal/messaging/types.go index 2769a62c..05a53826 100644 --- a/internal/messaging/types.go +++ b/internal/messaging/types.go @@ -3,6 +3,7 @@ package messaging import ( accommodationv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/accommodation/v1alpha" activityv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/activity/v1alpha" + bookv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/book/v1alpha" networkv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/network/v1alpha" partnerv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/partner/v1alpha" pingv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/ping/v1alpha" @@ -19,6 +20,8 @@ type RequestContent struct { accommodationv1alpha.AccommodationSearchRequest networkv1alpha.GetNetworkFeeRequest partnerv1alpha.GetPartnerConfigurationRequest + bookv1alpha.MintRequest + bookv1alpha.ValidationRequest pingv1alpha.PingRequest transportv1alpha.TransportSearchRequest } @@ -30,6 +33,8 @@ type ResponseContent struct { accommodationv1alpha.AccommodationSearchResponse networkv1alpha.GetNetworkFeeResponse partnerv1alpha.GetPartnerConfigurationResponse + bookv1alpha.MintResponse + bookv1alpha.ValidationResponse pingv1alpha.PingResponse transportv1alpha.TransportSearchResponse } @@ -69,6 +74,10 @@ const ( GetNetworkFeeResponse MessageType = "GetNetworkFeeResponse" GetPartnerConfigurationRequest MessageType = "GetPartnerConfigurationRequest" GetPartnerConfigurationResponse MessageType = "GetPartnerConfigurationResponse" + MintRequest MessageType = "MintRequest" + MintResponse MessageType = "MintResponse" + ValidationRequest MessageType = "ValidationRequest" + ValidationResponse MessageType = "ValidationResponse" PingRequest MessageType = "PingRequest" PingResponse MessageType = "PingResponse" TransportSearchRequest MessageType = "TransportSearchRequest" @@ -82,6 +91,8 @@ func (mt MessageType) Category() MessageCategory { AccommodationProductInfoRequest, AccommodationProductListRequest, AccommodationSearchRequest, + MintRequest, + ValidationRequest, PingRequest, TransportSearchRequest: return Request @@ -92,6 +103,8 @@ func (mt MessageType) Category() MessageCategory { AccommodationSearchResponse, GetNetworkFeeResponse, GetPartnerConfigurationResponse, + MintResponse, + ValidationResponse, PingResponse, TransportSearchResponse: return Response @@ -131,6 +144,14 @@ func (m *Message) MarshalContent() ([]byte, error) { return proto.Marshal(&m.Content.GetPartnerConfigurationRequest) case GetPartnerConfigurationResponse: return proto.Marshal(&m.Content.GetPartnerConfigurationResponse) + case MintRequest: + return proto.Marshal(&m.Content.MintRequest) + case MintResponse: + return proto.Marshal(&m.Content.MintResponse) + case ValidationRequest: + return proto.Marshal(&m.Content.ValidationRequest) + case ValidationResponse: + return proto.Marshal(&m.Content.ValidationResponse) case PingRequest: return proto.Marshal(&m.Content.PingRequest) case PingResponse: diff --git a/internal/rpc/server/server.go b/internal/rpc/server/server.go index 46a03049..d6966581 100644 --- a/internal/rpc/server/server.go +++ b/internal/rpc/server/server.go @@ -2,6 +2,8 @@ package server import ( "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/activity/v1alpha/activityv1alphagrpc" + "buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/book/v1alpha/bookv1alphagrpc" + bookv1alpha "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/book/v1alpha" "context" "errors" "fmt" @@ -39,6 +41,8 @@ var ( _ activityv1alphagrpc.ActivitySearchServiceServer = (*server)(nil) _ networkv1alphagrpc.GetNetworkFeeServiceServer = (*server)(nil) _ partnerv1alphagrpc.GetPartnerConfigurationServiceServer = (*server)(nil) + _ bookv1alphagrpc.MintServiceServer = (*server)(nil) + _ bookv1alphagrpc.ValidationServiceServer = (*server)(nil) _ pingv1alphagrpc.PingServiceServer = (*server)(nil) _ transportv1alphagrpc.TransportSearchServiceServer = (*server)(nil) @@ -87,6 +91,8 @@ func createGrpcServerAndRegisterServices(server *server, opts ...grpc.ServerOpti accommodationv1alphagrpc.RegisterAccommodationSearchServiceServer(grpcServer, server) networkv1alphagrpc.RegisterGetNetworkFeeServiceServer(grpcServer, server) partnerv1alphagrpc.RegisterGetPartnerConfigurationServiceServer(grpcServer, server) + bookv1alphagrpc.RegisterMintServiceServer(grpcServer, server) + bookv1alphagrpc.RegisterValidationServiceServer(grpcServer, server) pingv1alphagrpc.RegisterPingServiceServer(grpcServer, server) transportv1alphagrpc.RegisterTransportSearchServiceServer(grpcServer, server) return grpcServer @@ -145,6 +151,16 @@ func (s *server) ActivitySearch(ctx context.Context, request *activityv1alpha.Ac return &response.ActivitySearchResponse, err } +func (s *server) Mint(ctx context.Context, request *bookv1alpha.MintRequest) (*bookv1alpha.MintResponse, error) { + response, err := s.processExternalRequest(ctx, messaging.MintRequest, &messaging.RequestContent{MintRequest: *request}) + return &response.MintResponse, err +} + +func (s *server) Validation(ctx context.Context, request *bookv1alpha.ValidationRequest) (*bookv1alpha.ValidationResponse, error) { + response, err := s.processExternalRequest(ctx, messaging.ValidationRequest, &messaging.RequestContent{ValidationRequest: *request}) + return &response.ValidationResponse, err +} + func (s *server) TransportSearch(ctx context.Context, request *transportv1alpha.TransportSearchRequest) (*transportv1alpha.TransportSearchResponse, error) { response, err := s.processExternalRequest(ctx, messaging.TransportSearchRequest, &messaging.RequestContent{TransportSearchRequest: *request}) return &response.TransportSearchResponse, err