commercetools / commercetools-dotnet-core-sdk

The e-commerce SDK from commercetools running on the .NET Core platform
https://docs.commercetools.com/sdk/dotnet-sdk#net-core-sdk
Apache License 2.0
10 stars 5 forks source link

Setup multiple clients with different configurations #103

Closed btastic closed 1 year ago

btastic commented 4 years ago

Hello, we are currently migrating to the new library. We are using the SDK to create CtpClients (or Client in the old version) different types of clients using different projects.

This is used in a multi-tenancy setup where different customers have different needs but we only have one backend to handle the requests.

Right now I don't see a way to setup multiple clients with different configurations (project key, client secrets and so on).

Can you point me in a direction what I need to do?

jenschude commented 4 years ago

See here for example https://github.com/commercetools/commercetools-dotnet-core-sdk/blob/master/commercetools.Sdk/IntegrationTests/commercetools.Sdk.IntegrationTests/appsettings.test.json

And this as example how to instantiate this config https://github.com/commercetools/commercetools-dotnet-core-sdk/blob/0cbc0c85b066f07b9b28f780c452b5148ff2f74f/commercetools.Sdk/IntegrationTests/commercetools.Sdk.IntegrationTests/ClientsTest.cs#L37

btastic commented 4 years ago

Oh I did not mention that we do the creation of clients during runtime. Is that a thing we can do?

jenschude commented 4 years ago

It should not be impossible but it‘s not documented and could be better supported by the SDK. For the moment take a look in the https://github.com/commercetools/commercetools-dotnet-core-sdk/blob/0cbc0c85b066f07b9b28f780c452b5148ff2f74f/commercetools.Sdk/commercetools.Sdk.DependencyInjection/DependencyInjectionSetup.cs#L40 and https://github.com/commercetools/commercetools-dotnet-core-sdk/blob/0cbc0c85b066f07b9b28f780c452b5148ff2f74f/commercetools.Sdk/commercetools.Sdk.HttpApi/DependencyInjectionSetup.cs#L54 how to setup a client manually.

jenschude commented 4 years ago

Hey Ben, can you write me an email please? I would like to discuss some options with you.

nullableobj commented 1 year ago

Hi @jenschude I am trying to have two client setups for both api and importapi, they differs only on the ApiBaseAddress https://**import**.europe-west1.gcp.commercetools.com https://**api**.europe-west1.gcp.commercetools.com

I am using both UseCommercetoolsImportApi and UseCommercetoolsApi for each, but the ApiBaseAddress always overwritten by the last called configuration. Any idea how to solve that?

jenschude commented 1 year ago

Hi @nullableobj

I think you hit the wrong repository. It should have been https://github.com/commercetools/commercetools-dotnet-core-sdk-v2 as the v2 SDK supports the Import-API

When looking into your issue I found a bug when registering the ProjectApiRoots in the service container. Already created a fix and will release it today. As a workaround it would be possible to create the ProjectApiRoots from the named clients itself like shown here:

https://github.com/commercetools/commercetools-dotnet-core-sdk-v2/blob/ebff51b286623f00903e2647bd5e0781dbca36ec/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/MultipleClientsTest.cs#L63-L71

jenschude commented 1 year ago

Fixed it in 6.4.0

nullableobj commented 1 year ago

Fixed it in 6.4.0

Thank you no it works! However, I found that commercetools.Sdk.ImportApi.Clien.ProjectApiRoot does not have support for ByProjectKeyStandalonePricesRequestBuilde and ByProjectKeyInventoriesRequestBuilder which are needed to import Standalone prices and inventories. I figured out how to do it via ByProjectKeyRequestBuilder though but it would be nice to add that support to ImportApi.Clien.ProjectApiRoot.

Thanks again!

jenschude commented 1 year ago

In case a resource is missing it should always work to use ProjectApiRoot.With() which returns the ByProjectKeyRequestBuilder. In the Java SDK we already added a test to check for missing methods in the ProjectApiRoot. Looks like we have to introduce a similar one for dotnet.