/// <summary>
 /// PutThings Method
 /// </summary>
 /// <param name="item">The health item to upload</param>
 /// <param name="responseCallback">Function to resolve callback</param>
 public static void PutThings(HealthRecordItemModel item,
     EventHandler<HealthVaultResponseEventArgs> responseCallback)
 {
     XElement info = XElement.Parse(item.GetXml());
     HealthVaultRequest request = new HealthVaultRequest("PutThings", "2", info, responseCallback);
     App.HealthVaultService.BeginSendRequest(request);
 }
        /// <summary>
        /// Get things Method
        /// </summary>
        /// <param name="typeId">HealthVault Type Id</param>
        /// <param name="responseCallback">Response Handler for the Type</param>
        public static void GetThings(string typeId,
            int? maxItems,
            DateTime? effDateMin,
            DateTime? effDateMax,
            EventHandler<HealthVaultResponseEventArgs> responseCallback)
        {
            string thingXml = @"
            <info>
                <group {0}>
                    <filter>
                        <type-id>{1}</type-id>
                        <thing-state>Active</thing-state>
                        {2}
                        {3}
                    </filter>
                    <format>
                        <section>core</section>
                        <xml/>
                        <type-version-format>{1}</type-version-format>
                    </format>
                </group>
            </info>";

            XElement info = XElement.Parse(string.Format
                (thingXml,
                MaxItemsXml(maxItems),
                typeId,
                EffDateMinXml(effDateMin),
                EffDateMaxXml(effDateMax)));
            HealthVaultRequest request = new HealthVaultRequest("GetThings", "3", info, responseCallback);
            App.HealthVaultService.BeginSendRequest(request);
        }
Example #3
0
        /// <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)
        {
            HealthVaultRequest request = HealthVaultRequest.Create("NewApplicationCreationInfo", "1", null, NewApplicationCreationInfoCompleted);

            request.UserState = state;

            state.Service.BeginSendRequest(request);
        }
Example #4
0
        /// <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();
            HealthVaultRequest request = HealthVaultRequest.Create("CreateAuthenticatedSessionToken", "2", info, CastCallCompleted);

            request.UserState = state;

            state.Service.BeginSendRequest(request);
        }
Example #5
0
        /// <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(
            HealthVaultRequest request)
        {
            string requestXml = GenerateRequestXml(request);

            WebTransport transport = new WebTransport();

            transport.BeginSendPostRequest(HealthServiceUrl, requestXml, SendRequestCallback, request);
        }
Example #6
0
        /// <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();
            HealthVaultRequest request = HealthVaultRequest.Create("CreateAuthenticatedSessionToken", "2", info, RefreshSessionTokenCompleted);

            request.UserState = args;

            BeginSendRequest(request);
        }
Example #7
0
        /// <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")
                                         );

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

            request.UserState = state;

            state.Service.BeginSendRequest(request);
        }
Example #8
0
        /// <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(
            HealthVaultRequest request,
            string resultText,
            HealthVaultResponse response,
            string errorText)
        {
            HealthVaultResponseEventArgs eventArgs = new HealthVaultResponseEventArgs(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 HealthVaultRequest Create(
            string methodName,
            string methodVersion,
            XElement infoSection,
            EventHandler <HealthVaultResponseEventArgs> responseCallback)
        {
            HealthVaultRequest request = new HealthVaultRequest(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 HealthVaultResponseEventArgs(
            HealthVaultRequest request,
            string responseXml,
            HealthVaultResponse 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);
            }
        }
Example #11
0
        /// <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 HealthVaultResponseEventArgs(
            HealthVaultRequest request,
            string responseXml,
            HealthVaultResponse 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);
            }
        }
Example #12
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,
            HealthVaultRequest 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;
            }
        }
Example #13
0
        /// <summary>
        /// Generate the XML for a request.
        /// </summary>
        /// <param name="clientRequest">The request.</param>
        /// <returns>The XML representation.</returns>
        internal string GenerateRequestXml(HealthVaultRequest 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>
        /// 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,
            HealthVaultRequest 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>
 /// PutThings Method
 /// HACK: Need to fix hardcoding of the type
 /// </summary>
 /// <param name="item">The health item to upload</param>
 /// <param name="responseCallback">Function to resolve callback</param>
 public static void PutThings(IEnumerable<BloodPressureModel> items,
     EventHandler<HealthVaultResponseEventArgs> responseCallback)
 {
     string itemXml = "";
     foreach (HealthRecordItemModel item in items)
     {
         itemXml += item.GetXml();
     }
     XElement info = XElement.Parse("<info>" + itemXml + "</info>");
     HealthVaultRequest request = new HealthVaultRequest("PutThings", "2", info, responseCallback);
     App.HealthVaultService.BeginSendRequest(request);
 }
        /// <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 HealthVaultRequest Create(
            string methodName,
            string methodVersion,
            XElement infoSection,
            EventHandler<HealthVaultResponseEventArgs> responseCallback)
        {
            HealthVaultRequest request = new HealthVaultRequest(methodName, methodVersion, infoSection, responseCallback);

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

            return request;
        }