Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using System.Net;
- using App3.Models;
- namespace Plugin.RestClient
- {
- /// <summary>
- /// RestClient implements methods for calling CRUD operations
- /// using HTTP.
- /// </summary>
- public class RestClient<T>
- {
- private const string WebServiceUrl = "http://localhost:64000/api/Employees";
- public async Task<List<T>> GetAsync()
- {
- var httpClient = new HttpClient();
- var json = await httpClient.GetStringAsync(WebServiceUrl);
- var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
- return taskModels;
- }
- public async Task<bool> PostAsync(T t)
- {
- var httpClient = new HttpClient();
- var json = JsonConvert.SerializeObject(t);
- HttpContent httpContent = new StringContent(json);
- httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
- var result = await httpClient.PostAsync(WebServiceUrl, httpContent);
- return result.IsSuccessStatusCode;
- }
- public async Task<bool> PutAsync(int id, T t)
- {
- var httpClient = new HttpClient();
- var json = JsonConvert.SerializeObject(t);
- HttpContent httpContent = new StringContent(json);
- httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
- var result = await httpClient.PutAsync(WebServiceUrl + id, httpContent);
- return result.IsSuccessStatusCode;
- }
- public async Task<bool> DeleteAsync(int id, T t)
- {
- var httpClient = new HttpClient();
- var response = await httpClient.DeleteAsync(WebServiceUrl + id);
- return response.IsSuccessStatusCode;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement