Skip to content

Commit

Permalink
Add support for Mint and Validation services
Browse files Browse the repository at this point in the history
- Bump protocol version to release-5
  • Loading branch information
Kleonikos Kyriakis authored and knikos committed Apr 15, 2024
1 parent ad41481 commit 5e6219d
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 10 deletions.
2 changes: 2 additions & 0 deletions camino-messenger-bot.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ supported_request_types:
- AccommodationSearchRequest
- GetNetworkFeeRequest
- GetPartnerConfigurationRequest
- MintRequest
- ValidationRequest
- PingRequest
- TransportSearchRequest
48 changes: 45 additions & 3 deletions examples/rpc/partner-plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: &timestamppb.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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
8 changes: 8 additions & 0 deletions internal/matrix/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions internal/messaging/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
}

Expand Down
7 changes: 7 additions & 0 deletions internal/messaging/service_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}
Expand Down
21 changes: 21 additions & 0 deletions internal/messaging/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,6 +20,8 @@ type RequestContent struct {
accommodationv1alpha.AccommodationSearchRequest
networkv1alpha.GetNetworkFeeRequest
partnerv1alpha.GetPartnerConfigurationRequest
bookv1alpha.MintRequest
bookv1alpha.ValidationRequest
pingv1alpha.PingRequest
transportv1alpha.TransportSearchRequest
}
Expand All @@ -30,6 +33,8 @@ type ResponseContent struct {
accommodationv1alpha.AccommodationSearchResponse
networkv1alpha.GetNetworkFeeResponse
partnerv1alpha.GetPartnerConfigurationResponse
bookv1alpha.MintResponse
bookv1alpha.ValidationResponse
pingv1alpha.PingResponse
transportv1alpha.TransportSearchResponse
}
Expand Down Expand Up @@ -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"
Expand All @@ -82,6 +91,8 @@ func (mt MessageType) Category() MessageCategory {
AccommodationProductInfoRequest,
AccommodationProductListRequest,
AccommodationSearchRequest,
MintRequest,
ValidationRequest,
PingRequest,
TransportSearchRequest:
return Request
Expand All @@ -92,6 +103,8 @@ func (mt MessageType) Category() MessageCategory {
AccommodationSearchResponse,
GetNetworkFeeResponse,
GetPartnerConfigurationResponse,
MintResponse,
ValidationResponse,
PingResponse,
TransportSearchResponse:
return Response
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions internal/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e6219d

Please sign in to comment.