-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstackResponse.cs
More file actions
202 lines (173 loc) · 6.13 KB
/
ContentstackResponse.cs
File metadata and controls
202 lines (173 loc) · 6.13 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Contentstack.Management.Core
{
/// <summary>
/// Abstract class for Response objects
/// </summary>
public class ContentstackResponse : IResponse, IDisposable
{
private bool _disposed = false;
string[] _headerNames;
Dictionary<string, string> _headers;
HashSet<string> _headerNamesSet;
private readonly HttpResponseMessage _response;
private readonly JsonSerializer _serializer;
#region Public
/// <summary>
/// Returns the content length of the HTTP response.
/// </summary>
public long ContentLength { get; private set; }
/// <summary>
/// Gets the property ContentType.
/// </summary>
public string ContentType { get; private set; }
/// <summary>
/// The HTTP status code from the HTTP response.
/// </summary>
public HttpStatusCode StatusCode { get; private set; }
/// <summary>
/// Gets a value that indicates whether the HTTP response was successful.
/// </summary>
public bool IsSuccessStatusCode { get; private set; }
/// <summary>
/// The entire response body from the HTTP response.
/// </summary>
public HttpResponseMessage ResponseBody {
get
{
return _response;
}
}
/// <summary>
/// Gets the header names from HTTP response headers.
/// </summary>
/// <returns>The string Array</returns>
public string[] GetHeaderNames()
{
return _headerNames;
}
/// <summary>
/// Gets the value for the header name from HTTP response headers.
/// </summary>
/// <param name="headerName">Header name for which value is needed</param>
/// <returns>The string</returns>
public string GetHeaderValue(string headerName)
{
string headerValue;
if (_headers.TryGetValue(headerName, out headerValue))
return headerValue;
return string.Empty;
}
/// <summary>
/// Return true if header name present in HTTP response headers.
/// </summary>
/// <param name="headerName"></param>
/// <returns>The bool</returns>
public bool IsHeaderPresent(string headerName)
{
return _headerNamesSet.Contains(headerName);
}
#endregion
#region Private
private string GetFirstHeaderValue(HttpHeaders headers, string key)
{
IEnumerable<string> headerValues = null;
if (headers.TryGetValues(key, out headerValues))
return headerValues.FirstOrDefault();
return string.Empty;
}
private void CopyHeaderValues(HttpResponseMessage response)
{
List<string> headerNames = new List<string>();
_headers = new Dictionary<string, string>(10, StringComparer.OrdinalIgnoreCase);
foreach (string key in response.Headers.Select((kvp) => kvp.Key))
{
headerNames.Add(key);
var headerValue = GetFirstHeaderValue(response.Headers, key);
_headers.Add(key, headerValue);
}
if (response.Content != null)
{
foreach (var key in response.Content.Headers.Select((kvp) => kvp.Key))
{
if (!headerNames.Contains(key))
{
headerNames.Add(key);
var headerValue = GetFirstHeaderValue(response.Content.Headers, key);
_headers.Add(key, headerValue);
}
}
}
_headerNames = headerNames.ToArray();
_headerNamesSet = new HashSet<string>(_headerNames, StringComparer.OrdinalIgnoreCase);
}
#endregion
internal ContentstackResponse(HttpResponseMessage response, JsonSerializer serializer)
{
_response = response;
_serializer = serializer;
this.StatusCode = response.StatusCode;
this.IsSuccessStatusCode = response.IsSuccessStatusCode;
this.ContentLength = response.Content.Headers.ContentLength ?? 0;
if (response.Content.Headers.ContentType != null)
{
this.ContentType = response.Content.Headers.ContentType.MediaType;
}
CopyHeaderValues(response);
}
/// <summary>
/// Json Object format response.
/// </summary>
/// <returns>The JObject.</returns>
public JObject OpenJObjectResponse()
{
ThrowIfDisposed();
return JObject.Parse(OpenResponse());
}
/// <summary>
/// String format response.
/// </summary>
/// <returns>The string.</returns>
public string OpenResponse()
{
ThrowIfDisposed();
return _response.Content.ReadAsStringAsync().Result;
}
/// <summary>
/// Type response to serialize the response.
/// </summary>
/// <typeparam name="TResponse">The type to serialize the response into.</typeparam>
/// <returns></returns>
public TResponse OpenTResponse<TResponse>()
{
ThrowIfDisposed();
JObject jObject = OpenJObjectResponse();
return jObject.ToObject<TResponse>(_serializer);
}
#region Dispose method
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
_disposed = true;
}
private void ThrowIfDisposed()
{
if (this._disposed)
throw new ObjectDisposedException(GetType().FullName);
}
#endregion
}
}