-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHttpRequestHandler.cs
More file actions
158 lines (141 loc) · 5.58 KB
/
HttpRequestHandler.cs
File metadata and controls
158 lines (141 loc) · 5.58 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Contentstack.Core.Internals
{
internal class HttpRequestHandler
{
ContentstackClient client
{
get; set;
}
internal HttpRequestHandler(ContentstackClient contentstackClient)
{
client = contentstackClient;
}
public async Task<string> ProcessRequest(string Url, Dictionary<string, object> Headers, Dictionary<string, object> BodyJson, string FileName = null, string Branch = null, bool isLivePreview = false, int timeout = 30000, WebProxy proxy = null)
{
String queryParam = String.Join("&", BodyJson.Select(kvp => {
var value = "";
if (kvp.Value is string[])
{
string[] vals = (string[])kvp.Value;
value = String.Join("&", vals.Select(item =>
{
return String.Format("{0}={1}", kvp.Key, item);
}));
return value;
}
else if (kvp.Value is Dictionary<string, object>)
value = JsonConvert.SerializeObject(kvp.Value);
else
return String.Format("{0}={1}", kvp.Key, kvp.Value);
return String.Format("{0}={1}", kvp.Key, value);
}));
var uri = new Uri(Url+"?"+queryParam);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.ContentType = "application/json";
request.Headers["x-user-agent"]="contentstack-delivery-dotnet/2.16.0";
request.Timeout = timeout;
if (proxy != null)
{
request.Proxy = proxy;
}
if (Branch != null)
{
request.Headers["branch"] = Branch;
}
if (Headers != default(IDictionary<string, string>)) {
foreach (var header in Headers) {
try {
request.Headers[header.Key] = header.Value.ToString();
} catch {
}
}
}
foreach (var plugin in client.Plugins)
{
request = await plugin.OnRequest(client, request);
};
var serializedresult = JsonConvert.SerializeObject(BodyJson);
byte[] requestBody = Encoding.UTF8.GetBytes(serializedresult);
StreamReader reader = null;
HttpWebResponse response = null;
try {
response = (HttpWebResponse)await request.GetResponseAsync();
if (response != null) {
reader = new StreamReader(response.GetResponseStream());
string responseString = await reader.ReadToEndAsync();
foreach (var plugin in client.Plugins)
{
responseString = await plugin.OnResponse(client, request, response, responseString);
}
if (isLivePreview == false && this.client.LivePreviewConfig.Enable == true)
{
JObject data = JsonConvert.DeserializeObject<JObject>(responseString.Replace("\r\n", ""), this.client.SerializerSettings);
updateLivePreviewContent(data);
responseString = JsonConvert.SerializeObject(data);
}
return responseString;
} else {
return null;
}
} catch (Exception we) {
throw we;
} finally {
if (reader != null) {
reader.Dispose();
}
if (response != null)
{
response.Dispose();
}
}
}
internal void updateLivePreviewContent(JObject response)
{
if (response.ContainsKey("uid") && response["uid"].ToString() == this.client.LivePreviewConfig.EntryUID)
{
response.Merge(this.client.LivePreviewConfig.PreviewResponse, new JsonMergeSettings()
{
MergeArrayHandling = MergeArrayHandling.Replace
});
}
else
{
foreach (var content in response)
{
if (content.Value.Type == JTokenType.Array)
{
updateArray((JArray)response[content.Key]);
}
else if (content.Value.Type == JTokenType.Object)
{
updateLivePreviewContent((JObject)response[content.Key]);
}
}
}
}
internal void updateArray(JArray array)
{
for (var i = 0; i < array.Count(); i++)
{
if (array[i].Type == JTokenType.Array)
{
updateArray((JArray)array[i]);
}
else if (array[i].Type == JTokenType.Object)
{
updateLivePreviewContent((JObject)array[i]);
}
}
}
}
}