-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtensions.cs
71 lines (61 loc) · 2.62 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Newtonsoft.Json;
using openapi_sample_client.Dtos;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace openapi_sample_client
{
public static class Extensions
{
public static async Task<T> GetFromJsonAsync<T>(this HttpClient client,string url)
{
var response = await client.GetAsync(url);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(content);
}
public static async Task<T> PostFromJsonAsync<T>(this HttpClient client, string url,object data)
{
var json = JsonConvert.SerializeObject(data);
var body= new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url,body);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(content);
}
public static async Task<T> PutFromJsonAsync<T>(this HttpClient client, string url, object data)
{
var json = JsonConvert.SerializeObject(data);
var body = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PutAsync(url, body);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(content);
}
public static async Task<T> DeleteFromJsonAsync<T>(this HttpClient client, string url)
{
var response = await client.DeleteAsync(url);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(content);
}
public static async Task<string> DeleteFromJsonAsync(this HttpClient client, string url)
{
var response = await client.DeleteAsync(url);
var content = await response.Content.ReadAsStringAsync();
return content;
}
public static async Task<string> PostFromJsonAsync(this HttpClient client, string url, object data)
{
var json = JsonConvert.SerializeObject(data);
var body = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, body);
var content = await response.Content.ReadAsStringAsync();
return content;
}
public static void Print(this string s)
{
Console.WriteLine(s);
}
}
}