Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating new PaymentsApi client changes configuration of existing PaymentsApi client #105

Open
bryancort opened this issue May 13, 2021 · 4 comments

Comments

@bryancort
Copy link

bryancort commented May 13, 2021

PaymentsApi objects have two config dictionaries: one at the top level (apiInstance.Configuration.MerchantConfigDictionaryObj) and one inside the ApiClient (apiInstance.Configuration.ApiClient.Configuration.MerchantConfigDictionaryObj). The config inside the ApiClient is the config that is used to send requests. This config is overwritten when a new PaymentsApi is created.

Example test demonstrating this issue (remove the .txt for a real .cs file, since the github upload will not accept a .cs file):

PaymentsApiConfigTests.cs.txt

@wrightsonm
Copy link

wrightsonm commented May 18, 2021

@bryancort thank you for raising this. I have tested our use of this api and I agree with your findings. I have found two workarounds. I am using option 2.

Option 1:
Configuration.Default = null;

Set Configuration.Default to null. In this situation, the api creates a a new ApiClient for each PaymentsApi

Option 2:
Create your own api Client:

var apiClient = new ApiClient();
apiClient.RestClient = new RestSharp.RestClient("https://apitest.cybersource.com");

// create a configuration for the first PaymentsApi
var clientConfig = new CyberSource.Client.Configuration(apiClient: apiClient, merchConfigDictObj: configurationDictionary);

// first api instance is created with the correct merchant config dictionary
var apiInstance = new PaymentsApi(clientConfig);

//apiInstance has the provided merchantId value
Assert.AreEqual(apiInstance.Configuration.ApiClient.Configuration.MerchantConfigDictionaryObj["merchantID"], configurationDictionary["merchantID"]);

// create a second configuration for a second PaymentsApi
var apiClient2 = new ApiClient();
apiClient2.RestClient = new RestSharp.RestClient("https://apitest.cybersource.com");
var clientConfig2 = new CyberSource.Client.Configuration(apiClient:apiClient2,merchConfigDictObj: configurationDictionary2);

// creating the second PaymentsApi client overwrites the config values in the first PaymentsApi client with the new values from clientConfig2
var apiInstance2 = new PaymentsApi(clientConfig2);

Assert.AreEqual(apiInstance.Configuration.ApiClient.Configuration.MerchantConfigDictionaryObj["merchantID"], configurationDictionary["merchantID"]);
Assert.AreEqual(apiInstance2.Configuration.ApiClient.Configuration.MerchantConfigDictionaryObj["merchantID"], configurationDictionary2["merchantID"]);

@wrightsonm
Copy link

I have since found that:

apiClient.RestClient = new RestSharp.RestClient("https://apitest.cybersource.com");

does not work. The RestClient is overwritten each time a request is made. The url is not preserved. The url is generated by a lookup using the config dictionary and "runEnvironment"

@bryancort
Copy link
Author

Thanks for the followup @wrightsonm - that's useful to know.

For option 1, it's the top level Configuration.Default that should be set to null, correct? eg.,

// do this:
apiInstance.Configuration.Default = null;

// not this
apiInstance.Configuration.ApiClient.Configuration.Default = null;

Is that right?

@wrightsonm
Copy link

CyberSource.Client.Configuration.Default = null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants