WebAPI and OData Functions



我正在使用webapi来获取数据。API 将结果限制为 100。在本例中,有 2481 个,但由于此限制,如果不使用 OData 函数,我无法获取所需的所有记录。我被告知要使用跳过功能,但有关此的文档对于以前没有使用过它的人来说不是很有帮助。

我的电话如下:

https://accounting.sageone.co.za/api/2.0.0/TaxInvoice/get?apikey={xxxxxxxxxx}&companyid=12345&includeDetail=true&$orderby=Created%20desc

下面是返回的第一个结果的狙击手:

> {
>     "TotalResults": 2481,
>     "ReturnedResults": 100,
>     "Results": [
>         {
>             "DueDate": "2018-10-31T00:00:00Z",
>             "FromDocument": "",
>             "AllowOnlinePayment": true,
>             "Paid": false,
>             "Status": "Unpaid",
>             "Locked": false,
>             "CustomerId": 3148838,
>             "CustomerName": "Cell C Limited",
>             "SalesRepresentativeId": 37307,
>             "SalesRepresentative": {
>                 "ID": 37307,
>                 "FirstName": "David",
>                 "LastName": "Markman",
>                 "Name": "David Markman",
>                 "Active": true,
>                 "Email": "davidm@infoslipsconnect.com",
>                 "Mobile": "",
>                 "Telephone": "",
>                 "Created": "2018-10-18T12:42:31.233",
>                 "Modified": "2018-10-18T12:46:49.01"
>             },
>             "Modified": "2018-10-18T12:46:49.01",
>             "Created": "2018-10-18T12:42:31.233",
>             "ID": 483959431,
>             "Date": "2018-10-18T00:00:00Z",
>             "Inclusive": false,
>             "DiscountPercentage": 0,
>             "TaxReference": "4870194356",
>             "DocumentNumber": "INV03357",
>             "Reference": "14480 - October Print & Post",
>             "Message": "",
>             "Discount": 0,
>             "Exclusive": 98243.04,
>             "Tax": 14736.46,
>             "Rounding": 0,
>             "Total": 112979.5,
>             "AmountDue": 112979.5,
>             "PostalAddress01": "Cell C ",
>             "PostalAddress02": "Waterfall Campus",
>             "PostalAddress03": "C/O Maxwell Drive and Pretoria Road",
>             "PostalAddress04": "Buccleuch",
>             "PostalAddress05": "",
>             "DeliveryAddress01": "September 2018 Print & Post Distribution",
>             "DeliveryAddress02": "",
>             "DeliveryAddress03": "",
>             "DeliveryAddress04": "",
>             "DeliveryAddress05": "PO: TBC",
>             "Printed": true,
>             "Editable": true,
>             "HasAttachments": false,
>             "HasNotes": false,
>             "HasAnticipatedDate": false,
>             "Lines": [
>                 {
>                     "SelectionId": 4804411,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907379,
>                     "Description": "CELL C PRINT & POST ",
>                     "LineType": 0,
>                     "Quantity": 1,
>                     "UnitPriceExclusive": 0,
>                     "Unit": "",
>                     "UnitPriceInclusive": 0,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>                     "Exclusive": 0,
>                     "Discount": 0,
>                     "Tax": 0,
>                     "Total": 0,
>                     "Comments": "",
>                     "AnalysisCategoryId1": 16041,
>                     "UnitCost": 0
>                 },
>                 {
>                     "SelectionId": 12926677,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907380,
>                     "Description": "CellC: PDF Compiled",
>                     "LineType": 0,
>                     "Quantity": 52926,
>                     "UnitPriceExclusive": 0.34,
>                     "Unit": "",
>                     "UnitPriceInclusive": 0.39,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>                     "Exclusive": 17994.84,
>                     "Discount": 0,
>                     "Tax": 2699.23,
>                     "Total": 20694.07,
>                     "Comments": "",
>                     "AnalysisCategoryId1": 16041,
>                     "UnitCost": 0
>                 },
>                 {
>                     "SelectionId": 12926854,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907382,
>                     "Description": "CellC: Rounding Difference",
>                     "LineType": 0,
>                     "Quantity": 1,
>                     "UnitPriceExclusive": -238.16,
>                     "Unit": "",
>                     "UnitPriceInclusive": -273.88,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>                     "Exclusive": -238.16,
>                     "Discount": 0,
>                     "Tax": -35.72,
>                     "Total": -273.88,
>                     "Comments": "",
>                     "AnalysisCategoryId1": 16041,
>                     "UnitCost": 0
>                 },
>                 {
>                     "SelectionId": 12926687,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907383,
>                     "Description": "CellC: PDF Print",
>                     "LineType": 0,
>                     "Quantity": 109782.5,
>                     "UnitPriceExclusive": 0.73,
>                     "Unit": "",
>                     "UnitPriceInclusive": 0.83,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>                     "Exclusive": 80141.23,
>                     "Discount": 0,
>                     "Tax": 12021.18,
>                     "Total": 92162.41,
>                     "Comments": "",
>                     "AnalysisCategoryId1": 16041,
>                     "UnitCost": 0
>                 },
>                 {
>                     "SelectionId": 12926854,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907384,
>                     "Description": "CellC: Rounding Difference",
>                     "LineType": 0,
>                     "Quantity": 1,
>                     "UnitPriceExclusive": -87.82,
>                     "Unit": "",
>                     "UnitPriceInclusive": -100.99,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>                     "Exclusive": -87.82,
>                     "Discount": 0,
>                     "Tax": -13.17,
>                     "Total": -100.99,
>                     "Comments": "",
>                     "AnalysisCategoryId1": 16041,
>                     "UnitCost": 0
>                 },
>                 {
>                     "SelectionId": 12926687,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907385,
>                     "Description": "CellC: Hand Delivery",
>                     "LineType": 0,
>                     "Quantity": 2,
>                     "UnitPriceExclusive": 0.34,
>                     "Unit": "",
>                     "UnitPriceInclusive": 0.39,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>                     "Exclusive": 0.68,
>                     "Discount": 0,
>                     "Tax": 0.1,
>                     "Total": 0.78,
>                     "Comments": "",
>                     "AnalysisCategoryId1": 16041,
>                     "UnitCost": 0
>                 },
>                 {
>                     "SelectionId": 12926854,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907386,
>                     "Description": "CellC: Rounding Difference",
>                     "LineType": 0,
>                     "Quantity": 1,
>                     "UnitPriceExclusive": -0.01,
>                     "Unit": "",
>                     "UnitPriceInclusive": -0.01,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>                     "Exclusive": -0.01,
>                     "Discount": 0,
>                     "Tax": 0,
>                     "Total": -0.01,
>                     "Comments": "",
>                     "AnalysisCategoryId1": 16041,
>                     "UnitCost": 0
>                 },
>                 {
>                     "SelectionId": 12926761,
>                     "TaxTypeId": 2691481,
>                     "ID": 328907387,
>                     "Description": "CellC: Nixie",
>                     "LineType": 0,
>                     "Quantity": 1,
>                     "UnitPriceExclusive": 432.28,
>                     "Unit": "",
>                     "UnitPriceInclusive": 497.12,
>                     "TaxPercentage": 0.15,
>                     "DiscountPercentage": 0,
>            "Exclusive": 432.28,
>             "Discount": 0,
>              "Tax": 64.84,
>               "Total": 497.12,
>                "Comments": "",
>                 "AnalysisCategoryId1": 16041,
>                  "UnitCost": 0
>               }
>            ]
>         },

我需要执行以下操作:返回特定字段("已创建"(包含当前年份的所有记录,即使结果超过 100 个也是如此。我假设需要使用$search和跳过函数,但我不确定语法。

谢谢 史蒂文

根据此处的文档,您应该将跳过参数合并到 URL 中。

像这样:

https://accounting.sageone.co.za/api/2.0.0/TaxInvoice/get?apikey={xxxxxxxxxx}&companyid=12345&includeDetail=true&$skip=100&$orderby=Created%20desc

提供的示例是:

https://services.odata.org/OData/OData.svc/Products?$skip=2&$top=2&$orderby=Rating

您可能需要摆弄一下,但这似乎是方法。

要返回特定字段("已创建"(包含当前年份的所有记录,您需要将以下筛选条件附加到 url。您可以在此处查看支持的其他 OData Date 函数。

&$filter=year(Created) eq 2018

OData 处理可能返回大量数据的服务的分页。由于 API 限制为 100,表示数据的页面大小,因此每次服务调用最多可以获得 100 条记录。要获取从 101 到 200 的记录,您需要通过将$skip=100附加到 url 来跳过前 100 条。此外,如果您需要获取从 201 到 300 的记录,则将 $skip=200 附加到 url,依此类推,直到获得 2481 条记录。这意味着您需要通过增加$skip值来调用服务 25 次。

因此,使用额外的过滤器和跳过查询,您需要将以下查询字符串附加到现有 url

&$filter=year(Created) eq 2018&$skip=100

此外,如果要获取每个服务调用少于 100 条的记录,则需要使用$top查询。假设应用程序中的页面大小为 50,则将$top=50追加到查询字符串。

最新更新