Created
December 9, 2018 06:38
-
-
Save luismts/846ee39192510b6e318888069aacfe56 to your computer and use it in GitHub Desktop.
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 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