Open Closed

Consume external REST API to get data #2785


User avatar
0
jhsanc created
  • ABP Framework version: v5.2.0-rc.2
  • UI type: Blazor Wasm
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

I need get data from external API for example: https://myapi.com/requestid/{id}

I need know if can I use HttpClient in HttpApi in DDD to do this, or exists any best practice to do this integration to my project, finally I need change data from my blazor pages. I'm using Blazor Wasm DDD tired solution, then I need know the best practice to do that. And if you have an example thats great.

Thank you,


4 Answer(s)
  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    If your external API is not abp module that implements an Http.Api.Client layer, you can make http requests using IHttpClientFactory and deserialize manually.

  • User Avatar
    0
    jhsanc created

    If your external API is not abp module that implements an Http.Api.Client layer, you can make http requests using IHttpClientFactory and deserialize manually.

    Thank you gterdem, but which layer can I implement IHttpClientFactory?, I already use Http.Api.Client layer but blazor display this error:

    Given type is not an ABP module:

    Then can I use Application Layer directly, to implement this class?

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Http.Api.Client layer is the short-hand api client for your module/application, not a layer for other clients to be injected or used.

    Http.Api.Client use scenario:
    1. You want to use the AbpAppX application service in your AbpAppY.
    2. You add the AbpAppX.HttpApi.Client to your AbpAppY.Web (or Api.Host) layer and configure RemoteService at appsettings of AbpAppY (indicates that it will look for the sevice implementation in the remote service).
    3. You can add AbpAppX.Application.Contracts to your AbpAppY.Application layer so that you are available to use the X application services in your AbpAppY application services.

    These steps saves you from creating http clients from factories, serializing/deseralizing dtos and provides you hard-coded application service contract (IAbpAppXApplicationService) you can use without bothering to write wrappers for service end-points etc.

    External service scenario:

    Now you have an external http api service which is not Abp module/application but you want to use it in your Abp application naturally. Abp is layered application so your question is, which is the question of the era: Where do I put the code?

    My answer to that is, it depends.

    • If you will use the external service to combine with your application service dtos to show some view data, implement IHttpClientFactory in the Web layer. This will make your presentation layer dependent on external service.
    • If you want to use the external service in your application logic and call external service in application services, then implement it in the Application layer. This will make your application and presentation layers dependent on external service.
    • If you want to use the external service in your domain logic and call external services in the domain services, then implement in the Domain layer. This will make your domain, application, and presentation layers dependent on external service.

    I wanted to point out the dependency graph. You need to decide the dependency level for your application to an external service.

  • User Avatar
    0
    jhsanc created

    Thank you gterdem, that's really good.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09