0
votes

I'm currently using API Platform to display some data in Elasticsearch. This works fine, but I now have another feature that I'm looking at.

My application needs to deal with a 3rd-party API that needs to hit an endpoint and return some data.

Within my app, I'd like to be able to hit (/api/logistics/{action} - where action is an endpoint, such as login) and this then hits my app layer and returns data (3rd-party can be re-named)

The API calls to the 3rd party are fine, but I'm unsure how to display the response.

I've seen https://api-platform.com/docs/core/data-providers/ which looks like i can create a custom response.

Do i still need to create an entity/model and configure the @ApiResource() with a controller that uses my Data Provider?

If so, then what do i need to add in my annotation, since I won't have an id identifier

I'm fairly new to API Platform and I've not used the Data Provider functionality before

I will not be storing the data from the 3rd party API, just doing a HTTP call, retrieving the response and hopefully displaying it via Api Platform

Thanks

1

1 Answers

0
votes

You are mosly right about the dataprovider. But as the docs page General Design Considerations states, "you have to write a plain old PHP object (POPO) representing the input and output of your endpoint. This is the class that is marked with the @ApiResource annotation. This class doesn't have to be mapped with Doctrine ORM, or any other persistence system."

So no, it does not need to be an Entity, but there must be a class marked with the @ApiResource annotation (but putting it in the Entity folder may help to make the @ApiResource() tag work - or adding the folder of your class in api/config/packages/api_platform.yaml).

For an item "get" endpoint your POPO needs an id. The poperty - or if there is only a getter, the getter - must be marked with the @ApiProperty(identifier=true) tag. Usually the easiest way to make one is by imploding/encoding some strings from the response of the external api call that together are unique for the response and will not change. Your dataprovider will have to explode/decode the id and use the components to make the external api call.

For a "post" operation you need a datapersister instead of a dataprovider. Apip will instatiate and populate your POPO and pass it to the datapersister and from there you can make the call to the external api and return an object as the result. If your object is not the same type of POPO you should specify "output"=TheOutputClass::class or put the operation on the output class and specify "input"=TheInputClass::class (replace TheOutputClass or TheInputClass by the actual class name)

For "put" and "patch" you need both a dataprovider, a datapersister and an id. They can have different input and output classes, see the docs about DTOs.

A collectionoperations with method "get" may seem convenient because you can just pass it any query string but your CollectionDataProvider must return an iterable.