-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExistClient.cs
58 lines (49 loc) · 2.05 KB
/
ExistClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
namespace ExistSpoon
{
public class ExistClient
{
private readonly string _oauthToken;
public ExistClient(string oauthToken)
{
_oauthToken = oauthToken;
}
public void SubmitCompletions(List<DateCompletionCount> completions)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _oauthToken);
ProgressAggregator.Publish($"Submitting completion updates.");
var attributeUpdateRequests = completions.Select(
completion => new AttributeUpdateRequest("tasks_completed", completion.Date, completion.CompletedCount)).ToArray();
var attributeUpdateResponseMessage = client.PostAsync(
new Uri("https://exist.io/api/1/attributes/update/"),
new StringContent(JsonConvert.SerializeObject(
attributeUpdateRequests),
Encoding.UTF8,
"application/json")).Result;
ProgressAggregator.Publish($"Submitted. Got response {attributeUpdateResponseMessage.StatusCode:D} {attributeUpdateResponseMessage.ReasonPhrase} from updating tasks_completed.");
}
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public class AttributeUpdateRequest
{
public string name { get; set; }
public string date { get; set; }
public int value { get; set; }
public AttributeUpdateRequest(string name, DateTime date, int value)
{
this.name = name;
this.date = date.ToString("yyyy-MM-dd");
this.value = value;
}
}
}
}