通过基于REST契约的API从Acumatica导出记录

时间:2017-09-21 21:15:22

标签: rest acumatica acumatica-kb

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

1 个答案:

答案 0 :(得分:2)

单个REST调用中的数据导出

在本例中,您将探索如何通过基于REST契约的API在一次调用中从Acumatica ERP导出以下数据:

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

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

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

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

使用版本 6.00.001 默认 端点从本地AcumaticaERP实例导出库存商品记录> ,您应该使用以下网址:http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem

下面是用C#编写的示例代码,用于通过向 6.00版本的 默认 端点发送单个REST调用来导出所有库存项目。 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 的端点,您应使用以下网址:http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$filter=OrderType eq 'IN'

以下是用C#编写的示例代码,用于通过向 默认<发送单个REST调用来导出 IN 类型的所有销售订单/ em> 版本 6.00.001的端点

using (RestService rs = new RestService(
    @"http://localhost/StackOverflow/", "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个记录的批量导出库存项目:

使用版本 6.00.001的 默认 端点从本地AcumaticaERP实例导出前10个库存商品 ,您应该使用以下网址:http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem?$top=10

因此,要申请10至20的库存商品,只需使用 过滤器 参数扩展上述网址:http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem?$top=10&$filter=InventoryID gt '<InventoryID>'

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

下面是用C#编写的示例代码,通过向版本 <的 默认 端点发送多个REST调用,以10个记录的批量导出所有库存项目EM> 6.00.001

using (RestService rs = new RestService(
    @"http://localhost/StackOverflow/", "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个记录的批量导出所有销售订单:##

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

由于销售订单实体的主键由 订单类型 订单号<组成< / em> ,在此示例中,您将使用 订单类型 <的过滤器参数的组合em>订单号 字段:

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

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

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

下面是用C#编写的示例代码,用于将所有销售订单分批导出100个记录,并且多次REST调用 默认 端点 6.00.001

using (RestService rs = new RestService(
    @"http://localhost/StackOverflow/", "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退出以关闭用户会话
  4. 本主题中提供的所有示例均使用默认端点构建,始终作为标准Acumatica ERP安装过程的一部分进行部署。在 Web服务端点屏幕(SM.20.70.60)上,您可以查看现有端点的详细信息或配置基于Acumatica ERP合同的Web服务的自定义端点: enter image description here

    供您参考,以下是上述所有示例中使用的 RestService 类的实现,以与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;
        }
    }