commercetools / commercetools-dotnet-core-sdk-v2

https://commercetools.github.io/commercetools-dotnet-core-sdk-v2/docs/html/index.html
Apache License 2.0
18 stars 13 forks source link

Can not check if store exists #374

Open sommmen opened 2 weeks ago

sommmen commented 2 weeks ago

hiya from the docs:

https://docs.commercetools.com/api/projects/stores#check-if-store-exists

You can make HEAD calls and see if a resource exists or not. Great - but the sdk automatically faults on a not found error;

   var exists = await _root.Stores()
       .WithKey("test-i-dont-exist)
       .Head()
       .SendAsync();

Always faults. I think that in the ErrorHandler we should check the http method and not throw on notfound errors when doing /head calls or something or make it configurable. Otherwise please advise how i would check if a resource exists (without a try-catch).

To be clear; exceptions are to be used in exceptional cases, and a exists or not case is normal operation, not exceptional. Such actions should not throw.

sommmen commented 2 weeks ago

@jenschude would you be able to assist?

jenschude commented 2 weeks ago

The API answers with a 404 which results in the NotFoundException.

In the Java SDK we added a NotFoundExceptionMiddleware which returns null in case of an NotFoundException. You may be able to register an additional MessageHandler to the client like this:

s.UseCommercetoolsApi(...)
    .AddHttpMessageHandler(new NotFoundExceptionHandler());
    .AddHttpMessageHandler(c => new NotFoundExceptionHandler(c.GetService<MyHelperService>())); // with addtionional DependencyInjection

I didn't tried it yet if the handler is being called before or after the ErrorHandler. Another option would be to add a new Middleware but atm the SDK is not as extendable as the Java SDK regarding middlewares.

sommmen commented 2 weeks ago

The API answers with a 404 which results in the NotFoundException.

In the Java SDK we added a NotFoundExceptionMiddleware which returns null in case of an NotFoundException. You may be able to register an additional MessageHandler to the client like this:

s.UseCommercetoolsApi(...)
    .AddHttpMessageHandler(new NotFoundExceptionHandler());
    .AddHttpMessageHandler(c => new NotFoundExceptionHandler(c.GetService<MyHelperService>())); // with addtionional DependencyInjection

I didn't tried it yet if the handler is being called before or after the ErrorHandler. Another option would be to add a new Middleware but atm the SDK is not as extendable as the Java SDK regarding middlewares.

Ok. seems like a workaround worth trying, but this should really be fixed in the sdk. Do i need to go through support to get this on the backlog or what would you recommend?

I can also push a PR with that exact change if you want, but idk if this repo is open to contributions, let me know what you think.

sommmen commented 1 week ago

@jenschude hello could you give any guidance regarding my last message? would you accept a PR or would i need a support ticket?

jenschude commented 6 days ago

The repository is open to contributors. So you are free to open a PR.

sommmen commented 5 days ago

Took a quick look, I could not get the tests to run locally;

System.AggregateException
One or more errors occurred. (Value cannot be null. (Parameter 'instance')) (The following constructor parameters did not have matching fixture data: ServiceProviderFixture serviceProviderFixture)
  Exception doesn't have a stacktrace

System.ArgumentNullException
Value cannot be null. (Parameter 'instance')
   at System.ComponentModel.DataAnnotations.ValidationContext..ctor(Object instance, IServiceProvider serviceProvider, IDictionary`2 items)
   at System.ComponentModel.DataAnnotations.ValidationContext..ctor(Object instance)
   at commercetools.Sdk.HttpApi.DependencyInjectionSetup.UseSingleClient(IServiceCollection services, IConfiguration configuration, String clientName, TokenFlow tokenFlow)
   at commercetools.Sdk.HttpApi.DependencyInjectionSetup.UseHttpApi(IServiceCollection services, IConfiguration configuration, IDictionary`2 clients)
   at Microsoft.Extensions.DependencyInjection.DependencyInjectionSetup.UseCommercetools(IServiceCollection services, IConfiguration configuration, IDictionary`2 clients, SerializationConfiguration serializationConfiguration)
   at Microsoft.Extensions.DependencyInjection.DependencyInjectionSetup.UseCommercetools(IServiceCollection services, IConfiguration configuration, String clientName, TokenFlow tokenFlow, SerializationConfiguration serializationConfiguration)
   at commercetools.Sdk.BCTest.ServiceProviderFixture..ctor(IMessageSink diagnosticMessageSink) in C:\repos\commercetools-dotnet-core-sdk-v2\commercetools.Sdk\compat\commercetools.Sdk.BCTest\ServiceProviderFixture.cs:line 36

Xunit.Sdk.TestClassException
The following constructor parameters did not have matching fixture data: ServiceProviderFixture serviceProviderFixture
  Exception doesn't have a stacktrace
jenschude commented 5 days ago

You will have to add an appsettings file to the test project

appsettings.test.Development.json

{
  "Container": "BuiltIn",
  "ClientType": "Stream",
  "Client": {
    "ClientId": "",
    "ClientSecret": "",
    "ProjectKey": ""
  },
  "MeClient": {
    "ClientId": "",
    "ClientSecret": "",
    "ProjectKey": ""
  },
  "Import": {
    "ClientId": "",
    "ClientSecret": "",
    "ProjectKey": "",
    "ApiBaseAddress": "https://import.europe-west1.gcp.commercetools.com/"
  },
  "Test2": {
    "ClientId": "",
    "ClientSecret": "",
    "ProjectKey": "test-2"
  }
}

or set up these environment variables:

      CTP_ClientType: ${{ matrix.ClientType }}
      CTP_Client__ClientId: ${{ secrets.CTP_CLIENT_ID }}
      CTP_Client__ClientSecret: ${{ secrets.CTP_CLIENT_SECRET }}
      CTP_Client__ProjectKey: ${{ secrets.CTP_PROJECT_KEY }}
      CTP_Client__Scope: ${{ secrets.CTP_SCOPE }}
      CTP_Import__ClientId: ${{ secrets.CTP_CLIENT_ID }}
      CTP_Import__ClientSecret: ${{ secrets.CTP_CLIENT_SECRET }}
      CTP_Import__ProjectKey: ${{ secrets.CTP_PROJECT_KEY }}
      CTP_Import__Scope: ${{ secrets.CTP_SCOPE }}
      CTP_Import__ApiBaseAddress: "https://import.europe-west1.gcp.commercetools.com/"
      CTP_MeClient__ClientId: ${{ secrets.CTP_MECLIENT_ID }}
      CTP_MeClient__ClientSecret: ${{ secrets.CTP_MECLIENT_SECRET }}
      CTP_MeClient__ProjectKey: ${{ secrets.CTP_MEPROJECT_KEY }}
      CTP_MeClient__Scope: ${{ secrets.CTP_MESCOPES }}
jenschude commented 5 days ago

Oh just saw that the error is coming from the BCTest project. Yes this is atm not tested. :)