Skip to content

Instantly share code, notes, and snippets.

@serialseb
Created November 17, 2011 14:52
Show Gist options
  • Save serialseb/1373311 to your computer and use it in GitHub Desktop.
Save serialseb/1373311 to your computer and use it in GitHub Desktop.
rest-aurant client
class Program
{
private const string REL_RESTAURANT_LIST = "http://rest.aurant.org/restaurant-list";
static void Main(string[] args)
{
var restaurantHref = DiscoverRestaurantListUri();
Console.WriteLine("Using " + restaurantHref);
var selectedRestaurant = SelectRestaurantUri(restaurantHref);
Console.WriteLine("Using " + selectedRestaurant);
}
private static Uri SelectRestaurantUri(Uri restaurantHref)
{
var document = XDocument.Load(restaurantHref.ToString())
.ItemScope("http://schema.org/Restaurant", "name", "address", "url")
.ToList();
foreach (var restaurant in document)
{
Console.WriteLine("{0}. {1} - {2}",
document.IndexOf(restaurant) + 1, restaurant["name"], restaurant["url"]);
}
var selected = int.Parse(Console.ReadLine());
return new Uri(document.ElementAt(selected - 1)["url"], UriKind.Absolute);
}
private static Uri DiscoverRestaurantListUri()
{
const string defaultHref = "http://localhost:40372";
Console.Write("Enter server (default {0}):", defaultHref);
var inputUri = Console.ReadLine() ?? defaultHref;
var req = WebRequest.Create(inputUri);
var response = req.GetResponse();
var linkHeader = response.GetLinkValue(REL_RESTAURANT_LIST);
var baseUri = new Uri(inputUri, UriKind.Absolute);
var linkUri = new Uri(linkHeader, UriKind.RelativeOrAbsolute);
return new Uri(baseUri, linkUri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment