Created
July 20, 2012 20:38
-
-
Save thunklife/3153086 to your computer and use it in GitHub Desktop.
Generic FitBitClient Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GetNullUserProfileRequest : IGetRequest | |
| { | |
| public string FormatApiCall() | |
| { | |
| return "/1/user/-/profile.xml"; | |
| } | |
| public string RootElementName | |
| { | |
| get { return "user"; } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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