Friday, November 25, 2011

YELP API in C#

Hi Every one,

Yelp is a power full API to get restaurants details and show it to your site and when it comes to c# most of users are pointed to API version @ YELP API and face a night mare of no support so i have used certain features of this API and added simple functionality to parse by JSON.net.

So First down load YELP API in c# and then download bineris form JSON.net include both in user visual studio solution and reference to your website.

Now open the YELP API code and add the following functions in class YelpSearchService.

public static string SearchLocationWithString(String location, String category, String term)
        {
            return SearchLocationWithString(location, category, term, MaxResultCount);
        }

        public static string SearchLocationWithString(String location, String category, String term, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("location", location);
            request.Parameters.Add("category", category);
            request.Parameters.Add("term", term);
            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequestWithString();
        }


After this add the following functions in class YelpRequest.


public string ExecuteRequestWithString()
        {
            this.CreateRequest();

            Monitor.Enter(YelpServices.GlobalLock);

            try
            {
                TimeSpan timeSinceLastRequest = DateTime.Now.Subtract(YelpServices.LastRequest);

                if (timeSinceLastRequest.TotalSeconds < 2.0)
                {
                    Thread.Sleep(2000 - (Int32)timeSinceLastRequest.TotalMilliseconds);
                }

                return this.getResponse((HttpWebResponse)this.m_HttpRequest.GetResponse());
            }
            finally
            {
                YelpServices.LastRequest = DateTime.Now;

                Monitor.Exit(YelpServices.GlobalLock);
            }
        }


        private string getResponse(HttpWebResponse WebResp)
        {

            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            return _Answer.ReadToEnd();

        
        }

Now its time to go to page where you want to show restaurants Data just call the newly created functions and pass then over to JSON.NET



YelpServices.APIKey = "YOUR API KEY";
            string json = YelpSearchService.SearchLocationWithString("RESTURANT ADDRESS", "",
             "CATEGORY/NAME OF RESTURANT");
            bool hasReviews = false;
            List<Business> bList = new List<Business>();

            JObject o = JObject.Parse(json);

            IList<object> reviews = o.SelectToken("businesses").Select(s => (object)s).ToList<object>();
            foreach (Object j in reviews)
            {
                try
                {
                    Business deserializedProduct = JsonConvert.DeserializeObject<Business>(j.ToString());
                    //Review r = (Review)j;
                    bList.Add(deserializedProduct);
                }
                catch (Exception)
                {

                }

            }


And you will get all the business in the YELP API business class if you find Nulls in values try changing the property names in Business calls to the ones you find in JSON response.

Hope it helps.

Happy Coding.. :)