Exemplo n.º 1
0
        /// <summary>
        /// Creates a <see cref="TimeZoneResponse"/> from the given XML stream.
        /// </summary>
        /// <param name="stream">The stream to create the response from.</param>
        /// <returns>The created response.</returns>
        public static TimeZoneResponse FromXml(Stream stream)
        {
            TimeZoneResponse response = new TimeZoneResponse() { Status = TimeZoneCallStatus.Success };

            XPathDocument doc = new XPathDocument(stream);
            XPathNavigator nav = doc.CreateNavigator();
            XPathNodeIterator iterator = nav.Select("//status");

            if (iterator.MoveNext())
            {
                string status = iterator.Current.GetAttribute("value", String.Empty);
                response.Status = (TimeZoneCallStatus)Enum.Parse(typeof(TimeZoneCallStatus), status);

                if (!Enum.IsDefined(typeof(TimeZoneCallStatus), response.Status))
                {
                    response.Status = TimeZoneCallStatus.Unknown;
                }
            }

            if (response.Status == TimeZoneCallStatus.Success)
            {
                iterator = nav.Select("//timezoneId");

                if (iterator.MoveNext())
                {
                    response.TimeZone = iterator.Current.Value.Trim();
                }
            }

            return response;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets this instance's response asynchronously.
        /// </summary>
        /// <param name="callback">A method to be called when the operation completes.</param>
        public void GetResponseAsync(Action<TimeZoneResponse> callback)
        {
            if (this.response == null)
            {
                WebRequest request = WebRequest.Create(this.RequestUri);

                request.BeginGetResponse(
                    delegate(IAsyncResult result)
                    {
                        this.response = CreateResponse((HttpWebResponse)request.EndGetResponse(result));
                        callback(this.response);
                    },
                    null);
            }
            else
            {
                callback(this.response);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a <see cref="TimeZoneResponse"/> from the given <see cref="HttpWebResponse"/>.
        /// </summary>
        /// <param name="webResponse">The web response to create the time zone response from.</param>
        /// <returns>The created time zone response.</returns>
        private static TimeZoneResponse CreateResponse(HttpWebResponse webResponse)
        {
            TimeZoneResponse response = webResponse.StatusCode == HttpStatusCode.OK ?
                TimeZoneResponse.FromXml(webResponse.GetResponseStream()) :
                new TimeZoneResponse();

            if (response.Status == TimeZoneCallStatus.Success && String.IsNullOrEmpty(response.TimeZone))
            {
                response = new TimeZoneResponse();
            }

            return response;
        }
Exemplo n.º 4
0
        public TimeZoneResponse GetResponse()
        {
            if (this.response == null)
            {
                WebRequest request = WebRequest.Create(this.RequestUri);
                this.response = CreateResponse((HttpWebResponse)request.GetResponse());
            }

            return this.response;
        }