forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElasticsearchResponse.cs
222 lines (186 loc) · 6.68 KB
/
ElasticsearchResponse.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
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
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Elasticsearch.Net;
using Elasticsearch.Net.Connection;
using Elasticsearch.Net.Serialization;
namespace Elasticsearch.Net
{
//TODO document and possibly rename some of the properties
public interface IElasticsearchResponse
{
bool Success { get; }
bool SuccessOrKnownError { get; }
IConnectionConfigurationValues Settings { get; }
Exception OriginalException { get; }
string RequestMethod { get; }
string RequestUrl { get; }
[DebuggerDisplay("{Request != null ? System.Text.Encoding.UTF8.GetString(Request) : null,nq}")]
byte[] Request { get; }
int? HttpStatusCode { get; }
int NumberOfRetries { get; }
/// <summary>
/// The raw byte response, only set when IncludeRawResponse() is set on Connection configuration
/// </summary>
[DebuggerDisplay("{ResponseRaw != null ? System.Text.Encoding.UTF8.GetString(ResponseRaw) : null,nq}")]
byte[] ResponseRaw { get; }
}
public static class ElasticsearchResponse
{
internal static Task<ElasticsearchResponse<DynamicDictionary>> WrapAsync(Task<ElasticsearchResponse<Dictionary<string, object>>> responseTask)
{
return responseTask
.ContinueWith(t =>
{
if (t.IsFaulted)
throw t.Exception.Flatten().InnerException;
return ToDynamicResponse(t.Result);
});
}
internal static ElasticsearchResponse<DynamicDictionary> Wrap(ElasticsearchResponse<Dictionary<string, object>> response)
{
return ToDynamicResponse(response);
}
public static ElasticsearchResponse<TTo> CloneFrom<TTo>(IElasticsearchResponse from, TTo to)
{
return new ElasticsearchResponse<TTo>(from.Settings)
{
OriginalException = from.OriginalException,
HttpStatusCode = from.HttpStatusCode,
Request = from.Request,
RequestMethod = from.RequestMethod,
RequestUrl = from.RequestUrl,
Response = to,
ResponseRaw = from.ResponseRaw,
Serializer = from.Settings.Serializer,
Settings = from.Settings,
Success = from.Success
};
}
private static ElasticsearchResponse<DynamicDictionary> ToDynamicResponse(ElasticsearchResponse<Dictionary<string, object>> response)
{
return CloneFrom(response, response.Response != null ? DynamicDictionary.Create(response.Response) : null);
}
}
public class ElasticsearchResponse<T> : IElasticsearchResponse
{
protected static readonly string _printFormat;
protected static readonly string _errorFormat;
public bool Success { get; protected internal set; }
public Exception OriginalException { get; protected internal set; }
public string RequestMethod { get; protected internal set; }
public string RequestUrl { get; protected internal set; }
public IConnectionConfigurationValues Settings { get; protected internal set; }
public T Response { get; protected internal set; }
public byte[] Request { get; protected internal set; }
public int NumberOfRetries { get; protected internal set; }
/// <summary>
/// The raw byte response, only set when IncludeRawResponse() is set on Connection configuration
/// </summary>
public byte[] ResponseRaw { get; protected internal set; }
public int? HttpStatusCode { get; protected internal set; }
public IElasticsearchSerializer Serializer { get; protected internal set; }
/// <summary>
/// If the response is succesful or has a known error (400-500 range)
/// The client should not retry this call
/// </summary>
public bool SuccessOrKnownError
{
get
{
return this.Success ||
(this.HttpStatusCode.HasValue
&& this.HttpStatusCode.Value != 503 //service unavailable needs to be retried
&& this.HttpStatusCode.Value != 502 //bad gateway needs to be retried
&& ((this.HttpStatusCode.Value >= 400 && this.HttpStatusCode.Value < 599)));
}
}
protected internal ElasticsearchResponse(IConnectionConfigurationValues settings)
{
this.Settings = settings;
this.Serializer = settings.Serializer;
}
private ElasticsearchResponse(IConnectionConfigurationValues settings, Exception e)
: this(settings)
{
this.Success = false;
this.OriginalException = e;
}
private ElasticsearchResponse(IConnectionConfigurationValues settings, int statusCode)
: this(settings)
{
this.Success = statusCode >= 200 && statusCode < 300;
this.HttpStatusCode = statusCode;
}
public static ElasticsearchResponse<T> CreateError(IConnectionConfigurationValues settings, Exception e, string method, string path, byte[] request)
{
var cs = new ElasticsearchResponse<T>(settings, e);
cs.Request = request;
cs.RequestUrl = path;
cs.RequestMethod = method;
return cs;
}
public static ElasticsearchResponse<T> Create(IConnectionConfigurationValues settings, int statusCode, string method, string path, byte[] request)
{
var cs = new ElasticsearchResponse<T>(settings, statusCode);
cs.Request = request;
cs.RequestUrl = path;
cs.RequestMethod = method;
return cs;
}
public static ElasticsearchResponse<T> Create(IConnectionConfigurationValues settings, int statusCode, string method, string path, byte[] request, T response)
{
var cs = new ElasticsearchResponse<T>(settings, statusCode);
cs.Request = request;
cs.RequestUrl = path;
cs.RequestMethod = method;
cs.Response = response;
return cs;
}
static ElasticsearchResponse()
{
_printFormat = "StatusCode: {1}, {0}\tMethod: {2}, {0}\tUrl: {3}, {0}\tRequest: {4}, {0}\tResponse: {5}";
_errorFormat = "{0}\tExceptionMessage: {1}{0}\t StackTrace: {2}";
}
public override string ToString()
{
var r = this;
var e = r.OriginalException;
string response = "<Response stream not captured or already read to completion by serializer>";
if (typeof(T) == typeof(string))
response = this.Response as string;
else if (this.Settings.KeepRawResponse)
response = this.ResponseRaw.Utf8String();
else if (typeof(T) == typeof(byte[]))
response = (this.Response as byte[]).Utf8String();
string requestJson = null;
if (r.Request != null)
{
requestJson = r.Request.Utf8String();
}
var print = _printFormat.F(
Environment.NewLine,
r.HttpStatusCode.HasValue ? r.HttpStatusCode.Value.ToString(CultureInfo.InvariantCulture) : "-1",
r.RequestMethod,
r.RequestUrl,
requestJson,
response
);
if (!this.Success && e != null)
{
print += _errorFormat.F(Environment.NewLine, e.Message, e.StackTrace);
}
return print;
}
}
}