private Uri MakeUriForSearching(VeNomQuery query)
        {
            var encoded    = HttpUtility.UrlEncode(query.SearchExpression);
            var searchBase = _sessionAddress + "search/" + encoded;

            var queryStringParameters = new Dictionary <string, string>();

            if (query.FilterSubset.Count > 0)  //server expects this format subset=[1,9,3]
            {
                queryStringParameters["subset"] = query.FilterSubset.Aggregate("[", (acc, id) => String.Format("{0}{1},", acc, id)).TrimEnd(',') + "]";
            }

            if (query.Skip.HasValue)
            {
                queryStringParameters["skip"] = query.Skip.Value.ToString();
            }

            if (query.Take.HasValue)
            {
                queryStringParameters["take"] = query.Take.Value.ToString();
            }

            if (!queryStringParameters.Any())
            {
                return(new Uri(searchBase));
            }

            var queryString = queryStringParameters.Aggregate("?", (acc, kv) => String.Format("{0}{1}={2}&", acc, kv.Key, kv.Value)).TrimEnd('&');

            return(new Uri(searchBase + queryString));
        }
        /// <summary>
        ///     Asynchronously queries the VetCompass webservice.  The task can be used for error detection/handling.  This call
        ///     won't block or throw an exception.
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public Task <VeNomQueryResponse> QueryAsync(VeNomQuery query)
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var task = _sessionCreationTask.FlatMapSuccess(_ => Query(query), cancellationTokenSource.Token);

            return(HonourTimeout(task, cancellationTokenSource));
        }
        /// <summary>
        ///     Queries the VetCompass webservice synchronously.  This will block your thread, instead prefer QueryAsync.  This
        ///     method will also throw an exception on a failure.
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public VeNomQueryResponse QuerySynch(VeNomQuery query)
        {
            var queryAsync = QueryAsync(query);

            Task.WaitAny(queryAsync); //will throw on a task fault
            return(queryAsync.Result);
        }
        /// <summary>
        ///     This does the actual query call to the server
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private Task <VeNomQueryResponse> Query(VeNomQuery query)
        {
            var searchBase = MakeUriForSearching(query);

            var request = CreateRequest(searchBase);

            request.Method = WebRequestMethods.Http.Get;
            request.Accept = "application/json";
            var task = request.GetResponseAsync();

            return(task.MapSuccess(DeserialiseQueryReponse));
        }