-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstackService.cs
More file actions
252 lines (213 loc) · 7.26 KB
/
ContentstackService.cs
File metadata and controls
252 lines (213 loc) · 7.26 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.IO;
using System.Text;
using System.Net.Http;
using System.Collections.Generic;
using Contentstack.Management.Core.Http;
using Newtonsoft.Json;
using Contentstack.Management.Core.Utils;
using Contentstack.Management.Core.Queryable;
using System.Net.Http.Headers;
namespace Contentstack.Management.Core.Services
{
internal class ContentstackService : IContentstackService
{
#region
internal readonly ParameterCollection collection;
readonly IDictionary<string, string> headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
readonly IDictionary<string, string> pathResources = new Dictionary<string, string>(StringComparer.Ordinal);
readonly IDictionary<string, string> queryResources = new Dictionary<string, string>(StringComparer.Ordinal);
private bool _useQueryString = false;
private bool _disposed = false;
private JsonSerializer _serializer { get; set; }
#endregion
#region Constructor
internal ContentstackService(JsonSerializer serializer, Core.Models.Stack stack = null, ParameterCollection collection = null)
{
if (serializer == null)
{
throw new ArgumentNullException("serializer", CSConstants.JSONSerializerError);
}
if (stack != null)
{
if (!string.IsNullOrEmpty(stack.APIKey))
{
this.Headers.Add("api_key", stack.APIKey);
}
if (!string.IsNullOrEmpty(stack.BranchUid))
{
Headers.Add("branch", stack.BranchUid);
}
this.ManagementToken = stack.ManagementToken;
}else
{
this.ManagementToken = null;
}
this.collection = collection ?? new ParameterCollection();
_serializer = serializer;
}
#endregion
public JsonSerializer Serializer
{
get
{
return _serializer;
}
}
public bool UseQueryString
{
get
{
if (HttpMethod == "GET")
return true;
return _useQueryString;
}
set
{
_useQueryString = value;
}
}
public ParameterCollection Parameters
{
get
{
return collection;
}
}
public IDictionary<string, string> Headers
{
get
{
return headers;
}
}
public IDictionary<string, string> QueryResources
{
get
{
return queryResources;
}
}
public IDictionary<string, string> PathResources
{
get
{
return pathResources;
}
}
public string ResourcePath { get; set; }
public byte[] ByteContent { get; set; }
public HttpContent Content { get; set; }
public string HttpMethod { get; set; } = "GET";
public string ManagementToken { get; set; }
public void AddQueryResource(string queryResource, string value)
{
ThrowIfDisposed();
QueryResources.Add(queryResource, value);
}
public void AddPathResource(string key, string value)
{
ThrowIfDisposed();
PathResources.Add(key, value);
}
public string GetHeaderValue(string headerName)
{
ThrowIfDisposed();
string headerValue;
if (headers.TryGetValue(headerName, out headerValue))
return headerValue;
return string.Empty;
}
/// <summary>
/// Returns true if the request has a body, else false.
/// </summary>
/// <returns>Returns true if the request has a body, else false.</returns>
public bool HasRequestBody()
{
return HttpMethod == "POST" || HttpMethod == "PUT" || HttpMethod == "PATCH" || HttpMethod == "DELETE";
}
/// <summary>
/// Returns true if the request should include Content-Type: application/json header.
/// Override to skip Content-Type for specific requests (e.g. DELETE /releases).
/// </summary>
protected virtual bool ShouldSetContentType()
{
return true;
}
public virtual IHttpRequest CreateHttpRequest(HttpClient httpClient, ContentstackClientOptions config, bool addAcceptMediaHeader = false, string apiVersion = null)
{
ThrowIfDisposed();
if (addAcceptMediaHeader)
{
httpClient.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("image/jpeg"));
}
Uri requestUri = ContentstackUtilities.ComposeUrI(config.GetUri(), this);
if (ShouldSetContentType())
{
Headers["Content-Type"] = "application/json";
}
if (!string.IsNullOrEmpty(this.ManagementToken))
{
Headers["authorization"] = this.ManagementToken;
}
else if (config.IsOAuthToken)
{
if (!string.IsNullOrEmpty(config.Authtoken))
{
Headers["authorization"] = $"Bearer {config.Authtoken}";
}
}
else if (!string.IsNullOrEmpty(config.Authtoken))
{
Headers["authtoken"] = config.Authtoken;
}
if (!string.IsNullOrEmpty(apiVersion))
{
Headers["api_version"] = apiVersion;
}
var contentstackHttpRequest = new ContentstackHttpRequest(httpClient, _serializer);
contentstackHttpRequest.Method = new HttpMethod(HttpMethod);
contentstackHttpRequest.RequestUri = requestUri;
ContentBody();
WriteContentByte();
contentstackHttpRequest.SetRequestHeaders(Headers);
return contentstackHttpRequest;
}
public virtual void ContentBody(){}
internal void WriteContentByte()
{
if (ByteContent != null && ByteContent.Length > 0)
{
Content = new ByteArrayContent(ByteContent);
Content.Headers.ContentLength = ByteContent.Length;
}
}
public virtual void OnResponse(IResponse httpResponse, ContentstackClientOptions config)
{
// Base implementation - no general cleanup
}
#region Dispose methods
/// <summary>
/// Wrapper for HttpClient Dispose.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
_disposed = true;
}
private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(GetType().FullName);
}
#endregion
}
}