wso2 动态生成接口_使用模拟响应有效负载生成的wso2 API管理器中的原型API

wso2 动态生成接口

This article is an informative step by step introduction and guide on how to prototype APIs in WSO2 API Manager with the help of mock response payload generation for writing inline scripts.

本文是一个内容丰富的分步介绍,并指导您如何借助模拟响应有效载荷生成来编写WMS2 API Manager中的API原型,以编写内联脚本。

什么是API原型? (What is API Prototyping?)

This is the process through which an API publisher can deploy a mock up version that can be used to test a concept and allow consumers to test, try out and provide feedback in order to make requested changes and improvements to APIs. This allows publishers to gain as many practical insights as possible while saving up on resources such as time and effort.

通过此过程,API发布者可以部署可用于测试概念的模型版本,并允许消费者进行测试,试用并提供反馈,以便对API进行请求的更改和改进。 这使发布者可以获得尽可能多的实践见解,同时节省诸如时间和精力之类的资源。

原型API如何工作? (How does a prototype API work?)

A prototype API behaves similar to the original API but lacks functional and non-functional properties of the original component and returns static data rather than fetching it from a database or a dynamic data source.

原型API的行为与原始API相似,但是缺少原始组件的功能和非功能属性,并且返回静态数据,而不是从数据库或动态数据源获取它。

Therefore, a prototype API is an impersonation of a real API that returns pre-defined responses, response codes, headers, parameters, etc.

因此,原型API是对真实API的模仿,该API返回预定义的响应,响应代码,标头,参数等。

原型API的好处是什么? (What are the benefits of prototyping APIs?)

  • Testing — consumers can test out APIs and provide feedback regarding changes to be made/requested or bugs. This allows for quick testing and bug fixing.

    测试 -消费者可以测试API并提供有关要进行/请求的更改或错误的反馈。 这样可以进行快速测试和错误修复。

  • Practical Insights — critical implementation and usability issues can be addressed with minimal effort.

    实用见解 -只需花费很少的精力,即可解决关键的实施和可用性问题。

  • Lower barrier for developers — Front-end developers can ensure they are working with an API that will satisfy them by creating endpoints they wish to see and provide corresponding endpoint status codes, response bodies and headers. Back-end developers have the ability to iterate on the API design till they are satisfied with how it looks, they can leave the complex logic until they know how it is supposed to work.

    降低开发人员的障碍 -前端开发人员可以通过创建希望查看的端点并提供相应的端点状态代码,响应主体和标头,来确保他们使用的API可以满足他们的要求。 后端开发人员可以迭代API设计,直到对外观满意为止,他们可以保留复杂的逻辑,直到知道应该如何工作为止。

  • DevOps — Reduces amount of dependencies thus supporting DevOps.

    DevOps-减少依赖项数量,从而支持DevOps。

WSO2 API Manager如何支持API原型制作? (How does WSO2 API Manager support API Prototyping?)

The 3.1.0 release of WSO2 API Manager has quite effectively enhanced the support for prototyping API. New APIs or new versions of existing APIs can be deployed as prototypes, allowing subscribers to test APIs without subscriptions or monetisations. This gives API publishers a larger audience and a greater scope for improving APIs.

WSO2 API Manager的3.1.0版本非常有效地增强了对原型API的支持。 可以将新API或现有API的新版本作为原型部署,从而使订户无需订阅或获利即可测试API。 这使API发布者拥有更多的受众,并且有更大的改进API的空间。

上一个场景: (Previous Scenario:)

Users would be presented with an inline script which had to be edited manually for each resource. The default script requires the user to edit it from scratch in order return a response in the developer portal.

将向用户显示一个内联脚本,该脚本必须针对每个资源手动进行编辑。 默认脚本要求用户从头开始对其进行编辑,以便在开发人员门户中返回响应。

Image for post

新方案(使用API​​ Manager 3.1+版本): (New Scenario (with API Manager version 3.1+):)

Prototype implementation is now equipped with Mock Response Payload Generation which works for OAS 2.0 (fka Swagger) and OAS 3.0.

原型实现现在配备了可用于OAS 2.0(fka Swagger)和OAS 3.0的模拟响应有效负载生成。

Users can prototype an API using the built-in JavaScript engine without writing the JavaScript implementation for each resource manually. The inline scripts will be generated automatically for each response code and payload type (JSON and / or XML) according to the response body example values or response schemas defined in the API Definition/Swagger.

