Beispiel #1
0
        public void GetAndProcessPage()
        {
            if (String.IsNullOrEmpty(resellerCid) ||
                adAuthorizationToken == null ||
                saAuthorizationToken == null)
            {
                throw new ArgumentException("Call Initialize() before processing");
            }

            int      retryAfter = 60;                     // seconds  - Make a new call after this interval
            TimeSpan abortAfter = new TimeSpan(0, 10, 0); // stop waiting after this interval

            DateTime start = DateTime.Now;

            do
            {
                StreamPageResponse subscriptionPage = Stream.GetStreamPage(resellerCid, saAuthorizationToken.AccessToken);
                foreach (var subscriptionEvent in subscriptionPage.items)
                {
                    CSPStreamEvent streamEvent = new CSPStreamEvent(subscriptionEvent);
                    lastCSPEventTime = streamEvent.EventDate;

                    Console.WriteLine("Entry: " + streamEvent.SubscriptionEventType);
                    localManager.WriteCSPStreamEvent(streamEvent);
                }

                // event not found - mark page as read
                Stream.MarkStreamPageComplete(subscriptionPage.links.completion.href, subscriptionPage.links.completion.method, saAuthorizationToken.AccessToken);

                Console.WriteLine("sleeping");
                Thread.Sleep(retryAfter * 1000);
            } while (DateTime.Now < start + abortAfter);
        }
Beispiel #2
0
        /// <summary>
        /// Retreives the next page of events
        /// </summary>
        /// <param name="resellerCid">cid of the reseller</param>
        /// <param name="sa_Token">sales agent token</param>
        /// <returns>array of items describing the events</returns>
        public static StreamPageResponse GetStreamPage(string resellerCid, string sa_Token)
        {
            StreamPageResponse streamPageResponse = null;

            var request = (HttpWebRequest)
                          HttpWebRequest.Create(
                string.Format("https://api.cp.microsoft.com/{0}/subscription-streams/{0}/{1}/pages", resellerCid, streamName));

            request.Method      = "GET";
            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);

            Utilities.PrintWebRequest(request, String.Empty);
            bool retry       = false;
            int  attempts    = 0;
            int  maxAttempts = 3;
            int  retryAfter  = 60;             // seconds  - Make a new call after this interval


            do
            {
                try
                {
                    var response = request.GetResponse();
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        var responseContent = reader.ReadToEnd();
                        Utilities.PrintWebResponse((HttpWebResponse)response, responseContent);
                        streamPageResponse = JsonConvert.DeserializeObject <StreamPageResponse>(responseContent);
                    }
                }
                catch (WebException webException)
                {
                    HttpWebResponse response   = (HttpWebResponse)webException.Response;
                    int             statusCode = (int)response.StatusCode;
                    if (statusCode == 429)
                    {
                        attempts++;
                        Thread.Sleep(retryAfter * 1000);
                        retry = (attempts < maxAttempts);
                    }
                    else
                    {
                        using (var reader = new StreamReader(webException.Response.GetResponseStream()))
                        {
                            var responseContent = reader.ReadToEnd();
                            Utilities.PrintErrorResponse((HttpWebResponse)webException.Response, responseContent);
                        }
                    }
                }
            } while (retry);

            return(streamPageResponse);
        }
		public CSPStreamEvent(StreamPageResponse.StreamEntryResponse response)
		{
			this.SubscriptionUri = response.subscription_uri;
			this.SubscriptionEventType = response.type;
			this.RecipientCustomerUri = response.recipient_customer_uri;
			this.Data = response.data.ToString().Replace(Environment.NewLine, "");

			DateTime temp;
			if (DateTime.TryParse(response.event_date, out temp))
			{
				this.EventDate = temp;
			}
		}