-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPublisher.cs
257 lines (207 loc) · 8.73 KB
/
Publisher.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// ***********************************************************************
// Assembly : GCC.API.ConnectorLib
// ***********************************************************************
/// <summary>
/// GCC.CTP.PublicAPIDemo.ConnectorLib namespace.
/// </summary>
namespace GCC.API.ConnectorLib
{
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
/// <summary>
/// Publisher
/// </summary>
public class Publisher
{
const string API_NEW_FILE_ENDPOINT = "https://api.open.glasgow.gov.uk/Files/Organisation/{0}/Dataset/{1}";
const string API_NEW_FILE_VERSION_ENDPOINT = "https://api.open.glasgow.gov.uk/Files/Organisation/{0}/Dataset/{1}/File/{2}";
public Publisher(string authBase, string tenantId, string resourceId, string clientId, string clientKey,
string subscriptionKey, Guid orgId, Guid datasetId)
{
this.Authbase = authBase;
this.TenantId = tenantId;
this.ResourceId = resourceId;
this.ClientId = clientId;
this.ClientKey = clientKey;
this.SubscriptionKey = subscriptionKey;
this.OrgId = orgId;
this.DatasetId = datasetId;
}
#region fields
Guid OrgId { get; set; }
Guid DatasetId { get; set; }
/// <summary>
/// Gets or sets the authbase.
/// </summary>
/// <value>
/// The authbase.
/// </value>
public String Authbase { get; set; }
/// <summary>
/// Gets or sets the client identifier.
/// </summary>
/// <value>
/// The client identifier.
/// </value>
public String ClientId { get; set; }
/// <summary>
/// Gets or sets the client key.
/// </summary>
/// <value>
/// The client key.
/// </value>
public String ClientKey { get; set; }
/// <summary>
/// Gets or sets the data collection application identifier.
/// </summary>
/// <value>
/// The data collection application identifier.
/// </value>
public String ResourceId { get; set; }
/// <summary>
/// Gets or sets the tenant identifier.
/// </summary>
/// <value>
/// The tenant identifier.
/// </value>
public String TenantId { get; set; }
/// <summary>
/// Gets or sets the subscription key.
/// </summary>
/// <value>
/// The subscription key.
/// </value>
public String SubscriptionKey { get; set; }
#endregion
public Guid AddExternalFile(string json)
{
// now make the api request - pass the token to the header
String token = GetToken();
// perform the request
return MakeRequest(token, json);
}
public Guid AddFile(string json, byte[] stream, string filename)
{
// now make the api request - pass the token to the header
String token = GetToken();
// perform the request
return MakeRequestWithFile(token, null, json, stream, filename);
}
public Guid AddExternalFileVersion(Guid fileId, string json)
{
// now make the api request - pass the token to the header
String token = GetToken();
// perform the request
return MakeRequest(token, fileId, json);
}
public Guid AddFileVersion(Guid fileId, string json, byte[] stream, string filename)
{
// now make the api request - pass the token to the header
String token = GetToken();
// perform the request
return MakeRequestWithFile(token, fileId, json, stream, filename);
}
Guid MakeRequest(string token, string json)
{
return MakeRequest(token, null, json);
}
Guid MakeRequestWithFile(string token, Guid? fileid, string json, byte[] filedata, string filename)
{
using (var client = new HttpClient())
{
var queryString = HttpUtility.ParseQueryString(string.Empty);
HttpResponseMessage response;
// set up authentication with api subscription key
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
queryString["subscription-key"] = SubscriptionKey;
string uri = String.Empty;
if (!fileid.HasValue)
uri = String.Format(String.Concat(API_NEW_FILE_ENDPOINT, "?", queryString), this.OrgId, this.DatasetId);
else
uri = String.Format(String.Concat(API_NEW_FILE_VERSION_ENDPOINT, "?", queryString), this.OrgId, this.DatasetId, fileid.Value);
using (var multipartFormDataContent = new MultipartFormDataContent("multipart/form-data"))
{
// add the json
multipartFormDataContent.Add(new StringContent(json, Encoding.UTF8, "application/json"));
multipartFormDataContent.Add(new ByteArrayContent(filedata), "content", filename);
response = client.PostAsync(uri, multipartFormDataContent).Result;
}
#region the result
if (response.Content != null)
{
var responseString = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode.Equals(System.Net.HttpStatusCode.OK))
{
dynamic resp = Newtonsoft.Json.JsonConvert.DeserializeObject(responseString);
return resp.RequestId;
}
else
{
throw new Exception(responseString);
}
}
#endregion
// something went wrong, just pass it up
throw new Exception(response.ReasonPhrase);
}
}
Guid MakeRequest(string token, Guid? fileid, string json)
{
using (var client = new HttpClient())
{
var queryString = HttpUtility.ParseQueryString(string.Empty);
HttpResponseMessage response;
// set up authentication with api subscription key
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
queryString["subscription-key"] = SubscriptionKey;
string uri = String.Empty;
if (!fileid.HasValue)
uri = String.Format(String.Concat(API_NEW_FILE_ENDPOINT, "?", queryString), this.OrgId, this.DatasetId);
else
uri = String.Format(String.Concat(API_NEW_FILE_VERSION_ENDPOINT, "?", queryString), this.OrgId, this.DatasetId, fileid.Value);
byte[] byteData = Encoding.UTF8.GetBytes(json);
using (var content = new ByteArrayContent(byteData))
{
response = client.PostAsync(uri, content).Result;
}
#region the result
if (response.Content != null)
{
var responseString = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode.Equals(System.Net.HttpStatusCode.OK))
{
dynamic resp = Newtonsoft.Json.JsonConvert.DeserializeObject(responseString);
return resp.RequestId;
}
else
{
throw new Exception(responseString);
}
}
#endregion
// something went wrong, just pass it up
throw new Exception(response.ReasonPhrase);
}
}
/// <summary>
/// Gets the token to make an oauth request.
/// </summary>
/// <returns></returns>
string GetToken()
{
#region auth to get the access token to make the api request with
AuthenticationContext context = new AuthenticationContext(Authbase + TenantId, false);
// build a credential
ClientCredential cred = new ClientCredential(ClientId, ClientKey);
// get a token
AuthenticationResult ar = context.AcquireTokenAsync(ResourceId, cred).Result;
return ar.AccessToken;
#endregion
}
}
}