-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHttpHandler.cs
More file actions
94 lines (81 loc) · 3.44 KB
/
HttpHandler.cs
File metadata and controls
94 lines (81 loc) · 3.44 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
using System;
using System.Net.Http;
using Contentstack.Management.Core.Http;
using Contentstack.Management.Core.Internal;
using Contentstack.Management.Core.Runtime.Contexts;
namespace Contentstack.Management.Core.Runtime.Pipeline
{
public class HttpHandler: IPipelineHandler
{
#region Private
private readonly HttpClient _httpClient;
#endregion
#region Constructor
internal HttpHandler(HttpClient httpClient)
{
_httpClient = httpClient;
}
#endregion
#region Public
public ILogManager LogManager { get; set; }
public IPipelineHandler InnerHandler { get; set; }
public async System.Threading.Tasks.Task<T> InvokeAsync<T>(IExecutionContext executionContext, bool addAcceptMediaHeader = false, string apiVersion = null)
{
IHttpRequest httpRequest = null;
try
{
var requestContext = executionContext.RequestContext;
httpRequest = requestContext.service.CreateHttpRequest(_httpClient, requestContext.config, addAcceptMediaHeader, apiVersion);
if (requestContext.service.HasRequestBody() && requestContext.service.Content != null)
{
httpRequest.WriteToRequestBody(requestContext.service.Content, requestContext.service.Headers);
}
executionContext.ResponseContext.httpResponse = await httpRequest.GetResponseAsync().ConfigureAwait(false);
executionContext.RequestContext.service.OnResponse(executionContext.ResponseContext.httpResponse, requestContext.config);
return await System.Threading.Tasks.Task.FromResult<T>((T)executionContext.ResponseContext.httpResponse);
}
catch (Exception e)
{
if (LogManager != null)
{
LogManager.Error(e, "Http request error", new object());
}
throw;
}
finally
{
if (httpRequest != null)
httpRequest.Dispose();
}
}
public void InvokeSync(IExecutionContext executionContext, bool addAcceptMediaHeader = false, string apiVersion = null)
{
IHttpRequest httpRequest = null;
try
{
var requestContext = executionContext.RequestContext;
httpRequest = requestContext.service.CreateHttpRequest(_httpClient, requestContext.config, addAcceptMediaHeader, apiVersion: apiVersion);
if (requestContext.service.HasRequestBody() && requestContext.service.Content != null)
{
httpRequest.WriteToRequestBody(requestContext.service.Content, requestContext.service.Headers);
}
executionContext.ResponseContext.httpResponse = httpRequest.GetResponse();
executionContext.RequestContext.service.OnResponse(executionContext.ResponseContext.httpResponse, requestContext.config);
}
catch (Exception e)
{
if (LogManager != null)
{
LogManager.Error(e, "Http request error", new object());
}
throw;
}
finally
{
if (httpRequest != null)
httpRequest.Dispose();
}
}
#endregion
}
}