forked from XiaoFaye/WooCommerce.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestClient.cs
142 lines (113 loc) · 5.76 KB
/
RestClient.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace WooCommerceNET
{
/// <summary>
/// Rest API class to use HTTPClient instead of HttpWebRequest.
/// </summary>
public class RestClient : RestAPI
{
// HttpClient is intended to be instantiated once per application, rather than per-use.
private static readonly HttpClient HttpClient = new HttpClient();
public RestClient(string url, string key, string secret, bool authorizedHeader = true,
Func<string, string> jsonSerializeFilter = null,
Func<string, string> jsonDeserializeFilter = null)
: base(url, key, secret, authorizedHeader, jsonSerializeFilter, jsonDeserializeFilter)
{
//TODO - Need to work on requestFilter and responseFilter
}
public override async Task<string> SendHttpClientRequest<T>(string endpoint, RequestMethod method, T requestBody, Dictionary<string, string> parms = null)
{
HttpRequestMessage request = new HttpRequestMessage();
if (Version == APIVersion.WordPressAPI)
{
if (string.IsNullOrEmpty(oauth_token) || string.IsNullOrEmpty(oauth_token_secret))
throw new Exception($"oauth_token and oauth_token_secret parameters are required when using WordPress REST API.");
}
if ((Version == APIVersion.WordPressAPIJWT || WCAuthWithJWT) && JWT_Object == null)
{
request.RequestUri = new Uri(wc_url.Replace("wp/v2", "jwt-auth/v1/token")
.Replace("wc/v1", "jwt-auth/v1/token")
.Replace("wc/v2", "jwt-auth/v1/token")
.Replace("wc/v3", "jwt-auth/v1/token"));
request.Method = HttpMethod.Post;
//if (JWTRequestFilter != null)
// JWTRequestFilter.Invoke(request);
request.Content = new StringContent($"username={wc_key}&password={WebUtility.UrlEncode(wc_secret)}",
Encoding.UTF8,
"application/x-www-form-urlencoded");
var jwtResponseMessage = await HttpClient.SendAsync(request);
//Check for responseMessage.IsSuccessStatusCode
string result = await jwtResponseMessage.Content.ReadAsStringAsync();
if (JWTDeserializeFilter != null)
result = JWTDeserializeFilter.Invoke(result);
JWT_Object = DeserializeJSon<WP_JWT_Object>(result);
}
if (wc_url.StartsWith("https", StringComparison.OrdinalIgnoreCase) && Version != APIVersion.WordPressAPI && Version != APIVersion.WordPressAPIJWT)
{
if (AuthorizedHeader == true)
{
request.RequestUri = new Uri(wc_url + GetOAuthEndPoint(method.ToString(), endpoint, parms));
if (WCAuthWithJWT && JWT_Object != null)
request.Headers.Add("Authorization", "Bearer " + JWT_Object.token);
else
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(wc_key + ":" + wc_secret)));
}
else
{
if (parms == null)
parms = new Dictionary<string, string>();
if (!parms.ContainsKey("consumer_key"))
parms.Add("consumer_key", wc_key);
if (!parms.ContainsKey("consumer_secret"))
parms.Add("consumer_secret", wc_secret);
request.RequestUri = new Uri(wc_url + GetOAuthEndPoint(method.ToString(), endpoint, parms));
}
}
else
{
request.RequestUri = new Uri(wc_url + GetOAuthEndPoint(method.ToString(), endpoint, parms));
if (Version == APIVersion.WordPressAPIJWT)
request.Headers.Add("Authorization", "Bearer " + JWT_Object.token);
}
request.Method = new HttpMethod(method.ToString());
//if (webRequestFilter != null)
// webRequestFilter.Invoke(httpWebRequest);
if (requestBody != null && !string.IsNullOrWhiteSpace(requestBody.ToString()))
{
if (requestBody.GetType() == typeof(string))
{
if (requestBody.ToString() == "fileupload")
{
}
else
{
request.Content = new StringContent(requestBody.ToString(),
Encoding.UTF8,
"application/json");
}
}
else
{
request.Content = new StringContent(SerializeJSon(requestBody),
Encoding.UTF8,
"application/json");
}
}
var responseMessage = await HttpClient.SendAsync(request);
string responseResult = await responseMessage.Content.ReadAsStringAsync();
if (responseMessage.IsSuccessStatusCode)
{
return responseResult;
}
else
{
throw new Exception(responseResult);
}
}
}
}