Beispiel #1
0
        /// <summary>
        /// Returns a list of business candidates based on search key.
        /// </summary>
        /// <param name="searchKey">search key eg. microsoft</param>
        /// <returns>Returns a list of candidates.</returns>
        public List <Candidate> SearchBusinessCandidates(string searchKey)
        {
            List <Candidate> candidates = new List <Candidate>();

            try
            {
                //Build the API  request to be send.
                _Request             = WebRequest.Create(new Uri(String.Format("{0}{1}", _DnBBasicURL, StatementBuilder.FindCompany(searchKey))));
                _Request.Method      = "GET";
                _Request.ContentType = "application/json; charset=UTF-8";
                _Request.Headers.Add("Authorization", _Token);
                //Call the API and get a response.
                HttpWebResponse response = (HttpWebResponse)_Request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        //Get Json
                        string json = streamReader.ReadToEnd();
                        //Deserialized Json
                        var foundCompany = JsonConvert.DeserializeObject <DnB.NET.JsonModels.SearchMatch.SearchMatchResponse>(json);
                        if (foundCompany == null)
                        {
                            _ErrorMessage = "No results found";
                            return(null);
                        }

                        if (foundCompany.MatchResponse.TransactionResult.ResultText.Equals("Success"))
                        {
                            List <Candidate> results = Assemble.Build(foundCompany.MatchResponse.MatchResponseDetail.MatchCandidate);
                            if (results == null || results.Count <= 0)
                            {
                                _ErrorMessage = "No results found. Couldn't Build Object from JSON.";
                                return(null);
                            }

                            candidates = results;
                        }
                        else
                        {
                            _ErrorMessage = "API Call Success. Couldn't read data.";
                            return(null);
                        }
                    }
                }
                response.Close();
            }
            catch (WebException wex)
            {
                HttpWebResponse resp = (HttpWebResponse)wex.Response;
                if (resp.StatusCode == HttpStatusCode.NotFound)
                {
                    _ErrorMessage = "Entity not found";
                    return(null);
                }
                else
                {
                    using (var streamReader = new StreamReader(resp.GetResponseStream()))
                    {
                        var errAPI = JsonConvert.DeserializeObject <DnB.NET.JsonModels.JsonError.ErrorAPICall>(streamReader.ReadToEnd());
                        _ErrorMessage = String.Format("API CALL ERROR: {0} - {1}", errAPI.MatchResponse.TransactionResult.ResultText, errAPI.MatchResponse.TransactionResult.ResultMessage[0].ResultDescription);
                    }
                    return(null);
                }
            }
            catch (JsonSerializationException jex)
            {
                _ErrorMessage = "Json De-serialization exception.";
                return(null);
            }
            catch (Exception ex)
            {
                _ErrorMessage = ex.Message;
                return(null);
            }

            return(candidates);
        }