Skip to content

Instantly share code, notes, and snippets.

@thunklife
Created July 20, 2012 20:38
Show Gist options
  • Select an option

  • Save thunklife/3153086 to your computer and use it in GitHub Desktop.

Select an option

Save thunklife/3153086 to your computer and use it in GitHub Desktop.
Generic FitBitClient Methods
class Program
{
static void Main(string[] args)
{
var client = new FitBitClient(); //pretending there are no dependencies for brevity.
var user = client.Get<UserProfile>(new GetUserProfileRequest("someUser"));
}
}
//Just a spike of a generic Get<T> method.
//I based this on the GetUserProfile method.
//It's rough, and I need to look through the rest of the methods to see what I might have missed.
public class FitBitClient
{
//removed everything else
public T Get<T>(IGetRequest getRequest) where T : new()
{
string apiCall = getRequest.FormatApiCall();
var request = new RestRequest(apiCall);
if (!string.IsNullOrWhiteSpace(getRequest.RootElementName))
request.RootElement = getRequest.RootElementName;
var response = restClient.Execute<T>(request);
HandleResponseCode(response.StatusCode);
return response.Data;
}
}
public class GetNullUserProfileRequest : IGetRequest
{
public string FormatApiCall()
{
return "/1/user/-/profile.xml";
}
public string RootElementName
{
get { return "user"; }
}
}
public class GetUserProfileRequest : IGetRequest
{
public string RootElementName
{
get { return "user"; }
}
readonly string _encodedUserId;
public GetUserProfileRequest(string encodedUserId)
{
_encodedUserId = encodedUserId;
}
public string FormatApiCall()
{
return string.Format("/1/user/{0}/profile.xml", _encodedUserId);
}
}
public interface IGetRequest
{
string FormatApiCall();
string RootElementName { get; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment