How to properly do JSON API GET requests and assign output (Kimai Time Tracking)
Posted By: Anonymous
I want to write a simple desktop application to track the overtime work of our employees. Whenever one of our employees needs to perform some tasks outside our normal working hours we want to track them using "Kimai Time Tracking".
The application I am programming needs to get the duration of all recorded tasks from Kimai, add them up and store them on a local SQL Server as an overtime contingent for the employee to claim later.
This is the GET request I’m mainly gonna use:
GET /api/timesheets (Returns a collection of timesheet records)
and this is an example output from the Kimai Demo (Sorry for length)
[
{
"activity": 174,
"project": 12,
"user": 1,
"tags": [],
"id": 5610,
"begin": "2021-05-28T01:44:00-0700",
"end": null,
"duration": null,
"description": null,
"rate": 0,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 305,
"project": 23,
"user": 1,
"tags": [
"Et quo.",
"Aut ut."
],
"id": 240,
"begin": "2021-05-25T23:33:00-0700",
"end": "2021-05-26T05:18:00-0700",
"duration": 20700,
"description": "Latitude was, or Longitude either, but thought they were mine before. If I or she should chance to be patted on the.",
"rate": 465.75,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 486,
"project": 40,
"user": 1,
"tags": [],
"id": 155,
"begin": "2021-05-23T14:48:00-0700",
"end": "2021-05-23T15:21:00-0700",
"duration": 1980,
"description": null,
"rate": 44.55,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 667,
"project": 52,
"user": 1,
"tags": [
"MistyRose"
],
"id": 300,
"begin": "2021-05-20T20:27:00-0700",
"end": "2021-05-21T08:10:00-0700",
"duration": 42180,
"description": "March Hare went on. 'Or would you like the wind, and the cool fountains. CHAPTER VIII. The Queen's.",
"rate": 949.05,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 789,
"project": 62,
"user": 1,
"tags": [],
"id": 272,
"begin": "2021-05-19T01:21:00-0700",
"end": "2021-05-19T12:47:00-0700",
"duration": 41160,
"description": null,
"rate": 926.1,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 585,
"project": 45,
"user": 1,
"tags": [
"in",
"Orphaborough"
],
"id": 120,
"begin": "2021-05-17T12:48:00-0700",
"end": "2021-05-17T18:56:00-0700",
"duration": 22080,
"description": "Who ever saw in another moment that it was very like having a game of play with a deep voice, 'are done.",
"rate": 496.8,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 250,
"project": 19,
"user": 1,
"tags": [
"Blue",
"Vergiebury"
],
"id": 68,
"begin": "2021-05-17T02:40:00-0700",
"end": "2021-05-17T05:21:00-0700",
"duration": 9660,
"description": null,
"rate": 217.35,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 426,
"project": 33,
"user": 1,
"tags": [
"Tomato"
],
"id": 5609,
"begin": "2021-05-12T05:50:00-0700",
"end": "2021-05-12T06:21:00-0700",
"duration": 1860,
"description": null,
"rate": 41.85,
"internalRate": 41.85,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 62,
"project": 6,
"user": 1,
"tags": [
"Port Jeremiefort",
"Gonzalo Summit"
],
"id": 327,
"begin": "2021-05-09T04:13:00-0700",
"end": "2021-05-09T07:08:00-0700",
"duration": 10500,
"description": "Duchess's cook. She carried the pepper-box in her hands.",
"rate": 236.25,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 465,
"project": 36,
"user": 1,
"tags": [],
"id": 321,
"begin": "2021-05-08T08:38:00-0700",
"end": "2021-05-08T22:37:00-0700",
"duration": 50340,
"description": "I grow up, I'll write one--but I'm grown up now,' she added in an undertone.",
"rate": 1132.65,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 1155,
"project": 92,
"user": 1,
"tags": [
"Chocolate"
],
"id": 122,
"begin": "2021-05-03T16:20:00-0700",
"end": "2021-05-03T19:49:00-0700",
"duration": 12540,
"description": null,
"rate": 282.15,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 916,
"project": 73,
"user": 1,
"tags": [
"Schiller Brooks"
],
"id": 215,
"begin": "2021-04-29T21:32:00-0700",
"end": "2021-04-30T00:36:00-0700",
"duration": 11040,
"description": null,
"rate": 248.4,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 426,
"project": 33,
"user": 1,
"tags": [
"Tomato"
],
"id": 5608,
"begin": "2021-04-27T05:50:00-0700",
"end": "2021-04-27T06:21:00-0700",
"duration": 1860,
"description": null,
"rate": 41.85,
"internalRate": 41.85,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 175,
"project": 12,
"user": 1,
"tags": [
"itaque",
"Harberland"
],
"id": 253,
"begin": "2021-04-25T18:18:00-0700",
"end": "2021-04-26T00:40:00-0700",
"duration": 22920,
"description": null,
"rate": 515.7,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 439,
"project": 33,
"user": 1,
"tags": [
"Fay Stravenue"
],
"id": 40,
"begin": "2021-04-20T09:07:00-0700",
"end": "2021-04-20T11:09:00-0700",
"duration": 7320,
"description": null,
"rate": 164.7,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 903,
"project": 73,
"user": 1,
"tags": [
"South Maraview"
],
"id": 55,
"begin": "2021-04-12T10:00:00-0700",
"end": "2021-04-12T19:57:00-0700",
"duration": 35820,
"description": null,
"rate": 805.95,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 406,
"project": 32,
"user": 1,
"tags": [],
"id": 334,
"begin": "2021-04-06T09:08:00-0700",
"end": "2021-04-06T10:04:00-0700",
"duration": 3360,
"description": null,
"rate": 75.6,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 799,
"project": 63,
"user": 1,
"tags": [
"Morar Plaza"
],
"id": 312,
"begin": "2021-04-03T18:57:00-0700",
"end": "2021-04-04T08:47:00-0700",
"duration": 49800,
"description": "Yet you turned a back-somersault in at the house, "Let us both go to on the breeze that followed them, the melancholy words:-- 'Soo--oop of the trees.",
"rate": 1120.5,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 119,
"project": 10,
"user": 1,
"tags": [
"debitis"
],
"id": 203,
"begin": "2021-03-30T09:59:00-0700",
"end": "2021-03-30T14:31:00-0700",
"duration": 16320,
"description": "Quam possimus earum ut tenetur eligendi. Autem est possimus at hic. Eius voluptatem dolores nihil qui debitis et laudantium.",
"rate": 367.2,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 118,
"project": 10,
"user": 1,
"tags": [],
"id": 159,
"begin": "2021-03-30T02:06:00-0700",
"end": "2021-03-30T04:25:00-0700",
"duration": 8340,
"description": "I'll never go THERE again!' said Alice very politely; but she felt certain it must be what he did with the time,' she said, 'than waste it in with a sigh.",
"rate": 187.65,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 259,
"project": 20,
"user": 1,
"tags": [],
"id": 294,
"begin": "2021-03-29T20:33:00-0700",
"end": "2021-03-30T06:01:00-0700",
"duration": 34080,
"description": "When the pie was all dark overhead; before her was another long passage, and the beak-- Pray how did you ever eat a little scream, half of them--and it belongs to the Mock.",
"rate": 766.8,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 108,
"project": 9,
"user": 1,
"tags": [],
"id": 41,
"begin": "2021-03-26T17:22:00-0700",
"end": "2021-03-26T19:48:00-0700",
"duration": 8760,
"description": null,
"rate": 197.1,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 1022,
"project": 79,
"user": 1,
"tags": [],
"id": 222,
"begin": "2021-03-25T08:40:00-0700",
"end": "2021-03-25T19:38:00-0700",
"duration": 39480,
"description": "I can find them.' As she said this, she looked up eagerly, half hoping that the Mouse heard this, it turned a back-somersault in at the.",
"rate": 888.3,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 387,
"project": 31,
"user": 1,
"tags": [],
"id": 198,
"begin": "2021-03-24T19:17:00-0700",
"end": "2021-03-24T21:32:00-0700",
"duration": 8100,
"description": "I to get us dry would be only rustling in the distance. 'And yet.",
"rate": 182.25,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 1082,
"project": 88,
"user": 1,
"tags": [],
"id": 213,
"begin": "2021-03-23T09:35:00-0700",
"end": "2021-03-23T19:09:00-0700",
"duration": 34440,
"description": "Mock Turtle in a tone of great surprise. 'Of course it is,' said the Queen, the royal children, and make out which were the verses the White Rabbit hurried by--the frightened.",
"rate": 774.9,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 683,
"project": 53,
"user": 1,
"tags": [
"Windler Springs",
"Lulu Mount"
],
"id": 146,
"begin": "2021-03-17T11:20:00-0700",
"end": "2021-03-17T16:01:00-0700",
"duration": 16860,
"description": null,
"rate": 379.35,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 984,
"project": 77,
"user": 1,
"tags": [],
"id": 36,
"begin": "2021-03-09T07:24:00-0800",
"end": "2021-03-09T10:33:00-0800",
"duration": 11340,
"description": "Who Stole the Tarts? The King and Queen of Hearts, who only bowed and smiled in reply. 'Idiot!' said the Hatter, it woke up.",
"rate": 255.15,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 265,
"project": 20,
"user": 1,
"tags": [],
"id": 138,
"begin": "2021-02-25T08:17:00-0800",
"end": "2021-02-25T13:09:00-0800",
"duration": 17520,
"description": "Alice; 'you needn't be afraid of them!' 'And who are THESE?' said the Mock Turtle with a teacup in one hand and a sad tale!' said the Mock Turtle sighed deeply, and drew the back of one flapper.",
"rate": 394.2,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 638,
"project": 50,
"user": 1,
"tags": [
"Keonberg"
],
"id": 282,
"begin": "2021-02-23T12:18:00-0800",
"end": "2021-02-23T17:13:00-0800",
"duration": 17700,
"description": "You gave us three or more; They.",
"rate": 398.25,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 753,
"project": 58,
"user": 1,
"tags": [],
"id": 100,
"begin": "2021-02-22T14:14:00-0800",
"end": "2021-02-22T19:11:00-0800",
"duration": 17820,
"description": null,
"rate": 400.95,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 1105,
"project": 89,
"user": 1,
"tags": [
"Ea iste."
],
"id": 98,
"begin": "2021-02-22T08:30:00-0800",
"end": "2021-02-22T19:17:00-0800",
"duration": 38820,
"description": "Mollitia optio at quidem ut saepe impedit consequatur. Repellat id et labore inventore magnam. Animi ex necessitatibus magni ut.",
"rate": 873.45,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 735,
"project": 57,
"user": 1,
"tags": [],
"id": 232,
"begin": "2021-02-22T04:42:00-0800",
"end": "2021-02-22T17:24:00-0800",
"duration": 45720,
"description": null,
"rate": 1028.7,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 445,
"project": 34,
"user": 1,
"tags": [
"Hegmann Squares",
"Camylle Drive"
],
"id": 148,
"begin": "2021-02-21T09:57:00-0800",
"end": "2021-02-21T23:53:00-0800",
"duration": 50160,
"description": null,
"rate": 1128.6,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 302,
"project": 23,
"user": 1,
"tags": [],
"id": 73,
"begin": "2021-02-20T16:03:00-0800",
"end": "2021-02-21T03:56:00-0800",
"duration": 42780,
"description": null,
"rate": 962.55,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 426,
"project": 33,
"user": 1,
"tags": [
"Tomato"
],
"id": 320,
"begin": "2021-02-16T09:25:00-0800",
"end": "2021-02-16T11:55:00-0800",
"duration": 9000,
"description": null,
"rate": 202.5,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 896,
"project": 72,
"user": 1,
"tags": [],
"id": 27,
"begin": "2021-02-13T04:40:00-0800",
"end": "2021-02-13T14:56:00-0800",
"duration": 36960,
"description": "Bill's place for a minute or two, she made it out to sea. So they got their tails in their mouths; and the reason they're called lessons,' the Gryphon.",
"rate": 831.6,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 683,
"project": 53,
"user": 1,
"tags": [
"Nobis sit.",
"Sed iure."
],
"id": 2,
"begin": "2021-02-13T00:22:00-0800",
"end": "2021-02-13T14:08:00-0800",
"duration": 49560,
"description": null,
"rate": 1115.1,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 938,
"project": 74,
"user": 1,
"tags": [
"Clementina Locks"
],
"id": 343,
"begin": "2021-02-12T08:38:00-0800",
"end": "2021-02-12T16:40:00-0800",
"duration": 28920,
"description": "Laboriosam dolorem laboriosam fugit qui quidem perferendis voluptas. Provident repudiandae maiores quia quis.",
"rate": 650.7,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 47,
"project": 3,
"user": 1,
"tags": [
"Bashirianfort"
],
"id": 60,
"begin": "2021-02-11T11:21:00-0800",
"end": "2021-02-11T14:08:00-0800",
"duration": 10020,
"description": "I should.",
"rate": 225.45,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 37,
"project": 3,
"user": 1,
"tags": [
"North Vanville",
"Tromp Lights"
],
"id": 72,
"begin": "2021-02-08T21:58:00-0800",
"end": "2021-02-09T11:57:00-0800",
"duration": 50340,
"description": "Queen, and Alice, were in custody and under sentence of.",
"rate": 1132.65,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 13,
"project": 1,
"user": 1,
"tags": [],
"id": 190,
"begin": "2021-02-05T18:50:00-0800",
"end": "2021-02-05T23:09:00-0800",
"duration": 15540,
"description": null,
"rate": 349.65,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 1071,
"project": 86,
"user": 1,
"tags": [],
"id": 200,
"begin": "2021-01-30T23:04:00-0800",
"end": "2021-01-31T00:45:00-0800",
"duration": 6060,
"description": null,
"rate": 136.35,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 940,
"project": 74,
"user": 1,
"tags": [
"Sienna"
],
"id": 14,
"begin": "2021-01-27T19:52:00-0800",
"end": "2021-01-27T23:47:00-0800",
"duration": 14100,
"description": "Corrupti suscipit sequi fugiat placeat ab. Impedit magnam et voluptatem iste eligendi temporibus. Consequuntur quaerat maxime aut provident odit quis.",
"rate": 317.25,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 806,
"project": 64,
"user": 1,
"tags": [
"quibusdam"
],
"id": 238,
"begin": "2021-01-26T14:15:00-0800",
"end": "2021-01-26T20:41:00-0800",
"duration": 23160,
"description": "Quia et reprehenderit tempora sit tempora. Aliquam numquam ad est omnis exercitationem. Et architecto itaque quas omnis. Numquam at et reprehenderit hic expedita consectetur at atque.",
"rate": 521.1,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 715,
"project": 55,
"user": 1,
"tags": [
"Whitechester",
"Schmitt Square"
],
"id": 188,
"begin": "2021-01-23T14:24:00-0800",
"end": "2021-01-24T02:47:00-0800",
"duration": 44580,
"description": null,
"rate": 1003.05,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 626,
"project": 49,
"user": 1,
"tags": [
"McCullough Spring",
"New Vidaburgh"
],
"id": 77,
"begin": "2021-01-22T10:58:00-0800",
"end": "2021-01-22T19:27:00-0800",
"duration": 30540,
"description": "Nisi sit est veritatis quae amet quod. Atque iure ratione animi iste. Quisquam et ad assumenda aut. Ut tempora itaque facere voluptas similique ab. Est est qui libero et aliquam nihil.",
"rate": 687.15,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 632,
"project": 50,
"user": 1,
"tags": [],
"id": 65,
"begin": "2021-01-18T12:24:00-0800",
"end": "2021-01-18T22:22:00-0800",
"duration": 35880,
"description": null,
"rate": 807.3,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 497,
"project": 41,
"user": 1,
"tags": [
"Quis.",
"tempora"
],
"id": 332,
"begin": "2021-01-15T00:13:00-0800",
"end": "2021-01-15T02:38:00-0800",
"duration": 8700,
"description": null,
"rate": 195.75,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 765,
"project": 58,
"user": 1,
"tags": [],
"id": 287,
"begin": "2021-01-13T10:49:00-0800",
"end": "2021-01-13T15:58:00-0800",
"duration": 18540,
"description": "Hic facilis a ipsa maxime aliquid enim. In aliquam et sequi soluta. Unde quis sunt ducimus officiis commodi.",
"rate": 417.15,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
},
{
"activity": 151,
"project": 11,
"user": 1,
"tags": [
"Adrien Union"
],
"id": 80,
"begin": "2021-01-04T15:08:00-0800",
"end": "2021-01-04T19:54:00-0800",
"duration": 17160,
"description": null,
"rate": 386.1,
"internalRate": 0,
"exported": false,
"billable": true,
"metaFields": []
}
]
I’m ONLY interested in the duration values. I need to add them all together and store them as an integer variable (Write to SQL DB).
So my question is: What’s the best-practise way of doing this? I’ve tried Process.Start() and redirecting the StandardOutput to a string variable. This technically works but would be a pain to seperate all the "duration values" etc.
Is there any better way to do this?
Thanks in advance!
In case you wanna test it yourself:
Kimai Live Demo: https://demo.kimai.org
Demo Login Credentials: https://www.kimai.org/demo
Kimai API: https://demo.kimai.org/api/doc (after login)
Solution
You could use the HttpClient API to issue a REST request and then parse the response directly in your .NET app:
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-AUTH-USER", "susan_super");
httpClient.DefaultRequestHeaders.Add("X-AUTH-TOKEN", "api_kitten");
string json = await httpClient.GetStringAsync("https://demo.kimai.org/api/timesheets?user=1");
There is no need to start another process to do this.
Answered By: Anonymous
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.