Skip to content

Instantly share code, notes, and snippets.

@irobinson
Created December 22, 2011 16:08
Show Gist options
  • Save irobinson/1510825 to your computer and use it in GitHub Desktop.
Save irobinson/1510825 to your computer and use it in GitHub Desktop.
Quick example of using RestSharp to query the Untappd Brewery API
// UntappdApi.cs
// *************
using RestSharp;
public class UntappdApi
{
private const string BaseUrl = "http://api.untappd.com/v3/";
private readonly string apiKey;
public UntappdApi(string apiKey)
{
this.apiKey = apiKey;
}
public T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient
{
BaseUrl = BaseUrl
};
request.AddParameter("key", apiKey); // used on every request
var response = client.Execute<T>(request);
return response.Data;
}
}
// Brewery.cs
// **********
using System;
public class Brewery
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Established { get; set; }
public Address Address { get; set; }
public string Phone { get; set; }
public string Website { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
}
public class Address
{
public string StreetAddress { get; set; }
public string ExtendedAddress { get; set; }
public string Locality { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string CountryName { get; set; }
}
// Consuming Logic
// *************
var request = new RestRequest
{
Resource = "brewery_search"
};
string pageNumber = "1";
if (Request.QueryString["Page"] != null)
pageNumber = Request.QueryString["Page"];
request.AddParameter("page", pageNumber);
request.AddParameter("q", "2nd shift");
var breweries = new UntappdApi("your key").Execute<List<Brewery>>(request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment