43 lines
970 B
Go
43 lines
970 B
Go
package authReg
|
|
|
|
import (
|
|
"context"
|
|
er "merch-api/pkg/externalRegistration/v1"
|
|
"time"
|
|
)
|
|
|
|
type service struct {
|
|
client er.ExternalRegistrationClient
|
|
timeout time.Duration
|
|
}
|
|
|
|
func newService(c er.ExternalRegistrationClient, timeout time.Duration) *service {
|
|
return &service{
|
|
client: c,
|
|
timeout: timeout,
|
|
}
|
|
}
|
|
|
|
func (s *service) AuthenticateOrRegister(ctx context.Context, req *RegRequest) (*RegResponse, error) {
|
|
authCtx, cancel := context.WithTimeout(ctx, s.timeout)
|
|
defer cancel()
|
|
|
|
request := er.RegistrationRequest{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
ServiceCode: int32(req.BaseCode),
|
|
ServiceEndCode: int32(req.EndCode),
|
|
Secret: req.SecretHash,
|
|
Status: req.Status,
|
|
}
|
|
|
|
response, err := s.client.AuthenticateOrRegister(authCtx, &request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &RegResponse{
|
|
ServiceId: response.ServiceId,
|
|
AlreadyRegistered: response.AlreadyRegistered,
|
|
}, nil
|
|
}
|