通过基于 REST 合同的 Acumatica 导出记录 API

Exporting Records from Acumatica via REST Contract-Based API

本主题将演示如何通过基于 REST 合同的 API 从 Acumatica ERP 导出记录。与 Acumatica ERP 的基于屏幕 API 相比,基于合同的 API 提供 SOAP 和 REST 接口。有关基于合同的 API 的更多信息,请参阅 Acumatica ERP Documentation

在单个 REST 调用中导出数据

在此示例中,您将探索如何通过基于 REST 合同的单个调用从 Acumatica ERP 中导出以下数据 API:

  • 应用程序中存在的所有库存项目
  • IN类型的所有销售订单

如果您需要从 Acumatica ERP 导出记录,请使用以下 URL: http://<Acumatica ERP instance URL>/entity/<Endpoint name>/<Endpoint version>/<Top-level entity>

<Top-level entity> 是您要导出的实体的名称

要在单个 REST 调用中导出所有库存项目:

使用版本 默认 端点从本地 AcumaticaERP 实例导出库存项目记录= ]6.00.001,你应该使用下面的URL:http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem

下面是用 C# 编写的示例代码,用于通过向版本的 Default 端点发送单个 REST 调用来导出所有库存项目6.00.001:

using (RestService rs = new RestService(
    @"http://localhost/AcumaticaERP/", "Default/6.00.001",
    username, password, company, branch))
{
    string stockItems = rs.GetList("StockItem");
}

要在单个 REST 调用中导出 IN 类型的所有销售订单:

要使用 从本地 AcumaticaERP 实例导出 IN 类型的销售订单默认端点版本6.00.001,你应该使用下面的URL: http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$filter=OrderType eq 'IN'

下面是用 C# 编写的示例代码,通过向默认版本端点6.00.001:

using (RestService rs = new RestService(
    @"http://localhost/Whosebug/", "Default/6.00.001",
    username, password, company, branch))
{
    var parameters = "$filter=OrderType eq 'IN'";
    string inSalesOrders = rs.GetList("SalesOrder", parameters);
}

多个 REST 请求的分页

在此示例中,您将探索如何通过基于 REST 合同的 API:

从 Acumatica ERP 批量导出以下数据
  • 应用程序中存在的库存项目,每批 10 条记录
  • 所有销售订单,每批100条记录

要使用多个 REST 调用以 10 条记录为一组导出库存项目:

使用版本 默认 端点从本地 AcumaticaERP 实例导出前 10 个库存项目=]6.00.001,你应该使用下面的URL:http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem?$top=10

因此,要请求从 10 到 20 的库存商品,您只需使用 filter 参数扩展上面的 URL: http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem?$top=10&$filter=InventoryID gt '<InventoryID>'

<InventoryID> 是使用之前的 REST 调用导出的最后一个库存项目的 ID

下面是用 C# 编写的示例代码,通过向 Default[=154= 发送多个 REST 调用,以 10 条记录为一组导出所有库存项目] 版本端点 6.00.001:

using (RestService rs = new RestService(
    @"http://localhost/Whosebug/", "Default/6.00.001",
    username, password, company, branch))
{
    var json = new JavaScriptSerializer();
    string parameters = "$top=10";
    string items = rs.GetList("StockItem", parameters);
    var records = json.Deserialize<List<Dictionary<string, object>>>(items);

    while (records.Count == 10)
    {
        var inventoryID = records[records.Count - 1]["InventoryID"] as Dictionary<string, object>;
        var inventoryIDValue = inventoryID.Values.First();
        string nextParameters = parameters + "&" + 
            "$filter=" + string.Format("InventoryID gt '{0}'", inventoryIDValue);
        items = rs.GetList("StockItem", nextParameters);
        records = json.Deserialize<List<Dictionary<string, object>>>(items);
    }
}

要使用多个 REST 调用以 100 条记录为一组导出所有销售订单:##

使用版本 默认 端点从本地 AcumaticaERP 实例导出前 100 个销售订单=]6.00.001,你应该使用下面的URL:http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$top=100

由于销售订单实体的主键由订单类型组成和 订单号 ,在本例中,您将使用 filter 参数的组合 订单类型订单编号字段:

  • 要请求 SO 类型的 100 到 200 的销售订单,您应该使用以下 URL:http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$top=100&$filter=OrderType eq 'SO' and OrderNbr gt '<OrderNbr>'

