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

Can not check if store exists #374

Open
sommmen opened this issue Nov 7, 2024 · 8 comments
Open

Can not check if store exists #374

sommmen opened this issue Nov 7, 2024 · 8 comments

Comments

@sommmen
Copy link

sommmen commented Nov 7, 2024

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
Copy link
Author

sommmen commented Nov 7, 2024

@jenschude would you be able to assist?

@jenschude
Copy link
Contributor

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
Copy link
Author

sommmen commented Nov 7, 2024

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 sommmen closed this as completed Nov 7, 2024
@sommmen sommmen reopened this Nov 7, 2024
@sommmen
Copy link
Author

sommmen commented Nov 19, 2024

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

@jenschude
Copy link
Contributor

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

@sommmen
Copy link
Author

sommmen commented Nov 22, 2024

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
Copy link
Contributor

jenschude commented Nov 22, 2024

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
Copy link
Contributor

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

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

No branches or pull requests

2 participants