-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstackHttpRequest.cs
More file actions
198 lines (174 loc) · 5.78 KB
/
Copy pathContentstackHttpRequest.cs
File metadata and controls
198 lines (174 loc) · 5.78 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
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using System.Collections.Generic;
using Contentstack.Management.Core.Utils;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Contentstack.Management.Core.Exceptions;
namespace Contentstack.Management.Core.Http
{
internal class ContentstackHttpRequest: IHttpRequest
{
#region Private
private bool _disposed = false;
private readonly HttpClient _httpClient;
private readonly HttpRequestMessage _request;
private readonly JsonSerializer _serializer;
#endregion
#region Public
/// <summary>
/// The HTTP method or verb.
/// </summary>
public HttpMethod Method
{
get { return _request.Method; }
set { _request.Method = value; }
}
/// <summary>
/// The request URI.
/// </summary>
public Uri RequestUri { get; set; }
/// <summary>
/// The underlying HttpClient
/// </summary>
public HttpClient HttpClient
{
get { return _httpClient; }
}
/// <summary>
/// The underlying HTTP web request.
/// </summary>
public HttpRequestMessage Request
{
get { return _request; }
}
#endregion
#region Constructor
internal ContentstackHttpRequest(HttpClient httpClient, JsonSerializer serializer)
{
_httpClient = httpClient;
_serializer = serializer;
_request = new HttpRequestMessage();
}
#endregion
#region Dispose methods
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
_disposed = true;
if (_request != null)
{
_request.Dispose();
}
}
}
private void ThrowIfDisposed()
{
if (this._disposed)
throw new ObjectDisposedException(GetType().FullName);
}
#endregion
/// <summary>
/// Returns the HTTP response.
/// </summary>
/// <returns></returns>
public IResponse GetResponse()
{
ThrowIfDisposed();
try
{
return this.GetResponseAsync().Result;
}
catch (AggregateException e)
{
throw e.InnerException;
}
}
/// <summary>
/// Returns the HTTP response.
/// </summary>
/// <returns>The <see cref="Task"/>.</returns>
public async Task<IResponse> GetResponseAsync()
{
ThrowIfDisposed();
try
{
_request.RequestUri = this.RequestUri;
var responseMessage = await _httpClient.SendAsync(_request, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(continueOnCapturedContext: false);
if (responseMessage.StatusCode >= HttpStatusCode.Ambiguous &&
responseMessage.StatusCode < HttpStatusCode.BadRequest)
return new ContentstackResponse(responseMessage, _serializer);
if (!responseMessage.IsSuccessStatusCode)
{
throw ContentstackErrorException.CreateException(responseMessage);
}
return new ContentstackResponse(responseMessage, _serializer);
}
catch (HttpRequestException httpException)
{
if (httpException.InnerException is IOException)
{
throw httpException.InnerException;
}
throw;
}
}
/// <summary>
/// Sets the headers on the request.
/// </summary>
/// <param name="headers">A dictionary of header names and values.</param>
public void SetRequestHeaders(IDictionary<string, string> headers)
{
ThrowIfDisposed();
foreach (var kvp in headers)
{
_request.Headers.TryAddWithoutValidation(kvp.Key, kvp.Value);
}
}
/// <summary>
/// Writes a stream to the request body.
/// </summary>
/// <param name="content">The destination where the content stream is written.</param>
public void WriteToRequestBody(HttpContent content)
{
_request.Content = content;
}
/// <summary>
/// Gets a handle to the request content.
/// </summary>
/// <returns>The <see cref="HttpContent"/>.</returns>
public HttpContent GetRequestContent()
{
ThrowIfDisposed();
return System.Threading.Tasks.Task.FromResult(_request.Content).Result;
}
/// <summary>
/// Writes a stream to the request body.
/// </summary>
/// <param name="content">The destination where the content stream is written.</param>
/// <param name="contentHeaders">A dictionary of header names and values.</param>
public void WriteToRequestBody(HttpContent content, IDictionary<string, string> contentHeaders)
{
ThrowIfDisposed();
WriteToRequestBody(content);
WriteContentHeaders(contentHeaders);
}
private void WriteContentHeaders(IDictionary<string, string> contentHeaders)
{
_request.Content.Headers.ContentType =
MediaTypeHeaderValue.Parse(contentHeaders[HeadersKey.ContentTypeHeader]);
}
}
}