github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/communicator/Session.go (about)

     1  package communicator
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     6  )
     7  
     8  var (
     9  	// ErrNilEndpoint occurs when the given endpoint is nil
    10  	ErrNilEndpoint = errors.New("apiEndpoint is required")
    11  	// ErrPathEndpoint occurs when the endpoint contains a path
    12  	ErrPathEndpoint = errors.New("apiEndpoint should not contain a path")
    13  	// ErrUserInfo occurs when the endpoint contains any userinfo
    14  	ErrUserInfo = errors.New("apiEndpoint should not contain user info, query or fragment")
    15  	// ErrNilConnection occurs when a nil connection is supplied
    16  	ErrNilConnection = errors.New("connection is required")
    17  	// ErrNilAuthenticator occurs when a nil authenticator is supplied
    18  	ErrNilAuthenticator = errors.New("authenticator is required")
    19  	// ErrNilMetaDataProvider occurs when a nil metaDataProvider is supplied
    20  	ErrNilMetaDataProvider = errors.New("metaDataProvider is required")
    21  )
    22  
    23  // Session Contains the components needed to communicate with the Ingenico ePayments platform. Thread-safe.
    24  type Session struct {
    25  	apiEndpoint      *url.URL
    26  	connection       Connection
    27  	metaDataProvider *MetaDataProvider
    28  	authenticator    Authenticator
    29  }
    30  
    31  // NewSession creates a new session with the given apiEndpoint, connection and authenticator
    32  func NewSession(apiEndpoint *url.URL, connection Connection, authenticator Authenticator,
    33  	metaDataProvider *MetaDataProvider) (*Session, error) {
    34  	if apiEndpoint == nil {
    35  		return nil, ErrNilEndpoint
    36  	}
    37  	if len(apiEndpoint.Path) > 0 && apiEndpoint.Path != "/" {
    38  		return nil, ErrPathEndpoint
    39  	}
    40  	if apiEndpoint.User != nil || apiEndpoint.RawQuery != "" || apiEndpoint.Fragment != "" {
    41  		return nil, ErrUserInfo
    42  	}
    43  	if connection == nil {
    44  		return nil, ErrNilConnection
    45  	}
    46  	if authenticator == nil {
    47  		return nil, ErrNilAuthenticator
    48  	}
    49  	if metaDataProvider == nil {
    50  		return nil, ErrNilMetaDataProvider
    51  	}
    52  	return &Session{apiEndpoint: apiEndpoint,
    53  		connection:       connection,
    54  		metaDataProvider: metaDataProvider,
    55  		authenticator:    authenticator}, nil
    56  }
    57  
    58  // APIEndpoint returns the apiEndpoint of the session
    59  func (s *Session) APIEndpoint() *url.URL {
    60  	return s.apiEndpoint
    61  }
    62  
    63  // Connection returns the connection of the session
    64  func (s *Session) Connection() Connection {
    65  	return s.connection
    66  }
    67  
    68  // MetaDataProvider returns the metaDataProvider of the session
    69  func (s *Session) MetaDataProvider() *MetaDataProvider {
    70  	return s.metaDataProvider
    71  }
    72  
    73  // Authenticator returns the authenticator of the session
    74  func (s *Session) Authenticator() Authenticator {
    75  	return s.authenticator
    76  }