【发布时间】:2018-07-17 17:50:21
【问题描述】:
在最终游入 API 的水域时,我发现了 Postman,我的天哪,它是一个了不起的游戏规则改变者!他们有一个很酷的演示项目,名为Sushi Selector,您可以在其中将多个 API 调用链接在一起,然后将它们作为一个集合运行以创建一个应用程序,该应用程序将来自 Google 地理编码和地点搜索 API 的功能合并,然后利用 Twitter 发送结果。
他们还在series of videos on YouTube 中提供了一个演示,它们的深度和流畅度都非常出色。非常直观且易于遵循,并尝试自行扩展。我没有为 San Francisco 构建 Sushi Selector,而是为 New Haven 构建了 Barbecue Finder。但是,为 Place Search 提供的测试代码失败了。 youtube 系列中介绍的代码:
let response = pm.response.json();
let choices = [];
for (let i = 0; i < response.results.length; i++) {
let establishment = response.results[i];
if (establishment.opening_hours.open_now && establishment.rating >= 4){
choices.push({
"name": establishment.name,
"rating": establishment.rating,
"vicinity": establishment.vicinity,
});
}
}
pm.environment.set("choices", JSON.stringify(choices));
运行 GET API 调用将带回大约 10-12 个烧烤关节的响应,包括几个现在都打开的 ('opening_hours.open_now' = TRUE) 并且评分为 4 或更高的响应,包括第一个结果.
{
"html_attributions": [],
"results": [ <--------------------
{
"geometry": {
"location": {
"lat": 41.2716997,
"lng": -72.9742454
},
"viewport": {
"northeast": {
"lat": 41.27280407989272,
"lng": -72.97257402010727
},
"southwest": {
"lat": 41.27010442010728,
"lng": -72.97527367989272
}
}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id": "6506ce2dbea6328207adb53545109e024e774e1b",
"name": "Uncle Willie's Smokehouse BBQ",
"opening_hours": { <--------------------------
"open_now": true <--------------------------
},
"photos": [
{
"height": 750,
"html_attributions": [
"<a href=\"https://maps.google.com/maps/contrib/109755391798790714147/photos\">A Google User</a>"
],
"photo_reference": "CmRaAAAAuG2Sb8o07byUTTa0ifo5Ubuq-KELAfvylsoyYNQi41tr6MVyNGsukvFsofpOuBtYvLQnJJf9vR69ow82dFsuaZwrbLecx1xzqy_ds-1FT1D-Gi0rK2IyyGh_CONyl70DEhCdXE5lhHeDN1AwSBwuNx_6GhQ9iUhCG45T85ZOsc_-HPIwOuIS-Q",
"width": 1200
}
],
"place_id": "ChIJp3q4ux926IkRGembozhnAj0",
"plus_code": {
"compound_code": "72CG+M8 West Haven, Connecticut",
"global_code": "87H972CG+M8"
},
"price_level": 2,
"rating": 4.1, <--------------------------
"reference": "CmRbAAAA62c_yAa_Safw9JWbRYKjcSXdY5D-xV1XfOrT4H21ouB6OoK6d3oiqkemwqH4IYxboQmmAn1wWDGo7GnhluH2ZjQdHslYDlDrIDhXXeYiHwYMW5uEy4DFLSdLIDU7BarUEhC0bTplHAz_OIR_inV2tmqCGhRDByzFCtlu7VOOKzC4atlM4kaU6Q",
"scope": "GOOGLE",
"types": [
"restaurant",
"food",
"point_of_interest",
"establishment"
],
"vicinity": "473 Saw Mill Rd, West Haven"
},
每次我针对来自 Google NearbySearch API 的响应运行此操作时,它都会抛出错误,“评估测试脚本时出错:参考错误:open_now 未定义。”
我搜索了高低,并试图找出不同的方法来尝试获取“open_now”键,但无济于事。我找到了 Sushi Selector 代码的公开版本的测试代码,在输入我的 API 密钥并将搜索调整为 New Haven 的 BBQ 后,它也失败了,并显示相同的错误消息!
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
let choices = [];
try {
let response = pm.response.json();
// loop through all the results
for (let i = 0; i < response.results.length; i++) {
let establishment = response.results[i];
// check if each establishment is open now and rated well (you can set your own search criteria here)
// add the establishments that pass your criteria to the choices array
if (establishment.opening_hours.open_now && establishment.rating >= 4.0) {
choices.push({
"name": establishment.name,
"rating": establishment.rating,
"vicinity": establishment.vicinity
});
}
}
}
catch (error) {
throw error;
}
// set your choices as an environment variable
// remember to wrap JSON.stringify() choices since it's an array
pm.environment.set("choices", JSON.stringify(choices));
无奈之下,我将演示 Sushi Selector 中的关键字参数重置为原始的“sushi”,测试通过了!环境变量选择成功填充。 然后我尝试在我的版本中使用“寿司”,它也有效!我再次尝试将 'sushi' 替换为 'barbecue' 或 'bbq' 并且测试会失败,但它适用于 'chinese'、'japanese' 和 'brazilian'。
尽管如此,为什么这些 Postman 测试会因为关键字参数不同而失败?!为什么它只会在 'bbq' 或 'barbecue' 中失败
非常感谢任何帮助。我的头好痛。
注意:我还尝试从我的版本中的 if 语句中删除 'establishment.opening_hours.open_now' 标准,只留下评级比较,测试通过,并且填充了 'choices' 数组。我将 'establishment.opening_hours.open_now' 状态添加到 'choices' 数组(“hours”=establishment.opening_hours.open_now),效果很好,根据需要填充 TRUE 和 FALSE。仅在 If 语句中使用它,使用关键字 'bbq' 或 'barbecue' 会失败。
【问题讨论】:
-
我尝试了同样的方法,您可以在这里看到结果:pasteboard.co/HuUBVp3.png 不知道为什么您会收到此错误,因为更改关键字不会使 JavaScript 或 Postman 中的任何内容失败。你能分享你的收藏吗(删除任何 API 密钥和其他东西)?
-
要了解如何分享收藏,请访问此链接:getpostman.com/docs/v6/postman/collections/sharing_collections 只需重置环境变量等并分享收藏,我会看看。
-
似乎是一个愚蠢的 n00b 问题,我猜是这样,因为我显然是:清除所有键不会破坏我 所做 的工作吗?还是我要复制并重置变量?谢谢!
-
这很酷。我认为 J.Lin 回答得很好:)
标签: postman google-geocoding-api postman-collection-runner