# 下 HTTP 客户客户

Canonical URL: https://developers.iagenify.com/zh/sdk/concepts/client/
Language: zh

每个资源下有一个客户端。 它附加会话, 携带机器头, 分析答案, 并将失败变成输入错误, 然后再看它 。

Status: one instance per IaGenify · typed failures

你很少直接碰它 `api.models`, `api.storage` 其余的人都经过它。知道它做什么还是值得一页,因为它的行为就是当一个电话发生意外时你正在调试。

## 每一次电话发生什么 {#lifecycle}

| Step | 它可以做什么 |
| --- | --- |
| 1 ・ 届会 | 获得有效的 JWT,如果需要,可以刷新或交换密钥。略过少数不需要身份的电话。 |
| 2 信头 | 添加版本、机器代号以及检测到的资源。 |
| 3 机构 | JSON除非您通过了 FormData —— 那么内容- 类型将留待运行时间 。 |
| 4 计时器 | 超时后中止, 除非超时为 0 。 |
| 5 分析 | JSON 当它能够时; 否则, 原始文本, 以 { msg } 表示 。 |
| 6 失败 | 401号的重试一次, 然后输入错误, 显示状态、 代码和细节 。 |

## A. 不作答复的答复 JSON {#parsing}

客户从来不会丢给无法分析的身体。JSON 成功回来 回到 `{ msg: "…" }` 而不是,这是你会看到的,如果有什么东西 在你和 API 答案在它的位置——代理、俘虏门户、网关错误页面。

```javascript
const result = await assistant.generate({ inputs });

if (result.msg !== undefined && result.outputs === undefined) {
  // Something answered, but it was not the API
  console.error("unexpected body:", result.msg.slice(0, 200));
}
```

## 上传 {#formdata}

当身体 `FormData`,客户端没有设置 `Content-Type`。这是故意的:边界是由运行时间产生的,用手设定页眉会产生一个服务器无法分割的身体。

## 指在别的地方 {#base}

```javascript
// Explicit
const api = new IaGenify({ apiKey, baseUrl: "https://api.iagenify.com" });

// From the environment
// IAGENIFY_API_URL=https://api.iagenify.com
const api = new IaGenify({ apiKey });

api.baseUrl;   // whichever won
```

两者都没有,客户没有地址可以打电话,而且每个请求都失败 `unreachable`. 部署的单位, `baseUrl` 是首先检查的一件事: 读取 `api.baseUrl` 并且确认它 `https://api.iagenify.com`.

- [错误](/zh/sdk/concepts/errors): 三个班级 和值得分流的田地
- [超时与中止](/zh/sdk/concepts/timeouts): 电话有最后期限,没有期限

```python
result = assistant.generate(inputs=inputs)

if "msg" in result and "outputs" not in result:
    # Something answered, but it was not the API
    print("unexpected body:", str(result["msg"])[:200])
```

```python
# Explicit
api = IaGenify(api_key=api_key, base_url="https://api.iagenify.com")

# From the environment
# IAGENIFY_API_URL=https://api.iagenify.com
api = IaGenify(api_key=api_key)

api.base_url   # whichever won
```
