Skip to content

Commit

Permalink
Add services AccommodationProductInfo and AccommodationProductList (#6)
Browse files Browse the repository at this point in the history
- fix wrong flag mapping response_timeout

Co-authored-by: Kleonikos Kyriakis <[email protected]>
  • Loading branch information
knikos and Kleonikos Kyriakis authored Feb 12, 2024
1 parent 0f4254f commit 2d69718
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 21 deletions.
2 changes: 2 additions & 0 deletions camino-messenger-bot.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ partner_plugin_ca_file: ca-cert.pem
response_timeout: 3000
supported_request_types:
- ActivitySearchRequest
- AccommodationProductInfoRequest
- AccommodationProductListRequest
- AccommodationSearchRequest
- GetNetworkFeeRequest
- GetPartnerConfigurationRequest
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type PartnerPluginConfig struct {
CACertFile string `mapstructure:"partner_plugin_ca_file"`
}
type ProcessorConfig struct {
Timeout int `mapstructure:"messenger_timeout"` // in milliseconds
Timeout int `mapstructure:"response_timeout"` // in milliseconds
}
type Config struct {
AppConfig `mapstructure:",squash"`
Expand Down
2 changes: 1 addition & 1 deletion config/flag_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ const (
PartnerPluginPortKey = "partner_plugin_port"
PartnerPluginUnencryptedKey = "partner_plugin_unencrypted"
PartnerPluginCAFileKey = "partner_plugin_ca_file"
MessengerTimeoutKey = "messenger_timeout"
MessengerTimeoutKey = "response_timeout"
SupportedRequestTypesKey = "supported_request_types"
)
33 changes: 33 additions & 0 deletions examples/rpc/partner-plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (

type partnerPlugin struct {
activityv1alphagrpc.ActivitySearchServiceServer
accommodationv1alphagrpc.AccommodationProductInfoServiceServer
accommodationv1alphagrpc.AccommodationProductListServiceServer
accommodationv1alphagrpc.AccommodationSearchServiceServer
networkv1alphagrpc.GetNetworkFeeServiceServer
partnerv1alphagrpc.GetPartnerConfigurationServiceServer
Expand All @@ -52,7 +54,36 @@ func (p *partnerPlugin) ActivitySearch(ctx context.Context, request *activityv1a
grpc.SendHeader(ctx, md.ToGrpcMD())
return &response, nil
}
func (p *partnerPlugin) AccommodationProductInfo(ctx context.Context, request *accommodationv1alpha.AccommodationProductInfoRequest) (*accommodationv1alpha.AccommodationProductInfoResponse, 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 := accommodationv1alpha.AccommodationProductInfoResponse{
Properties: []*accommodationv1alpha.PropertyExtendedInfo{{PaymentType: "cash"}},
}
grpc.SendHeader(ctx, md.ToGrpcMD())
return &response, nil
}
func (p *partnerPlugin) AccommodationProductList(ctx context.Context, request *accommodationv1alpha.AccommodationProductListRequest) (*accommodationv1alpha.AccommodationProductListResponse, 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 := accommodationv1alpha.AccommodationProductListResponse{
Properties: []*accommodationv1alpha.Property{{Name: "Hotel"}},
}
grpc.SendHeader(ctx, md.ToGrpcMD())
return &response, nil
}
func (p *partnerPlugin) AccommodationSearch(ctx context.Context, request *accommodationv1alpha.AccommodationSearchRequest) (*accommodationv1alpha.AccommodationSearchResponse, error) {
md := metadata.Metadata{}
err := md.ExtractMetadata(ctx)
Expand Down Expand Up @@ -140,6 +171,8 @@ func (p *partnerPlugin) TransportSearch(ctx context.Context, request *transportv
func main() {
grpcServer := grpc.NewServer()
activityv1alphagrpc.RegisterActivitySearchServiceServer(grpcServer, &partnerPlugin{})
accommodationv1alphagrpc.RegisterAccommodationProductInfoServiceServer(grpcServer, &partnerPlugin{})
accommodationv1alphagrpc.RegisterAccommodationProductListServiceServer(grpcServer, &partnerPlugin{})
accommodationv1alphagrpc.RegisterAccommodationSearchServiceServer(grpcServer, &partnerPlugin{})
networkv1alphagrpc.RegisterGetNetworkFeeServiceServer(grpcServer, &partnerPlugin{})
partnerv1alphagrpc.RegisterGetPartnerConfigurationServiceServer(grpcServer, &partnerPlugin{})
Expand Down
8 changes: 8 additions & 0 deletions internal/matrix/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ func (m *CaminoMatrixMessage) UnmarshalContent(src []byte) error {
return proto.Unmarshal(src, &m.Content.RequestContent.ActivitySearchRequest)
case messaging.ActivitySearchResponse:
return proto.Unmarshal(src, &m.Content.ResponseContent.ActivitySearchResponse)
case messaging.AccommodationProductInfoRequest:
return proto.Unmarshal(src, &m.Content.RequestContent.AccommodationProductInfoRequest)
case messaging.AccommodationProductInfoResponse:
return proto.Unmarshal(src, &m.Content.ResponseContent.AccommodationProductInfoResponse)
case messaging.AccommodationProductListRequest:
return proto.Unmarshal(src, &m.Content.RequestContent.AccommodationProductListRequest)
case messaging.AccommodationProductListResponse:
return proto.Unmarshal(src, &m.Content.ResponseContent.AccommodationProductListResponse)
case messaging.AccommodationSearchRequest:
return proto.Unmarshal(src, &m.Content.RequestContent.AccommodationSearchRequest)
case messaging.AccommodationSearchResponse:
Expand Down
2 changes: 1 addition & 1 deletion internal/messaging/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (p *processor) Respond(msg Message) error {
var header grpc_metadata.MD
response, msgType, err := service.Call(ctx, &msg.Content.RequestContent, grpc.Header(&header))
if err != nil {
return err
return err //TODO handle error and return a response message
}

err = md.FromGrpcMD(header)
Expand Down
31 changes: 31 additions & 0 deletions internal/messaging/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

var (
_ Service = (*activityService)(nil)
_ Service = (*accommodationProductInfoService)(nil)
_ Service = (*accommodationProductListService)(nil)
_ Service = (*accommodationService)(nil)
_ Service = (*networkService)(nil)
_ Service = (*partnerService)(nil)
Expand All @@ -35,6 +37,13 @@ type Service interface {
type activityService struct {
client *activityv1alphagrpc.ActivitySearchServiceClient
}
type accommodationProductInfoService struct {
client *accommodationv1alphagrpc.AccommodationProductInfoServiceClient
}
type accommodationProductListService struct {
client *accommodationv1alphagrpc.AccommodationProductListServiceClient
}

type accommodationService struct {
client *accommodationv1alphagrpc.AccommodationSearchServiceClient
}
Expand All @@ -49,6 +58,28 @@ type transportService struct {
client *transportv1alphagrpc.TransportSearchServiceClient
}

func (a accommodationProductInfoService) Call(ctx context.Context, request *RequestContent, opts ...grpc.CallOption) (ResponseContent, MessageType, error) {
if &request.AccommodationProductInfoRequest == nil {
return ResponseContent{}, "", ErrInvalidMessageType
}
response, err := (*a.client).AccommodationProductInfo(ctx, &request.AccommodationProductInfoRequest, opts...)
responseContent := ResponseContent{}
if err == nil {
responseContent.AccommodationProductInfoResponse = *response // otherwise nil pointer dereference
}
return responseContent, AccommodationProductInfoResponse, err
}
func (a accommodationProductListService) Call(ctx context.Context, request *RequestContent, opts ...grpc.CallOption) (ResponseContent, MessageType, error) {
if &request.AccommodationProductListRequest == nil {
return ResponseContent{}, "", ErrInvalidMessageType
}
response, err := (*a.client).AccommodationProductList(ctx, &request.AccommodationProductListRequest, opts...)
responseContent := ResponseContent{}
if err == nil {
responseContent.AccommodationProductListResponse = *response // otherwise nil pointer dereference
}
return responseContent, AccommodationProductListResponse, err
}
func (s activityService) Call(ctx context.Context, request *RequestContent, opts ...grpc.CallOption) (ResponseContent, MessageType, error) {
if &request.ActivitySearchRequest == nil {
return ResponseContent{}, "", ErrInvalidMessageType
Expand Down
6 changes: 6 additions & 0 deletions internal/messaging/service_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func (s *ServiceRegistry) RegisterServices(requestTypes config.SupportedRequestT
case ActivitySearchRequest:
c := activityv1alphagrpc.NewActivitySearchServiceClient(s.rpcClient.ClientConn)
service = activityService{client: &c}
case AccommodationProductInfoRequest:
c := accommodationv1alphagrpc.NewAccommodationProductInfoServiceClient(s.rpcClient.ClientConn)
service = accommodationProductInfoService{client: &c}
case AccommodationProductListRequest:
c := accommodationv1alphagrpc.NewAccommodationProductListServiceClient(s.rpcClient.ClientConn)
service = accommodationProductListService{client: &c}
case AccommodationSearchRequest:
c := accommodationv1alphagrpc.NewAccommodationSearchServiceClient(s.rpcClient.ClientConn)
service = accommodationService{client: &c}
Expand Down
44 changes: 32 additions & 12 deletions internal/messaging/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

type RequestContent struct {
activityv1alpha.ActivitySearchRequest
accommodationv1alpha.AccommodationProductInfoRequest
accommodationv1alpha.AccommodationProductListRequest
accommodationv1alpha.AccommodationSearchRequest
networkv1alpha.GetNetworkFeeRequest
partnerv1alpha.GetPartnerConfigurationRequest
Expand All @@ -21,6 +23,8 @@ type RequestContent struct {
}
type ResponseContent struct {
activityv1alpha.ActivitySearchResponse
accommodationv1alpha.AccommodationProductInfoResponse
accommodationv1alpha.AccommodationProductListResponse
accommodationv1alpha.AccommodationSearchResponse
networkv1alpha.GetNetworkFeeResponse
partnerv1alpha.GetPartnerConfigurationResponse
Expand Down Expand Up @@ -50,28 +54,36 @@ const (

// message types

ActivitySearchRequest MessageType = "ActivitySearchRequest"
ActivitySearchResponse MessageType = "ActivitySearchResponse"
AccommodationSearchRequest MessageType = "AccommodationSearchRequest"
AccommodationSearchResponse MessageType = "AccommodationSearchResponse"
GetNetworkFeeRequest MessageType = "GetNetworkFeeRequest"
GetNetworkFeeResponse MessageType = "GetNetworkFeeResponse"
GetPartnerConfigurationRequest MessageType = "GetPartnerConfigurationRequest"
GetPartnerConfigurationResponse MessageType = "GetPartnerConfigurationResponse"
PingRequest MessageType = "PingRequest"
PingResponse MessageType = "PingResponse"
TransportSearchRequest MessageType = "TransportSearchRequest"
TransportSearchResponse MessageType = "TransportSearchResponse"
ActivitySearchRequest MessageType = "ActivitySearchRequest"
ActivitySearchResponse MessageType = "ActivitySearchResponse"
AccommodationProductInfoRequest MessageType = "AccommodationProductInfoRequest"
AccommodationProductInfoResponse MessageType = "AccommodationProductInfoResponse"
AccommodationProductListRequest MessageType = "AccommodationProductListRequest"
AccommodationProductListResponse MessageType = "AccommodationProductListResponse"
AccommodationSearchRequest MessageType = "AccommodationSearchRequest"
AccommodationSearchResponse MessageType = "AccommodationSearchResponse"
GetNetworkFeeRequest MessageType = "GetNetworkFeeRequest"
GetNetworkFeeResponse MessageType = "GetNetworkFeeResponse"
GetPartnerConfigurationRequest MessageType = "GetPartnerConfigurationRequest"
GetPartnerConfigurationResponse MessageType = "GetPartnerConfigurationResponse"
PingRequest MessageType = "PingRequest"
PingResponse MessageType = "PingResponse"
TransportSearchRequest MessageType = "TransportSearchRequest"
TransportSearchResponse MessageType = "TransportSearchResponse"
)

func (mt MessageType) Category() MessageCategory {
switch mt {
case ActivitySearchRequest,
AccommodationProductInfoRequest,
AccommodationProductListRequest,
AccommodationSearchRequest,
PingRequest,
TransportSearchRequest:
return Request
case ActivitySearchResponse,
AccommodationProductInfoResponse,
AccommodationProductListResponse,
AccommodationSearchResponse,
GetNetworkFeeResponse,
GetPartnerConfigurationResponse,
Expand All @@ -90,6 +102,14 @@ func (m *Message) MarshalContent() ([]byte, error) {
return proto.Marshal(&m.Content.ActivitySearchRequest)
case ActivitySearchResponse:
return proto.Marshal(&m.Content.ActivitySearchResponse)
case AccommodationProductInfoRequest:
return proto.Marshal(&m.Content.AccommodationProductInfoRequest)
case AccommodationProductInfoResponse:
return proto.Marshal(&m.Content.AccommodationProductInfoResponse)
case AccommodationProductListRequest:
return proto.Marshal(&m.Content.AccommodationProductListRequest)
case AccommodationProductListResponse:
return proto.Marshal(&m.Content.AccommodationProductListResponse)
case AccommodationSearchRequest:
return proto.Marshal(&m.Content.AccommodationSearchRequest)
case AccommodationSearchResponse:
Expand Down
26 changes: 20 additions & 6 deletions internal/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ import (
var (
_ Server = (*server)(nil)

_ accommodationv1alphagrpc.AccommodationSearchServiceServer = (*server)(nil)
_ activityv1alphagrpc.ActivitySearchServiceServer = (*server)(nil)
_ networkv1alphagrpc.GetNetworkFeeServiceServer = (*server)(nil)
_ partnerv1alphagrpc.GetPartnerConfigurationServiceServer = (*server)(nil)
_ pingv1alphagrpc.PingServiceServer = (*server)(nil)
_ transportv1alphagrpc.TransportSearchServiceServer = (*server)(nil)
_ accommodationv1alphagrpc.AccommodationProductInfoServiceServer = (*server)(nil)
_ accommodationv1alphagrpc.AccommodationProductListServiceServer = (*server)(nil)
_ accommodationv1alphagrpc.AccommodationSearchServiceServer = (*server)(nil)
_ activityv1alphagrpc.ActivitySearchServiceServer = (*server)(nil)
_ networkv1alphagrpc.GetNetworkFeeServiceServer = (*server)(nil)
_ partnerv1alphagrpc.GetPartnerConfigurationServiceServer = (*server)(nil)
_ pingv1alphagrpc.PingServiceServer = (*server)(nil)
_ transportv1alphagrpc.TransportSearchServiceServer = (*server)(nil)

errMissingRecipient = errors.New("missing recipient")
)
Expand Down Expand Up @@ -78,6 +80,8 @@ func NewServer(cfg *config.RPCServerConfig, logger *zap.SugaredLogger, processor
func createGrpcServerAndRegisterServices(server *server, opts ...grpc.ServerOption) *grpc.Server {
grpcServer := grpc.NewServer(opts...)
activityv1alphagrpc.RegisterActivitySearchServiceServer(grpcServer, server)
accommodationv1alphagrpc.RegisterAccommodationProductInfoServiceServer(grpcServer, server)
accommodationv1alphagrpc.RegisterAccommodationProductListServiceServer(grpcServer, server)
accommodationv1alphagrpc.RegisterAccommodationSearchServiceServer(grpcServer, server)
networkv1alphagrpc.RegisterGetNetworkFeeServiceServer(grpcServer, server)
partnerv1alphagrpc.RegisterGetPartnerConfigurationServiceServer(grpcServer, server)
Expand All @@ -99,6 +103,16 @@ func (s *server) Stop() {
s.grpcServer.Stop()
}

func (s *server) AccommodationProductInfo(ctx context.Context, request *accommodationv1alpha.AccommodationProductInfoRequest) (*accommodationv1alpha.AccommodationProductInfoResponse, error) {
response, err := s.processExternalRequest(ctx, messaging.AccommodationProductInfoRequest, &messaging.RequestContent{AccommodationProductInfoRequest: *request})
return &response.AccommodationProductInfoResponse, err
}

func (s *server) AccommodationProductList(ctx context.Context, request *accommodationv1alpha.AccommodationProductListRequest) (*accommodationv1alpha.AccommodationProductListResponse, error) {
response, err := s.processExternalRequest(ctx, messaging.AccommodationProductListRequest, &messaging.RequestContent{AccommodationProductListRequest: *request})
return &response.AccommodationProductListResponse, err
}

func (s *server) AccommodationSearch(ctx context.Context, request *accommodationv1alpha.AccommodationSearchRequest) (*accommodationv1alpha.AccommodationSearchResponse, error) {
response, err := s.processExternalRequest(ctx, messaging.AccommodationSearchRequest, &messaging.RequestContent{AccommodationSearchRequest: *request})
return &response.AccommodationSearchResponse, err //TODO set specific errors according to https://grpc.github.io/grpc/core/md_doc_statuscodes.html ?
Expand Down

0 comments on commit 2d69718

Please sign in to comment.