<OrderNbr> 是上一个 REST 调用导出的最后一个销售订单的编号

  • 因此,要请求下一个 SO 类型的前 100 个销售订单,您应该使用以下 URL : http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$top=100&$filter=OrderType gt 'SO' and OrderNbr gt ''

下面是用 C# 编写的示例代码,用于通过对 Default 的多次 REST 调用以 100 条记录为一组导出所有销售订单版本端点 6.00.001:

using (RestService rs = new RestService(
    @"http://localhost/Whosebug/", "Default/6.00.001",
    username, password, company, branch))
{
    var json = new JavaScriptSerializer();
    string parameters = "$top=100";
    string items = rs.GetList("SalesOrder", parameters);
    var records = json.Deserialize<List<Dictionary<string, object>>>(items);

    bool sameOrderType = true;
    while (records.Count > 0 && (records.Count == 100 || !sameOrderType))
    {
        var orderType = records[records.Count - 1]["OrderType"] as Dictionary<string, object>;
        var orderTypeValue = orderType.Values.First();
        var orderNbr = records[records.Count - 1]["OrderNbr"] as Dictionary<string, object>;
        var orderNbrValue = orderNbr.Values.First();

        string nextParameters = parameters + "&" + "$filter=" +
            string.Format("OrderType {0} '{1}'", sameOrderType ? "eq" : "gt", orderTypeValue) + " and " +
            string.Format("OrderNbr gt '{0}'", sameOrderType ? orderNbrValue : "''" );
        items = rs.GetList("SalesOrder", nextParameters);
        records = json.Deserialize<List<Dictionary<string, object>>>(items);
        sameOrderType = records.Count == 100;
    }
}

要与 Acumatica ERP 的基于 REST 合同的 API 通信,您的客户端应用程序必须始终执行以下 3 个步骤:

  1. 登录 Acumatica ERP 实例并获取包含用户会话信息的 cookie
  2. 与 Acumatica ERP 实例上可用的基于合同的 API 端点之一交互
  3. 从 Acumatica ERP 注销以关闭用户会话

本主题中提供的所有示例都是使用 默认 端点构建的,始终作为标准 Acumatica ERP 安装过程的一部分进行部署。在 Web 服务端点 屏幕 (SM.20.70.60) 上,您可以查看现有端点的详细信息或配置 Acumatica ERP 基于合同的 Web 服务的自定义端点:

供您参考,以下是上述所有示例中使用的 RestService class 的实现,用于与 Acumatica ERP 的基于合同的 Web 服务交互:

public class RestService : IDisposable
{
    private readonly HttpClient _httpClient;
    private readonly string _acumaticaBaseUrl;
    private readonly string _acumaticaEndpointUrl;

    public RestService(string acumaticaBaseUrl, string endpoint,
        string userName, string password,
        string company, string branch)
    {
        _acumaticaBaseUrl = acumaticaBaseUrl;
        _acumaticaEndpointUrl = _acumaticaBaseUrl + "/entity/" + endpoint + "/";
        _httpClient = new HttpClient(
            new HttpClientHandler
            {
                UseCookies = true,
                CookieContainer = new CookieContainer()
            })
        {
            BaseAddress = new Uri(_acumaticaEndpointUrl),
            DefaultRequestHeaders =
            {
                Accept = {MediaTypeWithQualityHeaderValue.Parse("text/json")}
            }
        };

        var str = new StringContent(
            new JavaScriptSerializer()
                .Serialize(
                    new
                    {
                        name = userName,
                        password = password,
                        company = company,
                        branch = branch
                    }),
                    Encoding.UTF8, "application/json");

        _httpClient.PostAsync(acumaticaBaseUrl + "/entity/auth/login", str)
            .Result.EnsureSuccessStatusCode();
    }

    void IDisposable.Dispose()
    {
        _httpClient.PostAsync(_acumaticaBaseUrl + "/entity/auth/logout",
            new ByteArrayContent(new byte[0])).Wait();
        _httpClient.Dispose();
    }

    public string GetList(string entityName)
    {
        var res = _httpClient.GetAsync(_acumaticaEndpointUrl + entityName)
            .Result.EnsureSuccessStatusCode();

        return res.Content.ReadAsStringAsync().Result;
    }

    public string GetList(string entityName, string parameters)
    {
        var res = _httpClient.GetAsync(_acumaticaEndpointUrl + entityName + "?" + parameters)
            .Result.EnsureSuccessStatusCode();

        return res.Content.ReadAsStringAsync().Result;
    }
}