> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kasoftware.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 示例

> 知行之桥中常见的系统 API 用例，需要组合多个 API 请求，包括 ArcScript 集成。

export const siteNameShort = "知行之桥";

本节列举了一些需要组合 API 请求来完成的常见用例。

## 向工作流中插入文件

POST 到 **Files** 资源会将文件添加到特定端口的 Send、Receive 或 Sent 文件夹。要将文件插入到工作流中，请将该文件 POST 到应处理该文件的第一个端口的 **Send** 文件夹。例如：

```xml theme={null}
POST http://mydomain.com:8001.com/api.rsc/files
```

```json theme={null}
{
    "ConnectorId":"myConnector",
    "Folder":"Send",
    "Filename":"test.txt",
    "Content":"VGhpcyBpcyBhIHRlc3Qu"
}
```

请注意，**Content** 参数包含文件的 base64 编码内容。

如果目标端口启用了**发送自动化**，则文件会自动被处理。否则，需要额外调用 **sendFile** 函数，指示端口处理该文件。例如：

```xml theme={null}
POST http://mydomain.com:8001/api.rsc/sendFile
```

```json theme={null}
{
  "ConnectorId":"myConnector",
  "File":"test.txt"
}
```

## 检索特定事务的日志文件

**getTransactionLogs** 函数用于检索特定操作的日志文件。调用此函数需要知道相关事务的 MessageId。可以先查询 **Transactions** 资源来找到 MessageId（Transactions 资源返回有关事务的元数据，但不返回事务日志文件本身）。

对 **Transactions** 资源的 GET 查询应包含识别特定事务所需的任何过滤器，除非结果集可以由其他进程解析，以找到所需事务的 MessageId。例如：

```xml theme={null}
GET http://mydomain.com:8001/api.rsc/transactions(connectorId='myAS2Connector',Filename='myFile.edi')
```

响应的 JSON 正文将包含特定事务的 MessageId。在 **getTransactionLogs** 调用中使用此 MessageId，以及其他必需参数 **ConnectorId** 和 **Direction**（*Send* 或 *Receive*）。**IncludeContent** 参数应设置为 *True*，以便在响应中包含日志文件内容。例如：

```xml theme={null}
POST http://mydomain.com:8001/api.rsc/getTransactionLogs
```

```json theme={null}
{
 	"ConnectorId":"myConnector",
 	"Direction":"Send",
 	"MessageId":"message_id_from_earlier",
 	"IncludeContent":"True"
}
```

响应的 **Content** 参数将保存 base64 编码的日志文件内容。

## 从 {siteNameShort}Script 调用系统 API

可以使用 {siteNameShort}Script 向系统 API 发送 **arc:call** 命令。每个调用必须以 `api.rsc/` 开头。

此功能具有以下限制：

* 不支持 OData 查询语法。
* 不应用速率限制器。
* 如果脚本直接从管理控制台执行，则用户将是当前登录用户。
* 如果脚本由自动化服务执行，则审计中的 IP 地址将为 127.0.0.1。

### 检索端口

以下示例是一个独立的 **arc:call** 命令，用于检索 **connectors** [资源](./admin-api-resources)：

```xml theme={null}
<arc:call op="api.rsc/connectors" httpmethod="get" authtoken="6o6B3m4r3F8z6m4R3d1k" out="o">
```

此命令由以下组成部分构成：

| 组成部分                                 | 描述                                                                                                                      |
| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- |
| **op="api.rsc/connectors"**          | 设置 **op**（操作）[参数](../scripting/keyword-reference/op-arc-call#parameters)，以调用系统 API（使用 `api.rsc/`）并检索 **connectors** 资源。 |
| **httpmethod="get"**                 | 将命令指定为 **GET** 命令。                                                                                                      |
| **authtoken="6o6B3m4r3F8z6m4R3d1k"** | 将指定令牌传递给 **authtoken** 参数。可以在**设置**页面的**用户**部分获取。                                                                       |
| **out="o"**                          | 将命令返回的端口分配给 **o**。                                                                                                      |

以下 {siteNameShort}Script 代码使用此 **arc:call** 命令创建所有现有端口的列表：

```xml theme={null}
<!--Initialize item for output and filename-->
<arc:set attr="result.data" value=""/>
<arc:set attr="result.filename" value="connectors.txt"/>

<!--Empty 'in' attribute to allow global scope for connectors-->
<arc:set attr="in.in" value=""/>

<!-- List all existing connectors -->
<arc:call op="api.rsc/connectors" httpmethod="get" authtoken="YourUser:YourAdminAPIToken" out="o" in="in">
  <arc:set attr="result.data" value="[o.connectorid]([o.connectortype])\n[result.data]"/>
</arc:call>

<!--Push to output-->
<arc:push item="result"/>
```

### 创建和修改端口

以下示例向系统 API 发出 **POST** 命令，以在流程中创建新的 **Script** 端口：

```xml theme={null}
<arc:set attr="new.connectorid" value="Script1"/>
<arc:set attr="new.connectortype" value="Script"/>
<arc:call op="api.rsc/connectors" httpmethod="post" authtoken="test0:8s9T7b7m5Y5k3i5P2s6x" in="new">
</arc:call>
```

创建此新端口后，可以向系统 API 发出命令，以获取新创建的 Script 端口的 **LogLevel** 值：

```xml theme={null}
<arc:set attr="read.connectorid" value="Script1"/>
<arc:call op="api.rsc/connectors" httpmethod="get" authtoken="test0:8s9T7b7m5Y5k3i5P2s6x" in="read" out="o">
  <arc:set attr="result.new:old_loglevel" value="[o.loglevel]" />
</arc:call>
```

也可以使用 **PUT** 命令更新 Script 端口的 **LogLevel**：

```xml theme={null}
<arc:set attr="update.connectorid" value="Script1"/>
<arc:set attr="update.loglevel" value="Debug"/>

<arc:call op="api.rsc/connectors" httpmethod="put" authtoken="test0:8s9T7b7m5Y5k3i5P2s6x" in="update" out="o">
</arc:call>
```
