/// <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()); }
/// <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; } } }