Example #1
0
        public SearchRs GetByName(string name)
        {
            //string url = "http://api.cokecce.com/v1/product/search?query=%2AFANTA%2A&maxRows=20&language=EN&format=json&apiKey=uqjnugxp66c2qs6tburyy42b";

            string url = string.Format("{0}?query=*{1}*&maxRows={2}&language={3}&format={4}&apiKey={5}", "http://api.cokecce.com/v1/product/search", name, "20", "EN", "json", "uqjnugxp66c2qs6tburyy42b");

            SearchRs   searchResponse = new SearchRs();
            HttpClient client         = new HttpClient();

            client.BaseAddress = new Uri(url);

            // Add an Accept header for JSON format.

            // List data response.
            HttpResponseMessage response = client.GetAsync(url).Result;  // Blocking call!

            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var products = response.Content.ReadAsAsync <Rootobject>().Result;
                searchResponse.Products = products;
                searchResponse.Status   = true;
            }
            else
            {
                searchResponse.Status = false;
                //                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
            return(searchResponse);
        }
Example #2
0
 internal void Search(String search)
 {
     if (_appContext.ConnectionManager.IsOnline())
     {
         SearchRq request = new SearchRq()
         {
             query = search, pageSize = 20
         };
         Logger.Debug("Searching for " + search + " with id " + request.id);
         try
         {
             OnSearchStart();
             currentSearchId = request.id;
             SearchRs response = _appContext.ConnectionManager.Connection.Request <SearchRq, SearchRs>(request);
             // only process results if we are the current search
             if (currentSearchId == request.id)
             {
                 OnSearchStop();
                 Logger.Debug("Processing search results for " + search + " with id " + request.id);
                 // populate the list
                 lock (_listWriteLock)
                 {
                     SearchResults.Clear();
                     if (response.results != null)
                     {
                         foreach (var resultElement in response.results)
                         {
                             var searchResult = new SearchResult(resultElement);
                             SearchResults.Add(searchResult);
                             if (resultElement.user.hasAvatar)
                             {
                                 try
                                 {
                                     _appContext.ConnectionManager.Connection.RequestAsync <VCardRq, VCardRs>(
                                         new VCardRq(resultElement.user.user),
                                         delegate(VCardRq rq, VCardRs rs, Exception arg3)
                                     {
                                         ResponseHandler(rq, rs, arg3, searchResult);
                                     });
                                 }
                                 catch (Exception ex)
                                 {
                                     Logger.Error("Failed to get the vcard for " + resultElement.user.user, ex);
                                 }
                             }
                         }
                     }
                 }
             }
             else
             {
                 Logger.Debug("Will not process search results, a search has superceded my search for " + search);
             }
         }
         catch (Exception e)
         {
             if (currentSearchId == request.id)
             {
                 OnSearchStop();
             }
             Logger.Error("Search for " + search + " [id=" + request.id + "] returned an error : " + e.Message, e);
         }
         finally
         {
             if (currentSearchId == request.id)
             {
                 currentSearchId = null;
             }
         }
     }
     else
     {
         Logger.Warn("Cannot search for " + search + ", not online");
     }
 }
Example #3
0
 private void SearchResponseHandler(SearchRq request, SearchRs response, Exception e)
 {
 }