用户可以使用内置JavaScript引擎对API进行原型设计,而无需手动为每个资源编写JavaScript实现。 根据API定义/ Swagger中定义的响应主体示例值或响应模式,将为每种响应代码和有效负载类型(JSON和/或XML)自动生成内联脚本。

If the API definition contains mock responses for more than one response code per-resource, the payloads will be presented as multiple payloads but the “mc.setPayloadJson or mc.setPayloadXML” will only contain the lowest response code value. Furthermore, inline scripts of each resource can be further modified by users to suit the required outcome.

如果API定义包含针对每个资源多个响应代码的模拟响应,则有效负载将显示为多个有效负载,但“ mc.setPayloadJson或mc.setPayloadXML”将仅包含最低的响应代码值。 此外,用户可以进一步修改每种资源的内联脚本,以适应所需的结果。

Image for post

Once the generated mock payload scripts have been saved, the API can be deployed as a prototype and be tested in the developer portal.

保存生成的模拟有效载荷脚本后,可以将API部署为原型并在开发人员门户中进行测试。

如何使用WSO2 API Manager制作API原型? (How to Prototype an API with WSO2 API Manager ?)

Now, let’s get down to business and go through the steps of how to create a Prototype API in WSO2 API Manager.

现在,让我们开始做生意,并逐步介绍如何在WSO2 API管理器中创建原型API的步骤。

第1步—在Swagger中定义样本/模拟响应有效载荷 (Step 1 — Defining Sample/Mock Response Payloads in Swagger)

There are multiple ways in which sample response examples can be defined in Swagger (OAS 2.0) and OAS 3.0. To learn more on how to do so, follow the Swagger documentation on “Adding examples”.

可以在Swagger(OAS 2.0)和OAS 3.0中定义样本响应示例的多种方式。 要了解更多有关如何执行此操作的信息,请遵循Swagger文档中的“添加示例”。

Let’s use the Swagger Petstore example for the following steps.

让我们使用Swagger Petstore示例进行以下步骤。

https://petstore.swagger.io/v2/swagger.json

https://petstore.swagger.io/v2/swagger.json

第2步-生成和修改内联脚本 (Step 2 — Generating and Modifying Inline Scripts)

  1. Click CREATE NEW API, and click I Have an Existing REST API.

    单击“ 创建新API” ,然后单击“ 我具有现有的REST API”

Image for post

2. Upload the OpenAPI URL or OpenAPI File and click NEXT.

2.上传OpenAPI URL或OpenAPI文件,然后单击NEXT

Image for post

3. Provide the API name, context, and version. Since we are creating a prototyped API, make sure the endpoint field is empty. Thereafter, click CREATE.

3.提供API名称,上下文和版本。 由于我们正在创建原型API,因此请确保端点字段为 。 之后,点击创建

Image for post

You are then directed to the API overview page.

然后,您将被定向到API概述页面。

4. Click Endpoints to navigate to the Endpoints page.

4.单击“ 端点”以导航到“端点”页面。

5. Select Prototype Implementation as the endpoint type, and click ADD.

5.选择“ 原型实现”作为端点类型,然后单击“ 添加”

Image for post

The Endpoints page appears.

出现“端点”页面。

Image for post

6. Click and expand any of the methods that contain a sample/mock payload to view the inline script that has been generated.

6.单击并展开任何包含样本/模拟有效负载的方法,以查看已生成的内联脚本。

Image for post
var response200json = [ {
"id" : 0,
"category" : {
"id" : 0,
"name" : "string"
},
"name" : "string",
"photoUrls" : [ "string" ],
"tags" : [ {
"id" : 0,
"name" : "string"
} ],
"status" : "available"
} ]mc.setProperty('CONTENT_TYPE', 'application/json');
mc.setPayloadJSON(response200json);

The example response defined in the OpenAPI definition is set as the mock response payload. You can modify the generated inline scripts as required.

OpenAPI定义中定义的示例响应被设置为模拟响应有效负载。 您可以根据需要修改生成的内联脚本。

7. Modify the inline script for /pet/{petId}.

7.修改/pet/{petId}的内联脚本。

Let’s modify the generated inline script to suit our requirement, which is to allow the user to enter an ID of a pet and get a response corresponding to the entered ID.

让我们修改生成的内联脚本以适合我们的要求,即允许用户输入宠物的ID并获得与输入的ID对应的响应。

