为什么healthsites.io api呼叫失败?

发布于 2025-01-31 18:42:14 字数 2387 浏览 4 评论 0 原文

我正在尝试打电话给HealthSites.io,以获取给定区域的站点。但是到目前为止,我的API呼叫已经失败了,尽管使用了用于Python电话。当我使用以下函数时:

import pandas as pd
import time
import requests
import json
import coreapi

def getHealthcare(APIkey):


    # Initialize a client & load the schema document
    client = coreapi.Client()
    schema = client.get("https://healthsites.io/api/docs/")

    # Interact with the API endpoint
    action = ["api", "v2 > facilities > list"]
    params = {
        "api-key": APIkey,
        "page": 1
        #"country": ...,
        #"extent": ,
        #"output": ...,
        #"from": ...,
        #"to": ...,
        #"flat-properties": ...,
        #"tag-format": ...,
        }
    
    result = client.action(schema, action, params=params)

    df = pd.json_normalize(result) # normalize json file into pandas
    if not df.empty: # If there ARE results, continue
        return df

print(getHealthcare('INSERT API HERE'))

我会得到以下错误/跟踪:

Traceback (most recent call last):

  File "C:\Users\username\anaconda3\lib\site-packages\coreapi\client.py", line 34, in _lookup_link
    node = node[key]

  File "C:\Users\username\anaconda3\lib\site-packages\itypes.py", line 115, in __getitem__
    return self._data[key]

KeyError: 'v2 > facilities > list'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\username\OneDrive\Documents\GitHub\asset-mappr\AssetMappr\database\National\getHealthsites.py", line 44, in <module>
    print(getHealthcare('INSERT API HERE'))

  File "C:\Users\username\OneDrive\Documents\GitHub\asset-mappr\AssetMappr\database\National\getHealthsites.py", line 38, in getHealthcare
    result = client.action(schema, action, params=params)

  File "C:\Users\username\anaconda3\lib\site-packages\coreapi\client.py", line 163, in action
    link, link_ancestors = _lookup_link(document, keys)

  File "C:\Users\username\anaconda3\lib\site-packages\coreapi\client.py", line 38, in _lookup_link
    raise exceptions.LinkLookupError(msg % (index_string, repr(key).strip('u')))

LinkLookupError: Index ['api']['v2 &gt; facilities &gt; list'] did not reference a link. Key 'v2 &gt; facilities &gt; list' was not found.

I am trying to make a call to healthsites.io to obtain sites in a given area. But so far my API calls have been failing, despite using the format listed under 'Source Code' on https://healthsites.io/api/docs/ for python calls. When I use the following function:

import pandas as pd
import time
import requests
import json
import coreapi

def getHealthcare(APIkey):


    # Initialize a client & load the schema document
    client = coreapi.Client()
    schema = client.get("https://healthsites.io/api/docs/")

    # Interact with the API endpoint
    action = ["api", "v2 > facilities > list"]
    params = {
        "api-key": APIkey,
        "page": 1
        #"country": ...,
        #"extent": ,
        #"output": ...,
        #"from": ...,
        #"to": ...,
        #"flat-properties": ...,
        #"tag-format": ...,
        }
    
    result = client.action(schema, action, params=params)

    df = pd.json_normalize(result) # normalize json file into pandas
    if not df.empty: # If there ARE results, continue
        return df

print(getHealthcare('INSERT API HERE'))

I get the following error/traceback:

Traceback (most recent call last):

  File "C:\Users\username\anaconda3\lib\site-packages\coreapi\client.py", line 34, in _lookup_link
    node = node[key]

  File "C:\Users\username\anaconda3\lib\site-packages\itypes.py", line 115, in __getitem__
    return self._data[key]

KeyError: 'v2 > facilities > list'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\username\OneDrive\Documents\GitHub\asset-mappr\AssetMappr\database\National\getHealthsites.py", line 44, in <module>
    print(getHealthcare('INSERT API HERE'))

  File "C:\Users\username\OneDrive\Documents\GitHub\asset-mappr\AssetMappr\database\National\getHealthsites.py", line 38, in getHealthcare
    result = client.action(schema, action, params=params)

  File "C:\Users\username\anaconda3\lib\site-packages\coreapi\client.py", line 163, in action
    link, link_ancestors = _lookup_link(document, keys)

  File "C:\Users\username\anaconda3\lib\site-packages\coreapi\client.py", line 38, in _lookup_link
    raise exceptions.LinkLookupError(msg % (index_string, repr(key).strip('u')))

LinkLookupError: Index ['api']['v2 > facilities > list'] did not reference a link. Key 'v2 > facilities > list' was not found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2025-02-07 18:42:14

查看 coreapi documentation ,我想您想要类似的东西:

    action = ["api", "v2", "facilities", "list"]
    params = {...}
    result = client.action(schema, action, params=params)

如果我们启用debug记录,我们可以看到这会生成外观
就像一个合理的请求:

>>> import logging
>>> logging.basicConfig(level='DEBUG')
>>> client.action(schema, ['api', 'v2', 'facilities', 'list'], params={'api-key': 'foo', 'page': 1})
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (2): healthsites.io:443
DEBUG:urllib3.connectionpool:https://healthsites.io:443 "GET /api/v2/facilities/?api-key=foo&page=1 HTTP/1.1" 502 None

Looking at the coreapi documentation, I think you want something like:

    action = ["api", "v2", "facilities", "list"]
    params = {...}
    result = client.action(schema, action, params=params)

If we enable debug logging, we can see that this generates what looks
like a reasonable request:

>>> import logging
>>> logging.basicConfig(level='DEBUG')
>>> client.action(schema, ['api', 'v2', 'facilities', 'list'], params={'api-key': 'foo', 'page': 1})
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (2): healthsites.io:443
DEBUG:urllib3.connectionpool:https://healthsites.io:443 "GET /api/v2/facilities/?api-key=foo&page=1 HTTP/1.1" 502 None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文