Skip to main content

PyTiehu 是铁虎停车场管理系统的 Python SDK,提供同步和异步两种调用方式,实现完整的请求签名机制,用于与铁虎停车场管理系统进行 API 交互。

Project description

py-tiehu - 铁虎停车场 API Python SDK

Python Version License

py-tiehu 是铁虎停车场管理系统的 Python SDK,提供同步和异步两种调用方式,实现完整的请求签名机制,用于与铁虎停车场管理系统进行 API 交互。

功能特性

  • ✅ 自动生成请求签名(MD5 算法)
  • ✅ 支持同步/异步 HTTP 请求
  • ✅ 自动添加公共请求参数(parkingId、timestamp、sign)
  • ✅ 灵活的客户端配置选项
  • ✅ Pydantic 响应模型支持

安装

pip install py_tiehu

快速开始

同步客户端

from py_tiehu import Pklot
from py_tiehu.utils import convert_to_status_eq_1

# 初始化客户端
pklot_inst = Pklot(
    base_url="https://isc.example.com",
    parking_id="park001",
    app_key="your_app_secret"
)

# 发送请求
response = pklot_inst.request(
    url="/api/v1/parking/query",
    json={"plateNo": "京A12345"}
)

# 处理响应
print(convert_to_status_eq_1(response))

异步客户端

import asyncio
from py_tiehu import Pklot
from py_tiehu.utils import convert_to_status_eq_1

# 初始化客户端
pklot_inst = Pklot(
    base_url="https://isc.example.com",
    parking_id="park001",
    app_key="your_app_secret"
)


# 异步发送请求
async def main():
    response = await pklot_inst.async_request(
        url="/api/v1/parking/query",
        json={"plateNo": "京A12345"}
    )
    print(convert_to_status_eq_1(response))


asyncio.run(main())

API 文档

Pklot 类

初始化参数

参数 类型 必填 说明
base_url str 停车场管理系统服务器基础 URL
parking_id str 停车场 ID
app_key str 应用密钥,用于生成请求签名
client_kwargs dict 传递给 httpx.Client 的额外配置参数

方法

方法 说明 返回值
client() 创建同步 HTTP 客户端 httpx.Client
async_client() 创建异步 HTTP 客户端 httpx.AsyncClient
signature(data) 生成请求签名 str
request(**kwargs) 发送同步请求 httpx.Response
async_request(**kwargs) 发送异步请求 httpx.Response

签名算法

签名生成步骤:

  1. 排除数据中的 appKey 字段
  2. 对剩余字段按键名进行升序排序
  3. 将排序后的字段以 key=value 格式用 & 连接
  4. 尾部拼接 appKey 的 MD5 值(大写)
  5. 对拼接后的完整字符串进行 MD5 计算并转为大写

工具函数

timestamp()

生成当前时间戳(毫秒)

from py_tiehu.utils import timestamp

ts = timestamp()  # 输出示例: 1630000000000

json_find_first(expression, data)

使用 JSONPath 表达式从数据中查找第一个匹配项

from py_tiehu.utils import json_find_first

result = json_find_first("$.data[0].name", response.json())

convert_to_status_eq_1(response)

将 HTTP 响应或字典转换为 STATUS_EQ_1 模型对象

from py_tiehu.utils import convert_to_status_eq_1

result = convert_to_status_eq_1(response)
print(result.status)  # 1
print(result.Data)  # 响应数据

响应模型

Base

所有 API 响应的基类:

class Base(BaseModel):
    status: Union[int, str]  # 错误码,1表示成功,非1表示失败
    message: Optional[str]  # 错误信息描述

STATUS_EQ_1

成功响应模型:

class STATUS_EQ_1(Base):
    status: Literal[1, "1"]  # 成功,值为1
    Data: Any  # 成功响应数据

配置说明

默认配置

{
    "base_url": "<your_base_url>",
    "verify": False,  # 禁用 SSL 证书验证
    "timeout": 120  # 超时时间 120 秒
}

自定义配置

from py_tiehu import Pklot

pklot_inst = Pklot(
    base_url="https://isc.example.com",
    parking_id="park001",
    app_key="your_app_secret",
    client_kwargs={
        "timeout": 60,
        "verify": True,
        "headers": {"X-Custom": "value"}
    }
)

示例

查询车辆信息

from py_tiehu import Pklot
from py_tiehu.utils import convert_to_status_eq_1

pklot_inst = Pklot(
    base_url="https://isc.example.com",
    parking_id="park001",
    app_key="your_app_secret",
    client_kwargs={
        "timeout": 60,
        "verify": True,
        "headers": {"X-Custom": "value"}
    }
)
response = pklot_inst.request(
    url="/api/v1/vehicle/query",
    json={
        "plateNo": "京A12345",
        "parkingId": "park001"
    }
)
print(convert_to_status_eq_1(response))

批量查询

from py_tiehu import Pklot
from py_tiehu.utils import convert_to_status_eq_1

pklot_inst = Pklot(
    base_url="https://isc.example.com",
    parking_id="park001",
    app_key="your_app_secret",
    client_kwargs={
        "timeout": 60,
        "verify": True,
        "headers": {"X-Custom": "value"}
    }
)
response = pklot_inst.request(
    url="/api/v1/vehicle/batch",
    json={
        "plateNos": ["京A12345", "京B67890"],
        "pageIndex": 1,
        "pageSize": 20
    }
)
print(convert_to_status_eq_1(response))

主页

https://gitee.com/guolei19850528/py_tiehu

许可证

MIT License

作者

Guolei174000902@qq.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_tiehu-0.1.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_tiehu-0.1.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file py_tiehu-0.1.1.tar.gz.

File metadata

  • Download URL: py_tiehu-0.1.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.11

File hashes

Hashes for py_tiehu-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fdb2db722bc74ed4db159f1dd7457aec1c773830aeebd1f81bcb6d82f1d3fd0f
MD5 9798c898168749a3f7bda5b87153576b
BLAKE2b-256 d93cff640b754d2f88c39d7bafa5c685c00d4f8365d84b0caca5c27b0ecc2118

See more details on using hashes here.

File details

Details for the file py_tiehu-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: py_tiehu-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.11

File hashes

Hashes for py_tiehu-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef1b847ccbe13825a32b801d8ffe6fc4e29661a7242a2d55d0bc5e9bdf03d5fa
MD5 4c9ee008d6528b40445c642027aba8d0
BLAKE2b-256 94cdac2b0ed1fad4f5441e032ffb97fbcaeb0a8c4cdf0a3137d0b3a344b34e3f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page