forked from pact-foundation/pact-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdminHttpClient.cs
100 lines (85 loc) · 3.41 KB
/
AdminHttpClient.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using PactNet.Configuration.Json;
using PactNet.Mocks.MockHttpService.Mappers;
using PactNet.Mocks.MockHttpService.Models;
using static System.String;
namespace PactNet.Mocks.MockHttpService
{
internal class AdminHttpClient
{
private readonly HttpClient _httpClient;
private readonly IHttpMethodMapper _httpMethodMapper;
private readonly JsonSerializerSettings _jsonSerializerSettings;
internal AdminHttpClient(
Uri baseUri,
HttpMessageHandler handler,
JsonSerializerSettings jsonSerializerSettings)
{
_httpClient = new HttpClient(handler) { BaseAddress = baseUri };
_httpMethodMapper = new HttpMethodMapper();
_jsonSerializerSettings = jsonSerializerSettings ?? JsonConfig.ApiSerializerSettings;
}
public AdminHttpClient(Uri baseUri) :
this(baseUri, new HttpClientHandler(), null)
{
}
public AdminHttpClient(Uri baseUri, JsonSerializerSettings jsonSerializerSettings) :
this(baseUri, new HttpClientHandler(), jsonSerializerSettings)
{
}
public async Task SendAdminHttpRequest(HttpVerb method, string path, IDictionary<string, string> headers = null)
{
await SendAdminHttpRequest<object>(method, path, null, headers:headers);
}
public async Task SendAdminHttpRequest<T>(HttpVerb method, string path, T requestContent, IDictionary<string, string> headers = null) where T : class
{
var responseContent = Empty;
var request = new HttpRequestMessage(_httpMethodMapper.Convert(method), path);
request.Headers.Add(Constants.AdministrativeRequestHeaderKey, "true");
if (headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
if (requestContent != null)
{
var requestContentJson = JsonConvert.SerializeObject(requestContent, _jsonSerializerSettings);
request.Content = new StringContent(requestContentJson, Encoding.UTF8, "application/json");
}
var response = await _httpClient.SendAsync(request, CancellationToken.None);
var responseStatusCode = response.StatusCode;
if (response.Content != null)
{
responseContent = await response.Content.ReadAsStringAsync();
}
Dispose(request);
Dispose(response);
if (path == "/pact" && headers != null && headers.ContainsKey("fileNameAndPath"))
{
File.WriteAllText(headers["fileNameAndPath"], responseContent);
}
if (responseStatusCode != HttpStatusCode.OK)
{
throw new PactFailureException(responseContent);
}
}
private void Dispose(IDisposable disposable)
{
if (disposable != null)
{
disposable.Dispose();
}
}
}
}