【问题标题】:Exporting Records from Acumatica via REST Contract-Based API通过基于 REST 合同的 API 从 Acumatica 导出记录
【发布时间】:2017-09-21 21:15:22
【问题描述】:

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

【问题讨论】:

    标签: rest acumatica acumatica-kb


    【解决方案1】:

    在单个 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实例导出库存项目记录,您应该使用以下 URL:http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem

    以下是用 C# 编写的示例代码,通过向 6.00 版本的 Default 端点发送单个 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 类型的销售订单:

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

    下面是用 C# 编写的示例代码,通过向 DefaultIN 类型的所有销售订单/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 Contract-Based API 从 Acumatica ERP 中批量导出以下数据:

    • 应用程序中存在的库存项目,每批 10 条记录
    • 每批100条记录的所有销售订单

    使用多个 REST 调用分批导出 10 条记录的库存商品:

    使用版本 6.00.001Default 端点从本地 AcumaticaERP 实例导出前 10 个库存项目,您应该使用以下网址: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# 编写的示例代码,通过发送多个 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=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.001Default 端点从本地 AcumaticaERP 实例导出前 100 个销售订单,您应该使用以下网址:http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$top=100

    由于销售订单实体的主键是由订单类型订单号,在本例中,您将为 Order Typefilter 参数的组合em>订单号字段:

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

    &lt;OrderNbr&gt; 是使用先前 REST 调用导出的最后一个销售订单的编号

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

    下面是用 C# 编写的示例代码,用于通过多次 REST 调用将所有销售订单批量导出 100 条记录到版本 Default 端点>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 注销以关闭用户会话

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

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

    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;
        }
    }
    

    【讨论】:

    • 我希望我能在 6 个月前找到这个!
    • 嗨,登录后是否有一个端点可供我调用以获取已安装版本的 Acumatica 的默认端点字符串? (您的示例中为“6.00.001”)查看他们 github 上的示例控制台应用程序,它只是硬编码???
    猜你喜欢
    • 2018-03-03
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多