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

     1  // This class was auto-generated from the API references found at
     2  // https://epayments-api.developer-ingenico.com/
     3  
     4  package connectsdk
     5  
     6  import (
     7  	"encoding/base64"
     8  
     9  	"github.com/Ingenico-ePayments/connect-sdk-go/communicator"
    10  	"github.com/Ingenico-ePayments/connect-sdk-go/internal/apiresource"
    11  	"github.com/Ingenico-ePayments/connect-sdk-go/logging"
    12  	"github.com/Ingenico-ePayments/connect-sdk-go/logging/obfuscation"
    13  	"github.com/Ingenico-ePayments/connect-sdk-go/merchant"
    14  )
    15  
    16  // APIVersion is the version of the current SDK
    17  const APIVersion = "v1"
    18  
    19  // Client is the Ingenico ePayments platform client. Thread-safe.
    20  //
    21  // This client and all its child clients are bound to one specific value for the X-GCS-ClientMetaInfo header.
    22  // To get a new client with a different header value, use WithClientMetaInfo.
    23  type Client struct {
    24  	apiResource *apiresource.APIResource
    25  }
    26  
    27  // WithClientMetaInfo returns a new Client which uses the passed meta data for the X-GCS-ClientMetaInfo header.
    28  // - clientMetaInfo is a JSON string containing the meta data for the client
    29  // - can give an error if the given clientMetaInfo is not a valid JSON string
    30  func (c *Client) WithClientMetaInfo(clientMetaInfo string) (*Client, error) {
    31  	if len(c.apiResource.ClientMetaInfo()) == 0 && len(clientMetaInfo) == 0 {
    32  		return c, nil
    33  	} else if len(clientMetaInfo) == 0 {
    34  		return internalNewClient(c.apiResource.Communicator(), "")
    35  	} else {
    36  		// Checking to see if this is valid JSON (no JSON parse errors)
    37  		var testMap map[string]interface{}
    38  
    39  		err := c.apiResource.Communicator().Marshaller().Unmarshal(clientMetaInfo, &testMap)
    40  
    41  		if err != nil {
    42  			return nil, err
    43  		}
    44  
    45  		clientMetaInfoBase64 := base64.StdEncoding.EncodeToString([]byte(clientMetaInfo))
    46  
    47  		if clientMetaInfoBase64 == c.apiResource.ClientMetaInfo() {
    48  			return c, nil
    49  		}
    50  
    51  		return internalNewClient(c.apiResource.Communicator(), clientMetaInfoBase64)
    52  	}
    53  }
    54  
    55  // SetBodyObfuscator sets the body obfuscator to use.
    56  func (c *Client) SetBodyObfuscator(bodyObfuscator obfuscation.BodyObfuscator) {
    57  	c.apiResource.Communicator().SetBodyObfuscator(bodyObfuscator)
    58  }
    59  
    60  // SetHeaderObfuscator sets the header obfuscator to use.
    61  func (c *Client) SetHeaderObfuscator(headerObfuscator obfuscation.HeaderObfuscator) {
    62  	c.apiResource.Communicator().SetHeaderObfuscator(headerObfuscator)
    63  }
    64  
    65  // EnableLogging turns on logging using the given communicator logger.
    66  func (c *Client) EnableLogging(communicatorLogger logging.CommunicatorLogger) {
    67  	c.apiResource.Communicator().EnableLogging(communicatorLogger)
    68  }
    69  
    70  // DisableLogging turns off logging.
    71  func (c *Client) DisableLogging() {
    72  	c.apiResource.Communicator().DisableLogging()
    73  }
    74  
    75  // Close calls the internal closer of the communicator
    76  func (c *Client) Close() error {
    77  	return c.apiResource.Communicator().Close()
    78  }
    79  
    80  // Merchant represents the resource /{merchantId}
    81  func (c *Client) Merchant(merchantID string) *merchant.Client {
    82  	return merchant.NewClient(c.apiResource, map[string]string{
    83  		"merchantId": merchantID,
    84  	})
    85  }
    86  
    87  // NewClient creates a new Client with the given communicator
    88  func NewClient(communicator *communicator.Communicator) (client *Client, err error) {
    89  	client, err = internalNewClient(communicator, "")
    90  
    91  	return
    92  }
    93  
    94  func internalNewClient(communicator *communicator.Communicator, clientMetaInfo string) (client *Client, err error) {
    95  	apiResource, apiErr := apiresource.NewAPIResource(communicator, clientMetaInfo, nil)
    96  
    97  	if apiErr != nil {
    98  		err = apiErr
    99  
   100  		return
   101  	}
   102  
   103  	client = &Client{apiResource}
   104  
   105  	return
   106  }