Example #1
0
 /// <summary>
 /// Helper method to append text to the display.
 /// </summary>
 /// <param name="title">Title of the search.</param>
 /// <param name="searchRequest">The Search Request instance.</param>
 private void AppendSearch(string title, DSSearchRequest searchRequest)
 {
     SampleSearchText.Text += string.Format(
         "<h2>{0}</h2><div>{1}</div>",
         title,
         DSSerializer.Serialize(json, searchRequest));
 }
        /// <summary>
        /// Handles the Click event of the Execute button.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Execute_Click(object sender, EventArgs e)
        {
            JSONRequest.Visible = false;

            var searchRequest = DSSerializer.Deserialize <DSSearchRequest>(json, jsonText.Text);

            var response = api.ExecuteSearch(searchRequest);

            ResultText.Text      = DSSerializer.Serialize(json, response);
            JSONResponse.Visible = true;
        }
        /// <summary>
        /// Execute the supplied search request.
        /// </summary>
        /// <param name="searchRequest">Incoming Search Request.</param>
        /// <returns>Search results or errors.</returns>
        public DSSearchResponse ExecuteSearch(DSSearchRequest searchRequest)
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = client.PostAsJsonAsync("api/search", searchRequest);

            response.Wait();

            if (response.Exception != null)
            {
                var errReturn = new DSSearchResponse();
                errReturn.Errors.Add(response.Exception.Message + response.Exception.StackTrace);
                return(errReturn);
            }

            if (!response.Result.IsSuccessStatusCode)
            {
                try
                {
                    var error = response.Result.Content.ReadAsAsync <Dictionary <string, string> >();
                    error.Wait();

                    var errReturn = new DSSearchResponse();
                    errReturn.Errors = new List <string> {
                        error.Result["ExceptionMessage"] + ": " + error.Result["StackTrace"]
                    };
                    return(errReturn);
                }
                catch
                {
                    // If unable to get specific error data, dump whole message as string
                    var error = response.Result.Content.ReadAsStringAsync();
                    error.Wait();

                    var errReturn = new DSSearchResponse();
                    errReturn.Errors = new List <string> {
                        error.Result
                    };
                    return(errReturn);
                }
            }

            var contents = response.Result.Content.ReadAsStringAsync();

            contents.Wait();

            return(DSSerializer.Deserialize <DSSearchResponse>(json, contents.Result));
        }