Subscribe RSS Join our Facebook Group Follow us on Twitter!
in Search

Izzuddin Gumilar's Blog

#Using RestSharp Library for Simplify REST and HTTP API Client

When we build an application in .net, sometimes we need to get external data from HTTP API web services such as Bing API or Google API service to support our application programs. HTTP API usually return json or xml format to get data value from it services. So we must read json or xml node then parse it to our object entity to get returned value from HTTP API that we access. To get right return value from HTTP API services, we also must set or specify input parameter for HTTP API service that we access. It’s so complicated if we code manually for accessing HTTP API in our application.


RestSharp is open source library for Rest and HTTP API client for .net. It is used to help us when we access Rest and HTTP Api web services. We can download it for free from https://github.com/johnsheehan/RestSharp/archives/master. I will show you how to use this library on Windows Phone 7 Silverlight Application to get Google News API . Let’s start..


Prepare your windows phone 7 project application. We put some user control to the windows phone page like text box to get news keyword, button to handle event for call method, and listbox to display data result.

Add References 

Don't forget add RestSharp library references (Newtonsoft.Json.Silverlight and RestSharp.WindowsPhone) to our project.

Add References
 

 Add New Entity Class name it “SearchResult.cs” to represent news data object getting from google news api.

  1 public class SearchResult
2 {
3 public string Title { get; set; }
4 public string Content { get; set; }
5 public string Url { get; set; }
6 public string TitleNoFormatting { get; set; }
7 public string Publisher { get; set; }
8 public string UnescapedUrl { get; set; }
9 public DateTime PublishedDate { get; set; }
10 }

Add LoadNews method to get http api request from rest client.

  1 public void LoadNews(string keyword)
2 {
3 var client = new RestClient("https://ajax.googleapis.com/ajax/services/search/news");
4 var request = new RestRequest(Method.GET);
5 request.AddParameter("v", "1.0");
6 request.AddParameter("q", keyword);
7 request.AddParameter("hl", "id");
8 request.AddParameter("rsz", 5);
9
10 client.ExecuteAsync<SearchResult>(request, (response) =>
11 {
12 try
13 {
14 var resource = response.Data;
15 listBox1.Items.Clear();
16
17 JObject googleSearch = JObject.Parse(response.Content);
18 JEnumerable<JToken> results = googleSearch["responseData"]["results"].Children();
19 IList<SearchResult> searchResults = new List<SearchResult>();
20 foreach (JToken result in results)
21 {
22 SearchResult searchResult = JsonConvert.DeserializeObject<SearchResult>(result.ToString());
23 searchResults.Add(searchResult);
24 }
25
26 foreach (SearchResult t in searchResults)
27 {
28 listBox1.Items.Add(HttpUtility.HtmlDecode(t.TitleNoFormatting));
29 }
30
31
32 if (searchResults.Count == 0)
33 {
34 MessageBox.Show("Sorry, empty result.");
35 }
36 }
37 catch (Exception e)
38 {
39 MessageBox.Show(e.Message);
40 }
41 });
42
43 }

 Then add event button click to get data from google API based on keyword from input user.

  1 private void button1_Click(object sender, RoutedEventArgs e)
2 {
3 LoadNews(textBox1.Text);
4 }

Oke lets try…

 Input Keyword Result
You can download the source code here.. :)

2,560 Views, 2 Comment(s), Published on: 03-16-2011 23:55 by izzuddin to Izzuddin Gumilar's Blog

Comments

 

wildbohr said:

Great instructions here. I am just about to start work with some required REST work and this has been a big help. I did want to ask when I ran your code I keep coming back with responseStatus equal to error and therefore no content. When I run the url in a browser window things are fine. I am trying to run it with RestSharp 101.0 which might be the issue. I'll take a closer look tomorrow, but wanted to check to see if you ran into anything like this when creating your example.

April 3, 2011 9:53 PM
 

wildbohr said:

I confirmed the issue is with the new implementations in 101.0, by reverting to 100.3 the example works fine.

April 4, 2011 9:38 PM

About izzuddin

Izzuddin Gumilar Aprilian
Business System Application Services - PT. Berau Coal
ex-Microsoft Student Partner Lead - East Java Regional

Web : http://izzuddin.net/
Blog : http://students.netindonesia.net/blogs/izzuddin/
Facebook : http://www.facebook.com/izzuddin.cs
Email : izzuddin_cs@yahoo.co.id