Neat Sample: F# and Bing API
Jomo Fisher – I recently posted an sample of calling the Freebase web service with F#. Here’s another F# web service sample. This one uses the Bing Phone API to do a query. This time the code uses Xml instead of JSON and XmlDocument instead of a DataContract deserializer. This is pretty much a straight transliteration of one of the Bing SDK samples.
Code Snippet
- // BingPhone.fsx
- // An example of using the a Bing web service API from F#
- // by Jomo Fisher
- open System.Net
- open System.Xml
- let appId = "your app id goes here";
- let BuildRequest() : HttpWebRequest =
- let requestString =
- "https://api.bing.net/xml.aspx?"
- // Common request fields (required)
- + "AppId=" + appId
- + "&Query=microsoft offices"
- + "&Sources=Phonebook"
- // Common request fields (optional)
- + "&Version=2.0"
- + "&Market=en-us"
- + "&UILanguage=en"
- + "&Latitude=47.603450"
- + "&Longitude=-122.329696"
- + "&Radius=10.0"
- // Phonebook-specific request fields (optional)
- + "&Phonebook.Count=10"
- + "&Phonebook.Offset=0"
- + "&Phonebook.FileType=YP"
- + "&Phonebook.SortBy=Distance";
- // Create and initialize the request.
- downcast HttpWebRequest.Create(requestString)
- let DisplayErrors(errors:XmlNodeList) =
- // Iterate over the list of errors and display error details.
- printfn "Errors:"
- for error in errors do
- for detail in error.ChildNodes do
- printfn "%s: %s" detail.Name detail.InnerText
- let DisplayResults( root:XmlNode, nsmgr:XmlNamespaceManager) =
- // Add the Phonebook SourceType namespace to the namespace manager.
- nsmgr.AddNamespace("pho", "https://schemas.microsoft.com/LiveSearch/2008/04/XML/phonebook")
- let phonebook = root.SelectSingleNode("./pho:Phonebook", nsmgr)
- let results = phonebook.SelectNodes("./pho:Results/pho:PhonebookResult",nsmgr)
- let version = root.SelectSingleNode("./@Version", nsmgr).InnerText
- let searchTerms = root.SelectSingleNode("./api:Query/api:SearchTerms",nsmgr).InnerText
- let _,offset = System.Int32.TryParse(phonebook.SelectSingleNode("./pho:Offset", nsmgr).InnerText)
- let _,total = System.Int32.TryParse(phonebook.SelectSingleNode("./pho:Total", nsmgr).InnerText)
- // Display the results header.
- printfn "Bing API Version %s" version
- printfn "Phonebook results for %s" searchTerms
- printfn "Displaying %d to %d of %d results" (offset + 1) (offset + results.Count) total
- // Display the Phonebook results.
- let builder = System.Text.StringBuilder();
- for result in results do
- builder.Length <- 0
- builder
- .AppendLine(result.SelectSingleNode("./pho:Business", nsmgr).InnerText)
- .AppendLine(result.SelectSingleNode("./pho:Address", nsmgr).InnerText)
- .Append(result.SelectSingleNode("./pho:City", nsmgr).InnerText)
- .Append(", ")|>ignore
- let stateOrProvince = result.SelectSingleNode("./pho:StateOrProvince", nsmgr).InnerText
- builder
- .AppendLine(stateOrProvince)
- .AppendLine(result.SelectSingleNode("./pho:PhoneNumber", nsmgr).InnerText)
- .Append("Average Rating: ")
- .AppendLine(result.SelectSingleNode("./pho:UserRating", nsmgr).InnerText)|>ignore
- printfn "%s" (builder.ToString())
- let DisplayResponse(response:HttpWebResponse) =
- // Load the response into an XmlDocument.
- let document = XmlDocument()
- document.Load(response.GetResponseStream());
- // Add the default namespace to the namespace manager.
- let nsmgr = XmlNamespaceManager(document.NameTable);
- nsmgr.AddNamespace("api","https://schemas.microsoft.com/LiveSearch/2008/04/XML/element");
- let errors = document.DocumentElement.SelectNodes("./api:Errors/api:Error", nsmgr)
- if errors.Count > 0 then
- // There are errors in the response. Display error details.
- DisplayErrors(errors);
- else
- // There were no errors in the response. Display the
- // Phonebook results.
- DisplayResults(document.DocumentElement, nsmgr);
- let request = BuildRequest()
- try
- // Send the request; display the response.
- let response = request.GetResponse()
- DisplayResponse(downcast response)
- with ex ->
- // An exception occurred while accessing the network.
- printfn "%s" ex.Message
As before, let me know if there are other F# samples you’d like to see.
This posting is provided "AS IS" with no warranties, and confers no rights.