/
KinveyService.cs
148 lines (116 loc) · 5.16 KB
/
KinveyService.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Com.Penrillian.Kinvey
{
internal class KinveyService<T> : IKinveyService<T> where T : KinveyObject, new()
{
private readonly IFactory _factory;
private readonly IHttpClient _httpClient;
private readonly IHttpRequestHeaders _headers;
private string _entityName;
internal KinveyService(IFactory factory)
{
_factory = factory;
_httpClient = _factory.Get<IHttpClient>();
_headers = _httpClient.DefaultRequestHeaders;
ReadAttributes();
if(null == _entityName)
throw new ArgumentException("Target class must be marked with the KinveyCollection attribute");
InitClient();
}
private void InitClient()
{
_headers.Add("Authorization", new[]
{
string.Format("Kinvey {0}", KinveySettings.Get().UserAuthToken)
});
_headers.Add("X-Kinvey-Api-Version", new[]
{
"2"
});
_httpClient.BaseAddress = new Uri("https://baas.kinvey.com/");
}
private void ReadAttributes()
{
var t = typeof (T);
var attributes = Attribute.GetCustomAttributes(t);
foreach (var attribute in attributes
.Where(attribute => attribute is KinveyCollectionAttribute)
.Cast<KinveyCollectionAttribute>())
{
_entityName = attribute.CollectionName;
}
}
#region Implementation of IKinveyService<T>
public async Task<T> Create(T t)
{
var body = _factory.Get<IHttpContent>(JsonConvert.SerializeObject(t));
var uri = new Uri(string.Format("/appdata/{0}/{1}/", KinveySettings.Get().AppKey, _entityName), UriKind.Relative);
var response = await _httpClient.PostAsync(uri, body);
return await GetResult<T>(response);
}
public async Task<T> Read(string id)
{
var uri = new Uri(string.Format("/appdata/{0}/{1}/{2}/", KinveySettings.Get().AppKey, _entityName, id), UriKind.Relative);
var response = await _httpClient.GetAsync(uri);
return await GetResult<T>(response);
}
public async Task<IEnumerable<T>> Read(KinveyConstraints<T> kinveyQuery)
{
var uri = new Uri(string.Format("/appdata/{0}/{1}/{2}", KinveySettings.Get().AppKey, _entityName, kinveyQuery), UriKind.Relative);
var response = await _httpClient.GetAsync(uri);
return await GetResult<IEnumerable<T>>(response);
}
public async Task<T> Update(T t)
{
var body = _factory.Get<IHttpContent>(JsonConvert.SerializeObject(t));
var uri = new Uri(string.Format("/appdata/{0}/{1}/{2}", KinveySettings.Get().AppKey, _entityName, t.Id), UriKind.Relative);
var response = await _httpClient.PutAsync(uri, body);
return await GetResult<T>(response);
}
public async Task<int> Delete(T t)
{
var id = t.Id;
var query = "{\"_id\": \"" + id + "\"}";
var uri = new Uri(string.Format("/appdata/{0}/{1}/?query={2}", KinveySettings.Get().AppKey, _entityName, query), UriKind.Relative);
var response = await _httpClient.DeleteAsync(uri);
return (await GetResult<CountResponse>(response)).Count;
}
public async Task<int> Delete(KinveyConstraints<T> query)
{
var uri = new Uri(string.Format("/appdata/{0}/{1}/{2}", KinveySettings.Get().AppKey, _entityName, query), UriKind.Relative);
var response = await _httpClient.DeleteAsync(uri);
return (await GetResult<CountResponse>(response)).Count;
}
public async Task<int> Count()
{
var uri = new Uri(string.Format("/appdata/{0}/{1}/{2}", KinveySettings.Get().AppKey, _entityName, "_count"), UriKind.Relative);
var response = await _httpClient.GetAsync(uri);
return (await GetResult<CountResponse>(response)).Count;
}
public async Task<int> Count(KinveyConstraints<T> query)
{
var uri = new Uri(string.Format("/appdata/{0}/{1}/{2}/{3}", KinveySettings.Get().AppKey, _entityName, "_count", query), UriKind.Relative);
var response = await _httpClient.GetAsync(uri);
return (await GetResult<CountResponse>(response)).Count;
}
#endregion
private static async Task<TR> GetResult<TR>(IHttpResponseMessage response)
{
var content = response.Content;
var json = await content.ReadAsStringAsync();
try
{
var errorObject = JsonConvert.DeserializeObject<KinveyError>(json);
if (null != errorObject.Error)
throw new KinveyException(errorObject);
}
catch (JsonSerializationException)
{ }
return JsonConvert.DeserializeObject<TR>(json);
}
}
}