/// <summary>
        /// Start getting the new application info from the HealthVault platform.
        /// </summary>
        /// <param name="state">The current state.</param>
        private static void StartNewApplicationCreationInfo(AuthenticationCheckState state)
        {
            CHBaseRequest request = CHBaseRequest.Create("NewApplicationCreationInfo", "1", null, NewApplicationCreationInfoCompleted);

            request.UserState = state;

            state.Service.BeginSendRequest(request);
        }
        /// <summary>
        /// Send a request to the HealthVault web service.
        /// </summary>
        /// <remarks>
        /// This method returns immediately; the results and any error information will be passed to the
        /// completion method stored in the request.
        /// </remarks>
        /// <param name="request">The request to send.</param>
        public virtual void BeginSendRequest(
            CHBaseRequest request)
        {
            string requestXml = GenerateRequestXml(request);

            WebTransport transport = new WebTransport();

            transport.BeginSendPostRequest(HealthServiceUrl, requestXml, SendRequestCallback, request);
        }
        /// <summary>
        /// Start making the CreateAuthenticatedSessionToken call.
        /// </summary>
        /// <param name="state">The current state.</param>
        private static void StartCastCall(AuthenticationCheckState state)
        {
            XElement      info    = state.Service.CreateCastCallInfoSection();
            CHBaseRequest request = CHBaseRequest.Create("CreateAuthenticatedSessionToken", "2", info, CastCallCompleted);

            request.UserState = state;

            state.Service.BeginSendRequest(request);
        }
        /// <summary>
        /// Refresh the session token.
        /// </summary>
        /// <remarks>Makes a CAST call to get a new session token,
        /// and then re-issues the original request.</remarks>
        /// <param name="args">The request information.</param>
        private void RefreshSessionToken(SendPostEventArgs args)
        {
            AuthorizationSessionToken = null;

            XElement      info    = CreateCastCallInfoSection();
            CHBaseRequest request = CHBaseRequest.Create("CreateAuthenticatedSessionToken", "2", info, RefreshSessionTokenCompleted);

            request.UserState = args;

            BeginSendRequest(request);
        }
        /// <summary>
        /// Start getting the list of authorized people.
        /// </summary>
        /// <param name="state">The current state.</param>
        private static void StartGetAuthorizedPeople(AuthenticationCheckState state)
        {
            XElement info = new XElement("info",
                                         new XElement("parameters")
                                         );

            CHBaseRequest request = CHBaseRequest.Create("GetAuthorizedPeople", "1", info, GetAuthorizedPeopleCompleted);

            request.UserState = state;

            state.Service.BeginSendRequest(request);
        }
        /// <summary>
        /// Invoke the calling application's callback.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultText">The raw response.</param>
        /// <param name="response">The response object.</param>
        /// <param name="errorText">The error text, or null if successful. </param>
        private void InvokeApplicationResponseCallback(
            CHBaseRequest request,
            string resultText,
            CHBaseResponse response,
            string errorText)
        {
            CHBaseResponseEventArgs eventArgs = new CHBaseResponseEventArgs(request, resultText, response);

            eventArgs.ErrorText = errorText;

            if (request.ResponseCallback != null)
            {
                request.ResponseCallback(this, eventArgs);
            }
        }
        /// <summary>
        /// Return an instance of the class.
        /// </summary>
        /// <remarks>
        /// Called when HealthVaultService needs to create an instance; handles mocking through
        /// <see cref="EnableMocks"/>.
        /// </remarks>
        /// <param name="methodName">The name of the method.</param>
        /// <param name="methodVersion">The version of the method.</param>
        /// <param name="infoSection">The request-specific xml to pass.</param>
        /// <param name="responseCallback">The method to call when the request has completed.</param>
        /// <returns>An instance</returns>
        internal static CHBaseRequest Create(
            string methodName,
            string methodVersion,
            XElement infoSection,
            EventHandler <CHBaseResponseEventArgs> responseCallback)
        {
            CHBaseRequest request = new CHBaseRequest(methodName, methodVersion, infoSection, responseCallback);

            if (_mockRequests != null)
            {
                request.WebRequest = _mockRequests[0];
                _mockRequests.RemoveAt(0);
            }

            return(request);
        }
        /// <summary>
        /// Initializes a new instance of the HealthVaultResponseEventArgs class.
        /// </summary>
        /// <param name="request">The request that is being processed.</param>
        /// <param name="responseXml">The raw response xml from the request.</param>
        /// <param name="response">A deserialized version of the request.</param>
        public CHBaseResponseEventArgs(
            CHBaseRequest request,
            string responseXml,
            CHBaseResponse response)
        {
            Request = request;
            ResponseXml = responseXml;
            Response = response;

            if (responseXml != null)
            {
                XElement element = XElement.Parse(responseXml);

                XElement status = element.Element("status");

                ErrorCode = Int32.Parse(status.Element("code").Value);
            }
        }
        /// <summary>
        /// Initializes a new instance of the HealthVaultResponseEventArgs class.
        /// </summary>
        /// <param name="request">The request that is being processed.</param>
        /// <param name="responseXml">The raw response xml from the request.</param>
        /// <param name="response">A deserialized version of the request.</param>
        public CHBaseResponseEventArgs(
            CHBaseRequest request,
            string responseXml,
            CHBaseResponse response)
        {
            Request     = request;
            ResponseXml = responseXml;
            Response    = response;

            if (responseXml != null)
            {
                XElement element = XElement.Parse(responseXml);

                XElement status = element.Element("status");

                ErrorCode = Int32.Parse(status.Element("code").Value);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Begin to send a post request to a specific url.
        /// </summary>
        /// <param name="url">The target url.</param>
        /// <param name="requestData">The data to post to the url.</param>
        /// <param name="responseCallback">The completion routine.</param>
        /// <param name="userRequest">User parameter.</param>
        public virtual void BeginSendPostRequest(
            string url,
            string requestData,
            EventHandler <SendPostEventArgs> responseCallback,
            CHBaseRequest userRequest)
        {
            if (RequestResponseLogEnabled)
            {
                _requestResponseLog.Add(requestData);
            }

            SendPostEventArgs args = new SendPostEventArgs();

            args.RequestData        = requestData;
            args.HealthVaultRequest = userRequest;
            args.ResponseCallback   = responseCallback;

            if (userRequest.WebRequest == null)
            {
                args.WebRequest        = WebRequest.Create(url);
                args.WebRequest.Method = "POST";
            }
            else
            {
                // Mock case...
                args.WebRequest = userRequest.WebRequest;
            }

            try
            {
                IAsyncResult result = args.WebRequest.BeginGetRequestStream(GetRequestStreamCallback, args);
            }
            catch (Exception e)
            {
                InvokeResponseCallback(args, e.ToString());
                return;
            }
        }
        /// <summary>
        /// Return an instance of the class.
        /// </summary>
        /// <remarks>
        /// Called when HealthVaultService needs to create an instance; handles mocking through
        /// <see cref="EnableMocks"/>.
        /// </remarks>
        /// <param name="methodName">The name of the method.</param>
        /// <param name="methodVersion">The version of the method.</param>
        /// <param name="infoSection">The request-specific xml to pass.</param>
        /// <param name="responseCallback">The method to call when the request has completed.</param>
        /// <returns>An instance</returns>
        internal static CHBaseRequest Create(
            string methodName,
            string methodVersion,
            XElement infoSection,
            EventHandler<CHBaseResponseEventArgs> responseCallback)
        {
            CHBaseRequest request = new CHBaseRequest(methodName, methodVersion, infoSection, responseCallback);

            if (_mockRequests != null)
            {
                request.WebRequest = _mockRequests[0];
                _mockRequests.RemoveAt(0);
            }

            return request;
        }
        /// <summary>
        /// Begin to send a post request to a specific url. 
        /// </summary>
        /// <param name="url">The target url.</param>
        /// <param name="requestData">The data to post to the url.</param>
        /// <param name="responseCallback">The completion routine.</param>
        /// <param name="userRequest">User parameter.</param>
        public virtual void BeginSendPostRequest(
            string url,
            string requestData,
            EventHandler<SendPostEventArgs> responseCallback,
            CHBaseRequest userRequest)
        {
            if (RequestResponseLogEnabled)
            {
                _requestResponseLog.Add(requestData);
            }

            SendPostEventArgs args = new SendPostEventArgs();
            args.RequestData = requestData;
            args.HealthVaultRequest = userRequest;
            args.ResponseCallback = responseCallback;

            if (userRequest.WebRequest == null)
            {
                args.WebRequest = WebRequest.Create(url);
                args.WebRequest.Method = "POST";
            }
            else
            {
                    // Mock case...
                args.WebRequest = userRequest.WebRequest;
            }

            try
            {
                IAsyncResult result = args.WebRequest.BeginGetRequestStream(GetRequestStreamCallback, args);
            }
            catch (Exception e)
            {
                InvokeResponseCallback(args, e.ToString());
                return;
            }
        }
        /// <summary>
        /// Invoke the calling application's callback.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="resultText">The raw response.</param>
        /// <param name="response">The response object.</param>
        /// <param name="errorText">The error text, or null if successful. </param>
        private void InvokeApplicationResponseCallback(
            CHBaseRequest request,
            string resultText,
            CHBaseResponse response,
            string errorText)
        {
            CHBaseResponseEventArgs eventArgs = new CHBaseResponseEventArgs(request, resultText, response);
            eventArgs.ErrorText = errorText;

            if (request.ResponseCallback != null)
            {
                request.ResponseCallback(this, eventArgs);
            }
        }
        /// <summary>
        /// Send a request to the HealthVault web service.
        /// </summary>
        /// <remarks>
        /// This method returns immediately; the results and any error information will be passed to the
        /// completion method stored in the request.
        /// </remarks>
        /// <param name="request">The request to send.</param>
        public virtual void BeginSendRequest(
            CHBaseRequest request)
        {
            string requestXml = GenerateRequestXml(request);

            WebTransport transport = new WebTransport();
            transport.BeginSendPostRequest(HealthServiceUrl, requestXml, SendRequestCallback, request);
        }
        /// <summary>
        /// Generate the XML for a request.
        /// </summary>
        /// <param name="clientRequest">The request.</param>
        /// <returns>The XML representation.</returns>
        internal string GenerateRequestXml(CHBaseRequest clientRequest)
        {
            XElement request = XElement.Parse(@"<wc-request:request xmlns:wc-request=""urn:com.microsoft.wc.request"" />");

            XElement header = new XElement("header");
            {
                header.Add(new XElement("method", clientRequest.MethodName));
                header.Add(new XElement("method-version", clientRequest.MethodVersion));

                if (CurrentRecord != null)
                {
                    header.Add(new XElement("record-id", CurrentRecord.RecordId.ToString()));
                }

                if (!String.IsNullOrEmpty(AuthorizationSessionToken))
                {
                    XElement authSession = new XElement("auth-session");
                    authSession.Add(new XElement("auth-token", AuthorizationSessionToken));

                    if (CurrentRecord != null)
                    {
                        authSession.Add(new XElement("offline-person-info",
                                            new XElement("offline-person-id", CurrentRecord.PersonId.ToString())));
                    }

                    header.Add(authSession);
                }
                else
                {
                    if (AppIdInstance == Guid.Empty)
                    {
                        header.Add(new XElement("app-id", MasterAppId.ToString()));
                    }
                    else
                    {
                        header.Add(new XElement("app-id", AppIdInstance.ToString()));
                    }
                }

                header.Add(new XElement("language", Language));
                header.Add(new XElement("country", Country));
                header.Add(new XElement("msg-time", clientRequest.MessageTime.ToUniversalTime().ToString("O")));
                header.Add(new XElement("msg-ttl", "1800"));
                header.Add(new XElement("version", MobilePlatform.PlatformAbbreviationAndVersion));
            }

            XElement info = new XElement("info");
            if (clientRequest.InfoSection != null)
            {
                info = clientRequest.InfoSection;
            }

            if (clientRequest.MethodName != "CreateAuthenticatedSessionToken")
            {
                // if we have an info section, we need to compute the hash of that and put it in the header.
                if (clientRequest.InfoSection != null)
                {
                    string infoString = GetOuterXml(info);
                    header.Add(new XElement("info-hash",
                                    MobilePlatform.ComputeSha256HashAndWrap(infoString)));
                }

                if (!String.IsNullOrEmpty(SessionSharedSecret))
                {
                    byte[] sharedSecretKey = Convert.FromBase64String(SessionSharedSecret);
                    string headerXml = GetOuterXml(header);

                    request.Add(new XElement("auth",
                                    MobilePlatform.ComputeSha256HmacAndWrap(sharedSecretKey, headerXml)));
                }
            }

            request.Add(header);
            request.Add(info);

            string requestString = GetOuterXml(request);

            return requestString;
        }
        /// <summary>
        /// Generate the XML for a request.
        /// </summary>
        /// <param name="clientRequest">The request.</param>
        /// <returns>The XML representation.</returns>
        internal string GenerateRequestXml(CHBaseRequest clientRequest)
        {
            XElement request = XElement.Parse(@"<wc-request:request xmlns:wc-request=""urn:com.microsoft.wc.request"" />");

            XElement header = new XElement("header");
            {
                header.Add(new XElement("method", clientRequest.MethodName));
                header.Add(new XElement("method-version", clientRequest.MethodVersion));

                if (CurrentRecord != null)
                {
                    header.Add(new XElement("record-id", CurrentRecord.RecordId.ToString()));
                }

                if (!String.IsNullOrEmpty(AuthorizationSessionToken))
                {
                    XElement authSession = new XElement("auth-session");
                    authSession.Add(new XElement("auth-token", AuthorizationSessionToken));

                    if (CurrentRecord != null)
                    {
                        authSession.Add(new XElement("offline-person-info",
                                                     new XElement("offline-person-id", CurrentRecord.PersonId.ToString())));
                    }

                    header.Add(authSession);
                }
                else
                {
                    if (AppIdInstance == Guid.Empty)
                    {
                        header.Add(new XElement("app-id", MasterAppId.ToString()));
                    }
                    else
                    {
                        header.Add(new XElement("app-id", AppIdInstance.ToString()));
                    }
                }

                header.Add(new XElement("language", Language));
                header.Add(new XElement("country", Country));
                header.Add(new XElement("msg-time", clientRequest.MessageTime.ToUniversalTime().ToString("O")));
                header.Add(new XElement("msg-ttl", "1800"));
                header.Add(new XElement("version", MobilePlatform.PlatformAbbreviationAndVersion));
            }

            XElement info = new XElement("info");

            if (clientRequest.InfoSection != null)
            {
                info = clientRequest.InfoSection;
            }

            if (clientRequest.MethodName != "CreateAuthenticatedSessionToken")
            {
                // if we have an info section, we need to compute the hash of that and put it in the header.
                if (clientRequest.InfoSection != null)
                {
                    string infoString = GetOuterXml(info);
                    header.Add(new XElement("info-hash",
                                            MobilePlatform.ComputeSha256HashAndWrap(infoString)));
                }

                if (!String.IsNullOrEmpty(SessionSharedSecret))
                {
                    byte[] sharedSecretKey = Convert.FromBase64String(SessionSharedSecret);
                    string headerXml       = GetOuterXml(header);

                    request.Add(new XElement("auth",
                                             MobilePlatform.ComputeSha256HmacAndWrap(sharedSecretKey, headerXml)));
                }
            }

            request.Add(header);
            request.Add(info);

            string requestString = GetOuterXml(request);

            return(requestString);
        }
        void c_SaveWeight_Click(object sender, RoutedEventArgs e)
        {
            string thingXml = @"<info><thing>
                <type-id>3d34d87e-7fc1-4153-800f-f56592cb0d17</type-id>
                <thing-state>Active</thing-state>
                <flags>0</flags>
                <data-xml>
                    <weight>
                        {0}
                        <value>
                            <kg>{1}</kg>
                            <display units=""pounds"">{2}</display>
                        </value>
                    </weight>
                    <common/>
                </data-xml>
            </thing></info>";

            double weight = Double.Parse(c_textWeight.Text);
            c_textWeight.Text = "";
            string whenString = GetDateTime(DateTime.Now, "when");
            string xml = String.Format(thingXml, whenString, weight / 2.204, weight);

            XElement info = XElement.Parse(xml);

            CHBaseRequest request = new CHBaseRequest("PutThings", "2", info, PutThingsCompleted);

            App.HealthVaultService.BeginSendRequest(request);
            c_progressBar.Visibility = Visibility.Visible;
        }
        void GetThingsStart()
        {
            string thingXml = @"        <info>
            <group>
                <filter>
                    <type-id>3d34d87e-7fc1-4153-800f-f56592cb0d17</type-id>
                    <thing-state>Active</thing-state>
                </filter>
                <format>
                    <section>core</section>
                    <xml/>
                    <type-version-format>3d34d87e-7fc1-4153-800f-f56592cb0d17</type-version-format>
                </format>
            </group>
        </info>";

            XElement info = XElement.Parse(thingXml);

            CHBaseRequest request = new CHBaseRequest("GetThings", "3", info, GetThingsCompleted);

            App.HealthVaultService.BeginSendRequest(request);
            SetProgressBarVisibility(true);
        }
        void GetPersonalImageStart()
        {
            string thingXml = @"        <info>
        <group>
            <filter>
                <type-id>a5294488-f865-4ce3-92fa-187cd3b58930</type-id>
                <thing-state>Active</thing-state>
            </filter>
            <format>
                <section>core</section>
                <section>blobpayload</section>
                <type-version-format>a5294488-f865-4ce3-92fa-187cd3b58930</type-version-format>
                <blob-payload-request>
                    <blob-format>
                        <blob-format-spec>inline</blob-format-spec>
                    </blob-format>
                </blob-payload-request>
            </format>
        </group>
        </info>";

            XElement info = XElement.Parse(thingXml);

            CHBaseRequest request = new CHBaseRequest("GetThings", "3", info, GetPersonalImageCompleted);

            App.HealthVaultService.BeginSendRequest(request);
            SetProgressBarVisibility(true);
        }
        void c_ClearWeight_Click(object sender, RoutedEventArgs e)
        {
            XElement info = new XElement("info");

            foreach (string thingIdText in _currentThingIds)
            {
                info.Add(XElement.Parse(thingIdText));
            }

            CHBaseRequest request = new CHBaseRequest("RemoveThings", "1", info, RemoveThingsCompleted);

            App.HealthVaultService.BeginSendRequest(request);
            SetProgressBarVisibility(true);
        }