/// <summary>
        /// Generates the XML for the parameters for the "GetThings" request.
        /// </summary>
        ///
        /// <returns>
        /// An XML string defining the parameters for the "GetThings" call.
        /// </returns>
        ///
        /// <exception cref="HealthServiceException">
        /// No filters have been specified.
        /// </exception>
        ///
        internal string GetParametersXml()
        {
            if (Filters.Count == 0)
            {
                HealthServiceResponseError error = new HealthServiceResponseError();
                error.Message = Resources.HealthRecordSearcherNoFilters;

                HealthServiceException e =
                    HealthServiceExceptionHelper.GetHealthServiceException(
                        HealthServiceStatusCode.InvalidFilter, error);
                throw e;
            }

            StringBuilder parameters = new StringBuilder(128);

            XmlWriterSettings settings = SDKHelper.XmlUnicodeWriterSettings;

            using (XmlWriter writer = XmlWriter.Create(parameters, settings))
            {
                foreach (ThingQuery filter in Filters)
                {
                    // Add all filters
                    filter.AddFilterXml(writer);
                }

                writer.Flush();
            }

            return(parameters.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Checks whether at least one property is set.
        /// </summary>
        ///
        /// <exception cref="HealthServiceException">
        /// One of the following is true:
        /// (1) The filter has no properties set;
        /// (2) Both ItemIds and ItemKeys are specified; or
        /// (3) There are more than the allowable number of order by clauses.
        /// </exception>
        ///
        internal void ThrowIfNotValid()
        {
            bool isValid = AreFiltersPresent();

            // if no filters are present,
            // at least one of ItemKeys or ItemIds should be non-empty
            isValid |=
                ItemKeys.Count != 0 ||
                ItemIds.Count != 0 ||
                ClientItemIds.Count != 0;

            if (!isValid)
            {
                HealthServiceResponseError error = new HealthServiceResponseError
                {
                    Message = Resources.HealthRecordSearcherInvalidFilter
                };

                HealthServiceException e =
                    HealthServiceExceptionHelper.GetHealthServiceException(
                        HealthServiceStatusCode.InvalidFilter,
                        error);
                throw e;
            }

            int idTypesSpecified =
                ItemKeys.Count > 0 ? 1 : 0 +
                ItemIds.Count > 0 ? 1 : 0 +
                ClientItemIds.Count > 0 ? 1 : 0;

            // only one of ItemKeys or ItemIds can be non-empty
            // throw a specific error in this particular case
            if (idTypesSpecified > 1)
            {
                HealthServiceResponseError error = new HealthServiceResponseError
                {
                    Message = Resources.HealthRecordSearcherInvalidFilterIdsAndKeysSpecified
                };

                HealthServiceException e = HealthServiceExceptionHelper.GetHealthServiceException(
                    HealthServiceStatusCode.InvalidFilter,
                    error);
                throw e;
            }

            if (OrderByClauses.Count > 1)
            {
                HealthServiceResponseError error = new HealthServiceResponseError
                {
                    Message = Resources.HealthRecordSearcherInvalidOrderSpecified
                };

                HealthServiceException e =
                    HealthServiceExceptionHelper.GetHealthServiceException(
                        HealthServiceStatusCode.InvalidFilter,
                        error);
                throw e;
            }
        }
        /// <summary>
        /// <see cref="IHealthServiceResponseParser.ParseResponseAsync"/>
        /// </summary>
        public async Task <HealthServiceResponseData> ParseResponseAsync(HttpResponseMessage response)
        {
            using (Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
            {
                using (var reader = new StreamReader(responseStream, Encoding.UTF8, false, 1024))
                {
                    HealthServiceResponseData result =
                        new HealthServiceResponseData {
                        ResponseHeaders = response.Headers
                    };

                    XmlReaderSettings settings = SDKHelper.XmlReaderSettings;
                    settings.CloseInput       = false;
                    settings.IgnoreWhitespace = false;

                    XmlReader xmlReader = XmlReader.Create(reader, settings);
                    xmlReader.NameTable.Add("wc");

                    if (!SDKHelper.ReadUntil(xmlReader, "code"))
                    {
                        throw new MissingFieldException("code");
                    }

                    result.CodeId = xmlReader.ReadElementContentAsInt();

                    if (result.Code == HealthServiceStatusCode.Ok)
                    {
                        if (xmlReader.ReadToFollowing("wc:info"))
                        {
                            result.InfoNavigator = new XPathDocument(xmlReader).CreateNavigator();
                            result.InfoNavigator.MoveToFirstChild();
                        }

                        return(result);
                    }

                    result.Error = HandleErrorResponse(xmlReader);

                    HealthServiceException healthServiceException =
                        HealthServiceExceptionHelper.GetHealthServiceException(result);

                    throw healthServiceException;
                }
            }
        }
        /// <summary>
        /// Helper method that allows the SDK to throw the appropriate
        /// HealthServiceException based on the status code returned by 
        /// HealthVault.
        /// </summary>
        /// 
        /// <param name="errorCodeId">
        /// The integer status code returned by HealthVault representing the 
        /// error which occurred.
        ///</param>
        /// <param name="error">
        /// Information about an error that occurred while processing
        /// the request.
        /// </param>
        /// 
        internal static HealthServiceException GetHealthServiceException(int errorCodeId,
            HealthServiceResponseError error)
        {
            HealthServiceException e = null;
            HealthServiceStatusCode errorCode =
                HealthServiceStatusCodeManager.GetStatusCode(errorCodeId);

            if (errorCode != HealthServiceStatusCode.UnmappedError)
            {
                e = GetHealthServiceException(errorCode, error);
            }
            else
            {
                e = new HealthServiceException(errorCodeId, error);
            }

            return e;
        }
 /// <summary>
 /// Helper method that allows the SDK to throw the appropriate
 /// HealthServiceException based on the status code indicating the error
 /// type.
 /// </summary>
 /// 
 /// <param name="errorCode">
 /// The status code representing the error which occurred.
 /// </param>
 /// 
 /// <param name="error">
 /// Information about an error that occurred while processing
 /// the request.
 /// </param>
 /// 
 internal static HealthServiceException GetHealthServiceException(
     HealthServiceStatusCode errorCode,
     HealthServiceResponseError error)
 {
     HealthServiceException e = null;
     switch (errorCode)
     {
         case HealthServiceStatusCode.CredentialTokenExpired:
             e = new HealthServiceCredentialTokenExpiredException(error);
             break;
         case HealthServiceStatusCode.AuthenticatedSessionTokenExpired:
             e = new HealthServiceAuthenticatedSessionTokenExpiredException(error);
             break;
         case HealthServiceStatusCode.InvalidPerson:
             e = new HealthServiceInvalidPersonException(error);
             break;
         case HealthServiceStatusCode.InvalidRecord:
             e = new HealthServiceInvalidRecordException(error);
             break;
         case HealthServiceStatusCode.AccessDenied:
             e = new HealthServiceAccessDeniedException(error);
             break;
         case HealthServiceStatusCode.InvalidApplicationAuthorization:
             e = new HealthServiceInvalidApplicationAuthorizationException(error);
             break;
         case HealthServiceStatusCode.DuplicateCredentialFound:
             e = new HealthServiceApplicationDuplicateCredentialException(error);
             break;
         case HealthServiceStatusCode.MailAddressMalformed:
             e = new HealthServiceMailAddressMalformedException(error);
             break;
         case HealthServiceStatusCode.PasswordNotStrong:
             e = new HealthServicePasswordNotStrongException(error);
             break;
         case HealthServiceStatusCode.RecordQuotaExceeded:
             e = new HealthServiceRecordQuotaExceededException(error);
             break;
         case HealthServiceStatusCode.OtherDataItemSizeLimitExceeded :
             e = new HealthServiceOtherDataSizeLimitExceededException(error);
             break;
         default:
             e = new HealthServiceException(errorCode, error);
             break;
     }
     return e;
 }