Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Net.Http.Headers;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using System.Net;
  8. using App3.Models;
  9.  
  10. namespace Plugin.RestClient
  11. {
  12. /// <summary>
  13. /// RestClient implements methods for calling CRUD operations
  14. /// using HTTP.
  15. /// </summary>
  16. public class RestClient<T>
  17. {
  18. private const string WebServiceUrl = "http://localhost:64000/api/Employees";
  19.  
  20. public async Task<List<T>> GetAsync()
  21. {
  22. var httpClient = new HttpClient();
  23.  
  24.  
  25.  
  26. var json = await httpClient.GetStringAsync(WebServiceUrl);
  27.  
  28.  
  29. var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
  30.  
  31. return taskModels;
  32. }
  33.  
  34. public async Task<bool> PostAsync(T t)
  35. {
  36. var httpClient = new HttpClient();
  37.  
  38. var json = JsonConvert.SerializeObject(t);
  39.  
  40. HttpContent httpContent = new StringContent(json);
  41.  
  42. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  43.  
  44. var result = await httpClient.PostAsync(WebServiceUrl, httpContent);
  45.  
  46. return result.IsSuccessStatusCode;
  47. }
  48.  
  49. public async Task<bool> PutAsync(int id, T t)
  50. {
  51. var httpClient = new HttpClient();
  52.  
  53. var json = JsonConvert.SerializeObject(t);
  54.  
  55. HttpContent httpContent = new StringContent(json);
  56.  
  57. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  58.  
  59. var result = await httpClient.PutAsync(WebServiceUrl + id, httpContent);
  60.  
  61. return result.IsSuccessStatusCode;
  62. }
  63.  
  64. public async Task<bool> DeleteAsync(int id, T t)
  65. {
  66. var httpClient = new HttpClient();
  67.  
  68. var response = await httpClient.DeleteAsync(WebServiceUrl + id);
  69.  
  70. return response.IsSuccessStatusCode;
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement