-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKickStarterSession.cs
62 lines (53 loc) · 1.9 KB
/
KickStarterSession.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
59
60
61
62
using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Kickstarter.Api.Model;
namespace Kickstarter.Api
{
internal class KickStarterSession : IKickstarterSession
{
private const string Root = "https://api.kickstarter.com/";
private string _accessToken;
public async Task<TResult> Get<TResult>(string path)
{
using (var client = new WebClient())
{
client.Encoding = Encoding.UTF8;
var result = await client.DownloadStringTaskAsync(GetUrl(path));
return await result.ParsedAsJson<TResult>();
}
}
public async Task<TResult> Post<TResult>(string path, object parameters)
{
using (var client = new WebClient())
{
var result = await client.UploadStringTaskAsync(GetUrl(path), "POST", parameters.ToJson());
return await result.ParsedAsJson<TResult>();
}
}
private string GetUrl(string path)
{
var builder = new StringBuilder();
if (!path.StartsWith("http"))
builder.Append(Root);
builder.Append(path);
if (!String.IsNullOrWhiteSpace(_accessToken))
{
builder.Append(path.Contains("?") ? "&" : "?");
builder.AppendFormat("oauth_token={0}", _accessToken);
}
return builder.ToString();
}
public User User { get; private set; }
internal async Task<bool> Logon(string email, string password, string clientId)
{
var logonResult = await Post<LogonResult>(
String.Format("xauth/access_token?client_id={0}", clientId),
new {email, password});
_accessToken = logonResult.AccessToken;
User = logonResult.User;
return true;
}
}
}