S
smichaels1234
Guest
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
},
"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
},
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(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.
Continue reading...
"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
},
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(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.
Continue reading...