Api 平台 - 从外部 API 返回数据 [英] Api Platform - returning data from external API

查看:33
本文介绍了Api 平台 - 从外部 API 返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 API 平台在 Elasticsearch 中显示一些数据.这很好用,但我现在正在研究另一个功能.

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.

我的应用程序需要处理需要命中端点并返回一些数据的第 3 方 API.

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

在我的应用程序中,我希望能够点击 (/api/logistics/{action} - 其中 action 是一个端点,例如登录),然后点击我的应用程序层并返回数据(第3方可重命名)

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)

对第 3 方的 API 调用很好,但我不确定如何显示响应.

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

我见过 https://api-platform.com/docs/core/data-providers/ 看起来我可以创建自定义响应.

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

我是否仍然需要创建实体/模型并使用使用我的数据提供程序的控制器配置 @ApiResource()?

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

如果是这样,那么我需要在注释中添加什么,因为我没有 id 标识符

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

我对 API Platform 还很陌生,之前我没有使用过数据提供程序功能

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

我不会存储来自 3rd 方 API 的数据,只是进行 HTTP 调用,检索响应并希望通过 Api 平台显示它

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

谢谢

推荐答案

关于数据提供者,你说的很对.但正如文档页面 一般设计注意事项 所述,您必须编写一个表示端点的输入和输出的普通旧 PHP 对象 (POPO).这是用@ApiResource 注释标记的类.此类不必使用 Doctrine ORM 或任何其他持久性系统进行映射."

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."

所以不,它不需要是一个实体,但必须有一个用@ApiResource 注释标记的类(但将它放在实体文件夹中可能有助于使 @ApiResource() 标签工作 - 或添加api/config/packages/api_platform.yaml 中类的文件夹).

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).

对于项目get"您的 POPO 需要一个 id 的端点.poperty - 或者如果只有 getter,则 getter - 必须用 @ApiProperty(identifier=true) 标记标记.通常最简单的方法是从外部 api 调用的响应中内爆/编码一些字符串,这些字符串对于响应是唯一的并且不会改变.您的数据提供者必须分解/解码 id 并使用组件进行外部 api 调用.

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.

对于帖子"操作你需要一个 datapersister 而不是 dataprovider.Apip 将初始化并填充您的 POPO 并将其传递给 datapersister,然后您可以从那里调用外部 api 并返回一个对象作为结果.如果您的对象不是相同类型的 POPO,您应该指定output"=TheOutputClass::class 或将操作放在输出类上并指定input"=TheInputClass::class(用实际类替换 TheOutputClass 或 TheInputClass姓名)

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)

对于放置"和补丁"你需要一个数据提供者、一个数据持久化者和一个 id.它们可以有不同的输入和输出类,请参阅有关 DTO 的文档.

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.

带有方法get"的集合操作;可能看起来很方便,因为您可以将任何查询字符串传递给它,但您的 CollectionDataProvider 必须返回一个可迭代对象.

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

这篇关于Api 平台 - 从外部 API 返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