Image for post
// **GENERATED CODE** //var response200json = [ {       
"id" : 0,
"category" : {
"id" : 0,
"name" : "string"
},
"name" : "string",
"photoUrls" : [ "string" ],
"tags" : [ {
"id" : 0,
"name" : "string"
} ],
"status" : "available"
} ]// **MANUALLY ADDED CODE** //if (mc.getProperty('uri.var.petId') == 1) {
response200json = {
"id" : 1,
"category" : {
"id" : 1,
"name" : "Dog"
},
"name" : "doggie",
"photoUrls" : [ "string" ],
"tags" : [ {
"id" : 1,
"name" : "German Shepherd"
} ],
"status" : "available"
}
}mc.setProperty('CONTENT_TYPE', 'application/json');
mc.setPayloadJSON(response200json);

8. Click SAVE to save the API.

8.单击“ 保存”以保存API。

Image for post

Now that we’ve completed generating and modifying inline scripts, it’s time to deploy our prototype.

现在我们已经完成了生成和修改内联脚本的时间,是时候部署我们的原型了。

步骤3 —将API部署为原型 (Step 3 — Deploy the API as a Prototype)

  1. Click Lifecycle to navigate to the Lifecycle page.

    单击“ 生命周期”以导航到“生命周期”页面。

  2. Click DEPLOY AS A PROTOTYPE to deploy the API as a prototype.

    单击“ 作为原型部署”以将API部署为原型。

Image for post

After deploying the prototype, we can invoke the API and test it using the Developer Portal.

部署原型后,我们可以调用API并使用开发人员门户对其进行测试。

第4步-调用API (Step 4— Invoke the API)

  1. Click View in Dev Portal to navigate to the Developer Portal after the API is deployed.

    部署API后,在开发人员门户中单击查看以导航到开发人员门户。

  2. Click Try Out to navigate to the API Console.

    单击“ 试用”以导航到API控制台。

Image for post

3. Expand any method and click Try it out.

3.展开任何方法,然后单击“ 试用”

Image for post

Enter the value for the parameter and click Execute to invoke the API.

输入参数的值,然后单击“ 执行”以调用API。

Note that the payload that you gave as a JSON/XML output appears in the response for each respective parameter provided.

请注意,您提供的作为JSON / XML输出的有效负载会出现在响应中,分别提供所提供的每个参数。

i). For petId : " 0 " :

一世)。 对于petId : " 0 " :

Image for post

The response payload that is defined in the generated script is returned.

返回在生成的脚本中定义的响应有效负载。

Image for post

ii). For petId : " 1 " :

ii)。 对于petId : " 1 " :

Image for post

The response payload defined in the manually modified script is returned.

返回手动修改的脚本中定义的响应有效负载。

Image for post

We successfully created an API with an inline script, deployed it as a prototype, and invoked it via the integrated API Console.

我们使用内联脚本成功创建了一个API,将其部署为原型,并通过集成的API控制台调用了它。

An API can also be prototyped by moving the API to the PROTOTYPED state by changing the API lifecycle state and providing the prototype endpoints.

也可以通过更改API生命周期状态并提供原型端点,将API移至PROTOTYPED状态来对API进行原型设计。

Once the API prototype has been tested thoroughly, the final version of the prototyped API is can be released in the publisher.

一旦对API原型进行了彻底的测试,就可以在发布者中发布API原型的最终版本。

结论 (Conclusion)

As the number of businesses and organisations progress towards APIs, the Mock Payload Generation Feature has made the lives of developers much easier, by providing a DevOps friendly requirement with an efficient and effective way for prototyping APIs.

随着越来越多的企业和组织向API迈进,通过提供对DevOps友好的要求以及一种有效的API原型制作方法,模拟有效载荷生成功能使开发人员的生活变得更加轻松。

学到更多… (Learn more…)

About WSO2 API Manager :

关于WSO2 API管理器:

https://wso2.com/api-management/

https://wso2.com/api-management/

Official WSO2 Screen Cast of this feature :

此功能的官方WSO2屏幕演员:

https://www.youtube.com/watch?v=Hn2eR_h4cf0&t=266s

https://www.youtube.com/watch?v=Hn2eR_h4cf0&t=266s

Official WSO2 Documentation for this feature :

此功能的官方WSO2文档:

https://apim.docs.wso2.com/en/latest/learn/design-api/mock-api/create-a-mock-api-with-an-inline-script/#creating-a-prototype-api-with-mock-payload-generated-inline-scripts

https://apim.docs.wso2.com/zh_CN/latest/learn/design-api/mock-api/create-a-mock-api-with-an-inline-script/#creating-a-prototype-api模拟有效载荷生成的内联脚本

翻译自: https://medium.com/api-integration-essentials/prototyping-apis-in-wso2-api-manager-with-mock-response-payload-generation-dfcd3f19323f

wso2 动态生成接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值