This project is read-only.

Using WebBuddy

Here is a crude example using AjaxBuddy and UriBuddy which call the Yahoo Image Search API. This example uses Json.NET for JSON serialization.
// Go ahead, make a query... 
public void MakeQuery()
{
	string requestUrl = "http://search.yahooapis.com/ImageSearchService/V1/imageSearch";

	// Ask Yahoo about snow in Seattle and call us back with the strongly typed results...
	Dictionary<String, String> query = new Dictionary<string, string>
	{
	    {"appid","YahooDemo"},
	    {"query","Snow In Seattle"},
	    {"results","20"},
	    {"output","json"}
	};

	// by the way, see that stuff above?  UriBuddy will turn it all into this:
	// http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Snow%20In%20Seattle&output=json&results=20
	// ... nice, huh?
		
	AjaxBuddy buddy = new AjaxBuddy();
	
	// Notice how this looks kinda like jQuery?  Notice how there isn't any fuss with WebClient?
	buddy.Get<YahooImageResult>(new Uri(requestUrl), query, new GetResponseCallback<YahooImageResult>(queryCallback), null);
	
}

// This is our callback when the AJAX request has completed.  Notice it passes in a strongly
// typed result?  Nice, huh?
private void queryCallback(YahooImageResult queryResults, object userState)
{
	Console.WriteLine("Got back {0} results", queryResults.Results.totalResultsReturned);
    
}


// These classes will contain the deserialized results from Yahoo
// NOTE: The JsonObject and JsonProperty attributes come from Json.NET, which is a sweet
// serialization library
[JsonObject(MemberSerialization.OptIn )]
public class YahooImageResult
{
    [JsonProperty("ResultSet")]
    public ResultSet Results { get; set; }
}

[JsonObject(MemberSerialization=MemberSerialization.OptIn,Title="ResultSet",Id="ResultSet",Description="The result set")]
public class ResultSet
{
    [JsonProperty]
    public int totalResultsAvailable { get; set; }

    [JsonProperty]
    public int totalResultsReturned { get; set; }

    [JsonProperty]
    public int firstResultPosition { get; set; }

    [JsonProperty("Result")]
    public List<Result> Results { get; set; }
    
}

[JsonObject(MemberSerialization.OptOut  )]
public class Result
{
    public String Title { get; set; }
    public String Summary { get; set; }
    public String Url { get; set; }
    public String ClickUrl { get; set; }
    public String RefererUrl { get; set; }
    public int FileSize { get; set; }
    public String FileFormat { get; set; }
    public int Height { get; set; }
    public int Width { get; set; }
    public Thumbnail Thumbnail { get; set; }

}
[JsonObject(MemberSerialization.OptOut)]
public class Thumbnail
{
    public String Url { get; set; }
    public int Height { get; set; }
    public int Width { get; set; }
}

Last edited Mar 15, 2009 at 7:16 PM by coryking, version 2

Comments

No comments yet.