【问题标题】:Combine responses of two or more APIs online在线合并两个或多个 API 的响应
【发布时间】:2016-12-23 08:16:16
【问题描述】:


我想结合两个或多个 API 的响应。我之前没有制作过 API,所以我更喜欢在线提供的东西来组合两个 API。
例如,如果一个 API 列出了加利福尼亚州的城市名称,而另一个列出了德克萨斯州的城市名称,那么我如何将它们组合在一起,以使一个 API 调用返回加利福尼亚州和德克萨斯州的城市列表?
不知道网上有没有类似的东西?喜欢任何工具或其他东西吗?
提前感谢您的帮助。
我想要以下结构或具有以下结构的 API。任何具有这种结构的 API 都可以,只是应该有一系列位置

results:
[
California :
{
county:Orange county
lat:
long:
},
{
county:santa clara county
lat:
long:
},
{
county:LA county
lat:
long:
},
{
county:long beach county
lat:
long:
},
],
Texas:
{
county:Orange county
lat:
long:
},
{
county:Orange county
lat:
long:
},
{
county:Orange county
lat:
long:
},
{
county:Orange county
lat:
long:
},
{
county:Orange county
lat:
long:
},
],
New Jersey[
{
county:Orange county
lat:
long:
}

]
]

【问题讨论】:

  • 我不认为有这样的工具。你可以结合例如在您的后端进行两次休息调用并实现您自己的休息端点(api),但这不是一个好的架构。你为什么要这样做?你有想要组合的特定 api 吗?
  • 所以你想要国家名称,经纬度?你的参数是哪个?在哪里确定您获得了多少结果?
  • 基本上我想要一个州的数组,每个州应该有一个城市或县的数组,其中有一个经纬度、名称等的字典。
  • 例如,这个 Walmart Store Locator API(api.walmartlabs.com/v1/…) 的响应包含一个位置数组。同样,我希望将多个城市合并到一个 API 中,所以我只需要从前端调用一个 API。
  • 看看here你知道如何使用REST吗?您使用哪种程序语言?

标签: swift api


【解决方案1】:

可以通过在开发控制台中使用 javascript 或在<script> 标记中向openstreetmaps 发出两个请求来完成,您也可以使用curl 或python 和requests.get,或任何语言你喜欢:

Promise.all([
    fetch("http://nominatim.openstreetmap.org/search/?city=San%20Francisco&state=California&format=json"), 
    fetch("http://nominatim.openstreetmap.org/search/?city=Austin&state=Texas&format=json")
])
.then(values => {
  return Promise.all(values.map(v=>v.text()))
})
.then(values => {
  values.map(v=>{console.log(v)})
})
.catch(e => {console.warn(e)})

输出:

"[
      {
        "place_id": "158675742",
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
        "osm_type": "relation",
        "osm_id": "111968",
        "boundingbox": [
          "37.6403143",
          "37.9298443",
          "-123.1738249",
          "-122.2817799"
        ],
        "lat": "37.7792808",
        "lon": "-122.4192362",
        "display_name": "San Francisco, San Francisco City and County, California, United States of America",
        "class": "place",
        "type": "city",
        "importance": 0.99836369596997,
        "icon": "http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png"
      }
    ]"

    "[
  {
    "place_id": "158900509",
    "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
    "osm_type": "relation",
    "osm_id": "113314",
    "boundingbox": [
      "30.0986648",
      "30.516863",
      "-97.9383829",
      "-97.5614889"
    ],
    "lat": "30.2711286",
    "lon": "-97.7436994",
    "display_name": "Austin, Travis County, Texas, United States of America",
    "class": "place",
    "type": "city",
    "importance": 0.8589403884382,
    "icon": "http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png"
  }
]"

另一种选择是使用:

来自文档的示例:

from geopy.geocoders import GeocoderDotUS
geolocator = GeocoderDotUS(format_string="%s, Cleveland OH")
address, (latitude, longitude) = geolocator.geocode("11111 Euclid Ave")
print(address, latitude, longitude)
11111 Euclid Ave, Cleveland, OH 44106 41.506784 -81.608148

对于 swift 来说,这似乎可行,但它只是一个获取请求,所以我不确定 openstreetmaps 的包装器需要有多复杂:

或参见:

【讨论】:

    猜你喜欢
    • 2022-08-23
    • 1970-01-01
    • 2021-07-26
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2020-11-04
    • 2019-09-12
    相关资源
    最近更新 更多