← 返回题库
初级

设计设备资源RESTful路径规范

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd, json
    devs_csv = open_url("https://data.zuihe.com/dbd/ms-iot/state_00/devices.csv").read()
    devs = pd.read_csv(StringIO(devs_csv))
    def device_urls(device_id):
        base = f'/api/v1/devices/{device_id}'
        return {'get': f'GET {base}', 'data': f'POST {base}/data',
                'commands': f'GET {base}/commands', 'status': f'GET {base}/status'}
    for _, row in devs.head(3).iterrows():
        urls = device_urls(row['device_id'])
        print(f"{row['device_id']}({row['device_type']}):")
        for k,v in urls.items(): print(f"  {k}: {v}")

示例

输入
solve()
期望输出
DEV001(temperature):
  get: GET /api/v1/devices/DEV001
  data: POST /api/v1/devices/DEV001/data
  commands: GET /api/v1/devices/DEV001/commands
  status: GET /api/v1/devices/DEV001/status
DEV002(temperature):
  get: GET /api/v1/devices/DEV002
  data: POST /api/v1/devices/DEV002/data
  commands: GET /api/v1/devices/DEV002/commands
  status: GET /api/v1/devices/DEV002/status
DEV003(pressure):
  get: GET /api/v1/devices/DEV003
  data: POST /api/v1/devices/DEV003/data
  commands: GET /api/v1/devices/DEV003/commands
  status: GET /api/v1/devices/DEV003/status
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码