Metadata-Version: 2.4
Name: liwancai-ApiClient
Version: 1.0.10
Summary: 动态API客户端库，支持链式调用和自动方法解析
Author-email: liwancai <liwancai@example.com>
License: MIT
Keywords: api,client,rest,http,dynamic
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: requests>=2.25.0
Requires-Dist: liwancai-PyLog>=1.0.0

# ApiClient

动态API客户端库，支持链式调用和自动方法解析。

**注意**: 包在PyPI上的名称是 `liwancai-ApiClient`，但导入时使用 `ApiClient`

## 安装

```bash
pip install liwancai-ApiClient
```

## 使用

### 基本用法

```python
# 导入整个包的所有功能
from ApiClient import *

# 创建API客户端实例
client = ApiClient("https://api.example.com")

# 设置认证token
client.set_authtoken("your_token_here")

# 链式调用API
result = client.users.Get_profile(user_id=123)
print(result)
```

### 链式调用语法

ApiClient支持链式调用，自动解析方法和路径：

```python
# GET 请求：以 Get_ 开头的方法
result = client.api.users.Get_list(page=1, size=10)

# POST 请求：其他方法
result = client.api.users.create(name="张三", email="zhangsan@example.com")

# 多级路径
result = client.admin.system.logs.Get_recent(hours=24)
```

### 路径转换规则

ApiClient会自动转换链式调用的路径：

| 链式调用 | HTTP方法 | 实际URL | 说明 |
|---------|---------|---------|------|
| `client.users.Get_profile` | GET | `/users/profile` | Get_前缀表示GET方法 |
| `client.users.create` | POST | `/users/create` | 默认POST方法 |
| `client.admin.users.Get_list` | GET | `/admin/users/list` | 多级路径支持 |

### 命名转换规则

- **Group部分**（除最后一个部分外）：转为小写
- **接口名**（最后一个部分）：
  - 如果是GET请求，去掉`Get_`前缀后转为kebab-case
  - 其他请求直接转为kebab-case

示例：
- `Get_profile` → `profile`（GET请求，去掉Get_前缀）
- `createUser` → `create-user`（POST请求）
- `GetAPIList` → `api-list`（GET请求，去掉Get_前缀）

## API参考

### ApiClient类

#### 构造函数
```python
ApiClient(base_url="https://api.liwancai.cn")
```

参数：
- `base_url` (str): API基础URL，默认为liwancai API

#### 方法

##### set_authtoken(token)
设置认证token

参数：
- `token` (str): 认证token

##### __getattr__(name)
开始链式调用

参数：
- `name` (str): 路径第一部分

### APIProxy类

内部使用的代理类，负责链式调用的路径收集和方法解析。

## UserMgr 用户管理接口调用

对接 UserMgr（用户认证/RBAC 服务）时，登录接口使用 **`account`** 参数（支持手机号/邮箱/昵称/自定义账号）。

> ⚠️ **不要使用旧版的 `phone` 参数登录**——动态解析客户端按参数名转发，写 `phone=` 会导致登录失败（`phone` 仅用于注册接口）。

```python
from ApiClient import ApiClient

client = ApiClient("http://localhost:2003")

# 登录（POST /login，参数必须是 account）
resp = client.login(account="admin", password="your_password")
# resp.data.accessToken -> WebToken

# 设置 WebToken 后调用用户接口
client.set_webtoken(resp.data.accessToken)
me   = client.user.Get_me()           # GET /user/me
perm = client.user.Get_role_perm()    # GET /user/role-perm

# 管理员接口（需管理员角色）
users = client.admin.Get_user_list()  # GET /admin/user-list
logs  = client.admin.Get_login_log()  # GET /admin/login-log
```

**路径映射**（动态客户端链式调用 → UserMgr 路由）：

| 链式调用 | 方法/路径 |
|----------|-----------|
| `client.login(account=..., password=...)` | `POST /login` |
| `client.user.Get_me()` | `GET /user/me` |
| `client.user.Get_role_perm()` | `GET /user/role-perm` |
| `client.user.Post_info_update(...)` | `POST /user/info-update` |
| `client.admin.Get_user_list()` | `GET /admin/user-list` |
| `client.admin.Get_login_log()` | `GET /admin/login-log` |

> 方法名大小写规则见上文「命名转换规则」；`Post_`/`Get_` 前缀分别对应 POST/GET 方法。

## 示例

### 完整示例

```python
from ApiClient import ApiClient

# 创建客户端
client = ApiClient("https://api.example.com")
client.set_authtoken("your_jwt_token")

# 获取用户列表
users = client.users.Get_list(page=1, size=20)

# 创建新用户
new_user = client.users.create(
    name="李四",
    email="lisi@example.com",
    role="user"
)

# 获取用户详情
user_detail = client.users.Get_detail(user_id=123)

# 更新用户信息
updated_user = client.users.update(
    user_id=123,
    name="王五",
    email="wangwu@example.com"
)

# 删除用户
delete_result = client.users.delete(user_id=123)
```

### 错误处理

```python
try:
    result = client.users.Get_profile(user_id=999)
    print("请求成功:", result)
except Exception as e:
    print("请求失败:", e)
```

## 依赖

- `requests>=2.25.0` - HTTP请求库
- `liwancai-PyLog>=1.0.0` - 日志库

## 许可证

MIT License
