在最终游入 API 的水域时,我发现了 Postman,我的天哪,它是一个了不起的游戏规则改变者!他们有一个名为Sushi Selector的很酷的演示项目,您可以将几个 API 调用链接在一起,然后将它们作为一个集合运行以创建一个应用程序,该应用程序将来自 Google 地理编码和地方搜索 API 的功能合并,然后利用 Twitter 发送结果。
他们还在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 测试会因为关键字参数不同而失败?!为什么它只会因“烧烤”或“烧烤”而失败
任何帮助是极大的赞赏。我头疼。
注意:我还尝试从我的版本中的 if 语句中删除 'establishment.opening_hours.open_now' 标准,只留下评级比较,测试通过,并且填充了 'choices' 数组。我将 'establishment.opening_hours.open_now' 状态添加到 'choices' 数组(“hours”=establishment.opening_hours.open_now),效果很好,根据需要填充 TRUE 和 FALSE。仅在 If 语句中使用它,使用关键字 'bbq' 或 'barbecue' 会失败。