Skip to content

Instantly share code, notes, and snippets.

@canton7
Created October 14, 2017 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canton7/11c782970d9a1a8bb1792be3ed1563f5 to your computer and use it in GitHub Desktop.
Save canton7/11c782970d9a1a8bb1792be3ed1563f5 to your computer and use it in GitHub Desktop.
namespace RestEaseSampleApplication
{
// We receive a JSON response, so define a class to deserialize the json into
public class User
{
public string Name { get; set; }
public string Blog { get; set; }
// This is deserialized using Json.NET, so use attributes as necessary
[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }
}
// Define an interface representing the API
// GitHub requires a User-Agent header, so specify one
[Header("User-Agent", "RestEase")]
public interface IGitHubApi
{
// The [Get] attribute marks this method as a GET request
// The "users" is a relative path the a base URL, which we'll provide later
// "{userId}" is a placeholder in the URL: the value from the "userId" method parameter is used
[Get("users/{userId}")]
Task<User> GetUserAsync([Path] string userId);
}
public class Program
{
public static void Main(string[] args)
{
// Create an implementation of that interface
// We'll pass in the base URL for the API
IGitHubApi api = RestClient.For<IGitHubApi>("https://api.github.com", async (request, token) =>
{
});
// Now we can simply call methods on it
// Normally you'd await the request, but this is a console app
User user = api.GetUserAsync("canton7").Result;
Console.WriteLine($"Name: {user.Name}. Blog: {user.Blog}. CreatedAt: {user.CreatedAt}");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment