Skip to content

Instantly share code, notes, and snippets.

@DavidBurela
Last active December 14, 2015 09:59
Show Gist options
  • Save DavidBurela/5069136 to your computer and use it in GitHub Desktop.
Save DavidBurela/5069136 to your computer and use it in GitHub Desktop.
A Windows RT example of how to call the Sensis SAPI service Is a direct port of http://developers.sensis.com.au/docs/examples/C_Example
namespace SsapiExample
{
public class SearchSample
{
public SearchSample()
{
DoSampleSearch();
}
public async void DoSampleSearch()
{
var searchEndPoint = "http://api.sensis.com.au/ob-20110511/test/search";
var apiKey = "xxx";
var searcher = new SsapiSearcher(searchEndPoint, apiKey);
// Perform a search and check the response
var searchResponse = await searcher.SearchFor("cafe", "melbourne");
if (searchResponse.code < 200 || searchResponse.code > 299)
{
// You need some sort of error message here
//Console.WriteLine("Search failed - Error " + searchResponse.code + ": " + searchResponse.message);
return;
}
var totalResults string = "Total results found: " + searchResponse.totalResults;
// Iterate through the results
foreach (var result in searchResponse.results)
{
var name = result.name;
var addressLine = result.primaryAddress.addressLine;
var suburb = result.primaryAddress.suburb + " " + result.primaryAddress.state + " " + result.primaryAddress.postcode;
var geo = result.primaryAddress.latitude + ", " + result.primaryAddress.longitude;
}
}
}
}
using System;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
namespace SsapiExample.Helpers
{
class SsapiSearcher
{
readonly Uri _endPoint;
readonly string _apiKey;
public SsapiSearcher(string endPoint, string apiKey)
{
_endPoint = new Uri(endPoint);
_apiKey = apiKey;
}
public async Task<SearchResponse> SearchFor(string query, string location)
{
// Build the API request
var url = new Uri(_endPoint, "?query=" + Uri.EscapeDataString(query) + "&location=" + Uri.EscapeDataString(location) + "&key=" + Uri.EscapeDataString(_apiKey));
var req = WebRequest.Create(url);
// Send the request and read the response
using (var res = await req.GetResponseAsync())
{
var serializer = new DataContractJsonSerializer(typeof(SearchResponse));
var resultStream = res.GetResponseStream();
var result = serializer.ReadObject(resultStream);
return result as SearchResponse;
}
}
}
// These fields NEED to be lower case, due to JSON stuff
public class SearchResponse
{
public int code;
public string message;
public int totalResults;
public Listing[] results;
}
public class Listing
{
public string name;
public Address primaryAddress;
}
public class Address
{
public string addressLine;
public string suburb;
public string state;
public string postcode;
public double latitude;
public double longitude;
}
}
@DavidBurela
Copy link
Author

This is just a direct port of the sample .Net code on the Sensis website http://developers.sensis.com.au/docs/examples/C_Example
It has been ported to run on WindowsRT

It seems to only return the first 14 results. You may need to mess about with the query string to tell it to bring back more "rows"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment