HttpClient Rest Api Paging [英] HttpClient Rest Api Paging

查看:65
本文介绍了HttpClient Rest Api Paging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个API,我打电话来取回记录。 格式如下json格式:

So, I have an API that I am calling to get back records.  The format is below in json format:

" query":" budgets",
    "meta":{

        " href" ;:\"https://api2.e-builder.net/api/v2/budgets" ;,

        " offset":0,

        "limit":3000,

        "size":3000,

        "totalRecords":15016

    },b $ b    "记录":[

        {

            " budgetId":" 3e435eda-0fa6-49d3-afb3-c541b881711b",

            " projectId":" df9a22d6-72e4-4250-9dfb-9b423c9dfae2",

            " projectName":" * e-Builder Implementation Project",
            "budgetControl":"Uncontrolled","
            "description":"","
            "status":"Draft","
            "submittedForApprovalById":null,

            "submittedForApprovalBy":null,

            "submittedForApprovalDate":null,
            " approvedById":null,

            "approvedBy":null,

            "approvedByDate":null,

            " createdById":" e708077e-e570-4049-8bf5-09015427c494",&b $ b            "createdBy":"Avi Levin",

            "dateCreated":" 2014-05-21T20:28:47",$
            "lastModifiedById":" e708077e-e570-4049-8bf5-09015427c494",

            "lastModifiedBy":"Avi Levin","
            " lastModifiedDate":"2014-05-21T20:28:47","
            " costControlTolerancePercent" ;: null

        },

"query": "budgets",
    "meta": {
        "href": "https://api2.e-builder.net/api/v2/budgets",
        "offset": 0,
        "limit": 3000,
        "size": 3000,
        "totalRecords": 15016
    },
    "records": [
        {
            "budgetId": "3e435eda-0fa6-49d3-afb3-c541b881711b",
            "projectId": "df9a22d6-72e4-4250-9dfb-9b423c9dfae2",
            "projectName": "*e-Builder Implementation Project",
            "budgetControl": "Uncontrolled",
            "description": "",
            "status": "Draft",
            "submittedForApprovalById": null,
            "submittedForApprovalBy": null,
            "submittedForApprovalDate": null,
            "approvedById": null,
            "approvedBy": null,
            "approvedByDate": null,
            "createdById": "e708077e-e570-4049-8bf5-09015427c494",
            "createdBy": "Avi Levin",
            "dateCreated": "2014-05-21T20:28:47",
            "lastModifiedById": "e708077e-e570-4049-8bf5-09015427c494",
            "lastModifiedBy": "Avi Levin",
            "lastModifiedDate": "2014-05-21T20:28:47",
            "costControlTolerancePercent": null
        },

我可以按照分页限制的规定获得前3000条记录。 我不明白的是如何创建一个回叫,以便我可以继续接下来的3000条记录等等。 我是API的新手,所以我非常努力地使用
来恢复工作。 这是我的C#代码:

I can get the first 3000 records as stated for the limit in pagination.  What I am not understanding is how to create a call back so I can keep going on to the next 3000 records and so forth.  I am pretty new to API's so I am trying really hard to get this call back working.  Here is my C# code:

GetToken p = new GetToken();

            p.ProcessToken();

            var token = p.AuthToken.access_token;



            var serializer = new DataContractJsonSerializer(typeof(ebuilder));

            client.DefaultRequestHeaders.Accept.Clear();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(" application / x-www-form-urlencoded"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(" Bearer",token);

            // client.DefaultRequestHeaders.Add(" User-Agent",".NET Foundation Repository Reporter");



            var streamTask = client.GetStreamAsync(" https://api2.e-builder.net/api/v2/budgets");

            var repositories = serializer.ReadObject(等待streamTask)作为ebuilder;



            DateConverter dateConverter = new DateConverter();



            foreach(存储库中的var记录。记录)

            {

                var date = dateConverter.DateConvert(record.dateCreated);

               使用(var context = new EBuilderContext())

                {

                    var budgets = new预算()

                    {

                        BudgetId = record.budgetId,

                        ProjectId = record.projectId,

                        ProjectName = record.projectName,

                        BudgetControl = record.budgetControl,

                        Description = record.description,

                       状态= record.status,

                        SubmittedForApprovalById = record.submittedForApprovalById,

                        SubmittedForApprovalBy = record.submittedForApprovalBy,

                        SubmittedForApprovalDate = dateConverter.DateConvert(record.submittedForApprovalDate),

                        ApprovedById = record.approvedById,

                        ApprovedBy = record.approvedBy,

                        ApprovedByDate = dateConverter.DateConvert(record.approvedByDate),

                        CreatedById = record.createdById,

                        CreatedBy = record.createdBy,

                        DateCreated = dateConverter.DateConvert(record.dateCreated),

                        LastModifiedById = record.lastModifiedById,

                        LastModifiedBy = record.lastModifiedBy,

                        LastModifiedDate = dateConverter.DateConvert(record.lastModifiedDate),

                        CostControlTolerancePercent = record.costControlTolerancePercent

                    };
                    context.Budgets.Add(预算);

                    context.SaveChanges();

                }¥b $ b            }

GetToken p = new GetToken();
            p.ProcessToken();
            var token = p.AuthToken.access_token;

            var serializer = new DataContractJsonSerializer(typeof(ebuilder));
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            // client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

            var streamTask = client.GetStreamAsync("https://api2.e-builder.net/api/v2/budgets");
            var repositories = serializer.ReadObject(await streamTask) as ebuilder;

            DateConverter dateConverter = new DateConverter();

            foreach (var record in repositories.records)
            {
                var date = dateConverter.DateConvert(record.dateCreated);
                using (var context = new EBuilderContext())
                {
                    var budgets = new Budgets()
                    {
                        BudgetId = record.budgetId,
                        ProjectId = record.projectId,
                        ProjectName = record.projectName,
                        BudgetControl = record.budgetControl,
                        Description = record.description,
                        Status = record.status,
                        SubmittedForApprovalById = record.submittedForApprovalById,
                        SubmittedForApprovalBy = record.submittedForApprovalBy,
                        SubmittedForApprovalDate = dateConverter.DateConvert(record.submittedForApprovalDate),
                        ApprovedById = record.approvedById,
                        ApprovedBy = record.approvedBy,
                        ApprovedByDate = dateConverter.DateConvert(record.approvedByDate),
                        CreatedById = record.createdById,
                        CreatedBy = record.createdBy,
                        DateCreated = dateConverter.DateConvert(record.dateCreated),
                        LastModifiedById = record.lastModifiedById,
                        LastModifiedBy = record.lastModifiedBy,
                        LastModifiedDate = dateConverter.DateConvert(record.lastModifiedDate),
                        CostControlTolerancePercent = record.costControlTolerancePercent
                    };
                    context.Budgets.Add(budgets);
                    context.SaveChanges();
                }
            }

我想我需要使用某种类型的递归函数,但不知道如何将其构建为异步方法 任何帮助将不胜感激。

I would imagine that I would need to use some type of a recursive function but not sure how to build that in to an async method.  Any help would be greatly appreciated.




推荐答案

REST API的执行方式不同,因此您必须参考API文档。他们都将讨论如何进行分页。有些允许您传递查询参数(例如

REST APIs do it differently so you'll have to refer to the APIs documentation. They will all discuss how they do paging. Some allow you to pass query parameters (e.g.


pageIndex = 1&
pageIndex=1&


pageSize = 100)。其他人则要求您为请求添加HTTP标头。
对于GET请求(通常需要分页的时间),通常使用查询参数。根据您的API和URL,我的直觉是您需要将偏移/限制作为查询参数传递。因此,更新您的URL以使用API​​文档中定义的正确参数名称
pageSize=100). Others require you add an HTTP header to the request. For GET requests (the most common time you'll need paging) query parameters are generally used. My gut instinct given your API and URL is that you need to pass the offset/limit as query parameters. So update your URL to use the correct parameter names as defined by the APIs documentation.

注意,您指定的内容类型不是JSON。如果API返回JSON(他们几乎都这样做),那么您应该使用JSON内容类型。如果您的代码现在正在运行,那么我不会更改它,但您应该在以后的日期以
查看它。

Note, you're specifying a content type that isn't JSON. If the API returns JSON (which they pretty much all do) then you should be using the JSON content type instead. If you're code is working now then I wouldn't change it but you should look into it at a later date.


这篇关于HttpClient Rest Api Paging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