Skip to content

Instantly share code, notes, and snippets.

@luismts
Created December 9, 2018 06:38
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 luismts/846ee39192510b6e318888069aacfe56 to your computer and use it in GitHub Desktop.
Save luismts/846ee39192510b6e318888069aacfe56 to your computer and use it in GitHub Desktop.
public class RestService : IRestService
{
HttpClient client;
...
public RestService ()
{
client = new HttpClient (); // Creating the HTTPClient Object
}
public async Task<List<TodoItem>> RefreshDataAsync ()
{
...
var uri = new Uri (string.Format (Constants.RestUrl, string.Empty));
...
var response = await client.GetAsync (uri); // Retrieving Data
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
}
...
}
public async Task SaveTodoItemAsync (TodoItem item, bool isNewItem = false)
{
var uri = new Uri (string.Format (Constants.RestUrl, string.Empty));
...
var json = JsonConvert.SerializeObject (item);
var content = new StringContent (json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
if (isNewItem) {
response = await client.PostAsync (uri, content); // Creating Data
}
...
if (response.IsSuccessStatusCode) {
Debug.WriteLine (@"TodoItem successfully saved.");
}
...
}
public async Task SaveTodoItemAsync (TodoItem item, bool isNewItem = false)
{
...
response = await client.PutAsync (uri, content); // Updating Data
...
}
public async Task DeleteTodoItemAsync (string id)
{
var uri = new Uri (string.Format (Constants.RestUrl, id));
...
var response = await client.DeleteAsync (uri); // Deleting Data
if (response.IsSuccessStatusCode) {
Debug.WriteLine (@"TodoItem successfully deleted.");
}
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment