-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSubscription.cs
415 lines (360 loc) · 15.4 KB
/
Subscription.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/********************************************************
* *
* Copyright (C) Microsoft. All rights reserved. *
* *
*********************************************************/
namespace Microsoft.Partner.CSP.Api.V1.Samples
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Helpers;
public static class Subscription
{
/// <summary>
/// This method is to retrieve the subscriptions of a customer bought from the reseller
/// </summary>
/// <param name="customerCid">cid of the customer</param>
/// <param name="resellerCid">cir of the reseller</param>
/// <param name="sa_Token">sales agent token</param>
/// <returns>object that contains all of the subscriptions</returns>
public static dynamic GetSubscriptions(string customerCid, string resellerCid, string sa_Token)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}/subscriptions?recipient_customer_id={1}", resellerCid, customerCid));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
try
{
Utilities.PrintWebRequest(request, string.Empty);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
var subscriptionsResponse = Json.Decode(responseContent);
foreach (var subscription in subscriptionsResponse.items)
{
PrintSubscription(subscription);
}
return subscriptionsResponse;
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// This method returns the subscription given the subscription id
/// </summary>
/// <param name="subscriptionId">subscription id</param>
/// <param name="resellerCid">cid of the reseller</param>
/// <param name="sa_Token">sales agent token</param>
/// <returns>subscription object</returns>
public static dynamic GetSubscriptionById(string subscriptionId, string resellerCid, string sa_Token)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}/subscriptions/{1}", resellerCid, subscriptionId));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
try
{
Utilities.PrintWebRequest(request, string.Empty);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
var subscription = Json.Decode(responseContent);
PrintSubscription(subscription);
return subscription;
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// This method returns the subscription given the subscription uri
/// </summary>
/// <param name="subscriptionUri">subscription uri</param>
/// <param name="sa_Token">sales agent token</param>
/// <returns>subscription object</returns>
public static dynamic GetSubscriptionByUri(string subscriptionUri, string sa_Token)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}", subscriptionUri));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
try
{
Utilities.PrintWebRequest(request, string.Empty);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
var subscription = Json.Decode(responseContent);
PrintSubscription(subscription);
return subscription;
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// Transitions the existing subscription and returns the new subscription object
/// </summary>
/// <param name="subscriptionUri">existing subscription uri</param>
/// <param name="newOfferUri">transitioning to the new offer</param>
/// <param name="resellerCid">reseller Cid</param>
/// <param name="sa_Token">sa token of the reseller</param>
/// <returns>subscription to the new offer</returns>
public static dynamic Transition(string subscriptionId, string customerCid, string newOfferUri, string resellerCid, string sa_Token)
{
var subscription = GetSubscriptionById(subscriptionId, resellerCid, sa_Token);
var order = new
{
line_items = new List<dynamic>
{
new
{
line_item_number = 0,
reference_entitlement_uris = new List<string>
{
subscription.links.entitlement.href
},
offer_uri = newOfferUri,
quantity = subscription.quantity
}
},
recipient_customer_id = customerCid
};
var transitionOrder = Order.PlaceOrder(order, resellerCid, sa_Token);
return GetSubscriptionByUri(transitionOrder.line_items[0].resulting_subscription_uri, sa_Token);
}
/// <summary>
/// This method is to create a stream for a reseller to hear all subscription events
/// </summary>
/// <param name="resellerCid">cid of the reseller</param>
/// <param name="streamName">name for the string to be created</param>
/// <param name="sa_Token">sales agent token</param>
/// <returns>stream information</returns>
public static dynamic CreateSubscriptionStream(string resellerCid, string streamName, string sa_Token)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}/subscription-streams/{0}/{1}", resellerCid, streamName));
request.Method = "PUT";
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
var streamInfo = new
{
start_time = string.Format("{0:MM/dd/yyyy HH:mm:ss}", DateTime.UtcNow),
page_size = 100
};
string content = Json.Encode(streamInfo);
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(content);
}
try
{
Utilities.PrintWebRequest(request, content);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
return Json.Decode(responseContent);
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// This method returns the subscription stream for a reseller
/// </summary>
/// <param name="resellerCid">cid of the reseller</param>
/// <param name="streamName">name of the stream to be retrieved</param>
/// <param name="sa_Token">sales agent token</param>
/// <returns>subscription events for the reseller along with link to mark the page as completed</returns>
public static dynamic GetSubscriptionStream(string resellerCid, string streamName, string sa_Token)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}/subscription-streams/{0}/{1}/pages", resellerCid, streamName));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
try
{
Utilities.PrintWebRequest(request, string.Empty);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
return Json.Decode(responseContent);
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// This method is to mark the page as read inorder to move to the next page in the stream
/// </summary>
/// <param name="completedUri">uri to mark the stream as completed</param>
/// <param name="sa_Token">sales agent token</param>
/// <returns>information that indicated success</returns>
public static dynamic MarkPageAsCompletedInStream(string completedUri, string sa_Token)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}", completedUri));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
try
{
Utilities.PrintWebRequest(request, string.Empty);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
return Json.Decode(responseContent);
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// This method is to suspend a subscription.
/// </summary>
/// <param name="subscriptionId">subscription id</param>
/// <param name="resellerCid">cid of the reseller</param>
/// <param name="sa_Token">sales agent token</param>
/// <returns>subscription object</returns>
public static dynamic SuspendSubscription(string subscriptionId, string resellerCid, string sa_Token)
{
var request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://api.cp.microsoft.com/{0}/subscriptions/{1}/add-suspension", resellerCid, subscriptionId));
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
request.Headers.Add("api-version", "2015-03-31");
request.Headers.Add("x-ms-correlation-id", Guid.NewGuid().ToString());
request.Headers.Add("x-ms-tracking-id", Guid.NewGuid().ToString());
request.Headers.Add("Authorization", "Bearer " + sa_Token);
string content = "{ \"suspension_reason\" : \"CustomerCancellation\", \"suspension_reason_comment\" : \"Customer wanted to cancel\" }";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(content);
}
try
{
Utilities.PrintWebRequest(request, string.Empty);
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
var subscription = Json.Decode(responseContent);
PrintSubscription(subscription);
return subscription;
}
}
catch (WebException webException)
{
using (var reader = new StreamReader(webException.Response.GetResponseStream()))
{
var responseContent = reader.ReadToEnd();
Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
}
}
return string.Empty;
}
/// <summary>
/// This method is to print the subscription information
/// </summary>
/// <param name="subscription">subscription object</param>
private static void PrintSubscription(dynamic subscription)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("=========================================");
Console.WriteLine("Subscription Information");
Console.WriteLine("=========================================");
Console.WriteLine("Id\t\t: {0}", subscription.id);
Console.WriteLine("Order Id\t: {0}", subscription.order_id);
Console.WriteLine("Etag\t\t: {0}", subscription.etag);
Console.WriteLine("Offer Uri\t:{0}", subscription.offer_uri);
Console.WriteLine("Friendly Name\t: {0}", subscription.friendly_name);
Console.WriteLine("\nImportant Dates");
Console.WriteLine("\tCreated Date\t\t: {0}", subscription.creation_date);
Console.WriteLine("\tEffective Start Date\t: {0}", subscription.effective_start_date);
Console.WriteLine("\tCommitment End Date\t: {0}", subscription.commitment_end_date);
Console.WriteLine("\nLife Cycle");
Console.WriteLine("\tState\t\t\t: {0}", subscription.state);
foreach (var suspensionReason in subscription.suspension_reasons)
{
Console.WriteLine("\tSuspended Reason\t: {0}", suspensionReason);
}
Console.WriteLine("=========================================");
Console.ResetColor();
}
}
}