Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.