-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResponseHandler.cs
230 lines (208 loc) · 9.34 KB
/
ResponseHandler.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
223
224
225
226
227
228
229
230
// <copyright file="ResponseHandler.cs" company="APIMatic">
// Copyright (c) APIMatic. All rights reserved.
// </copyright>
using System;
using System.Collections.Generic;
using APIMatic.Core.Http.Configuration;
using APIMatic.Core.Types;
using APIMatic.Core.Types.Sdk;
using APIMatic.Core.Utilities;
namespace APIMatic.Core.Response
{
/// <summary>
/// Used to handle and process the response received from HttpClient
/// </summary>
/// <typeparam name="Request"> Class Type that holds http request info </typeparam>
/// <typeparam name="Response"> Class Type that holds http response info </typeparam>
/// <typeparam name="Context"> Class Type that holds http context info </typeparam>
/// <typeparam name="ApiException"> Class Type that holds BaseException info </typeparam>
/// <typeparam name="ResponseType"> Expected type of http response </typeparam>
public class ResponseHandler<Request, Response, Context, ApiException, ResponseType>
where Request : CoreRequest
where Response : CoreResponse
where Context : CoreContext<Request, Response>
where ApiException : CoreApiException<Request, Response, Context>
{
private readonly Dictionary<string, ErrorCase<Request, Response, Context, ApiException>> globalErrors;
private readonly Dictionary<string, ErrorCase<Request, Response, Context, ApiException>> localErrors;
private readonly ICompatibilityFactory<Request, Response, Context, ApiException> compatibilityFactory;
private bool nullOn404 = false;
private Func<string, ResponseType> deserializer = responseBody => CoreHelper.JsonDeserialize<ResponseType>(responseBody);
private Func<ResponseType, Context, ResponseType> contextAdder = (result, context) => result;
internal ContentType AcceptHeader { get; set; } = ContentType.JSON;
internal ResponseHandler(ICompatibilityFactory<Request, Response, Context, ApiException> compatibilityFactory,
Dictionary<string, ErrorCase<Request, Response, Context, ApiException>> globalErrors)
{
this.compatibilityFactory = compatibilityFactory;
this.globalErrors = globalErrors ?? new Dictionary<string, ErrorCase<Request, Response, Context, ApiException>>();
localErrors = new Dictionary<string, ErrorCase<Request, Response, Context, ApiException>>();
if (CoreHelper.IsScalarType(typeof(ResponseType)))
{
AcceptHeader = ContentType.SCALAR;
}
}
/// <summary>
/// This adds an case for throwing error, for a particular error code
/// </summary>
/// <param name="statusCode"></param>
/// <param name="error"></param>
/// <returns></returns>
public ResponseHandler<Request, Response, Context, ApiException, ResponseType> ErrorCase(
string statusCode, ErrorCase<Request, Response, Context, ApiException> errorCase)
{
localErrors[statusCode] = errorCase;
return this;
}
/// <summary>
/// This controls returning null on error code 404
/// </summary>
/// <returns></returns>
public ResponseHandler<Request, Response, Context, ApiException, ResponseType> NullOn404()
{
nullOn404 = true;
return this;
}
/// <summary>
/// This controls converting response into xml response
/// </summary>
/// <returns></returns>
public ResponseHandler<Request, Response, Context, ApiException, ResponseType> XmlResponse()
{
AcceptHeader = ContentType.XML;
return this;
}
/// <summary>
/// Used to Deserialize the body of the received http response
/// </summary>
/// <param name="deserializer"></param>
/// <returns></returns>
public ResponseHandler<Request, Response, Context, ApiException, ResponseType> Deserializer(
Func<string, ResponseType> deserializer)
{
this.deserializer = deserializer;
return this;
}
/// <summary>
/// Updates the actual http response with http context using the provided function
/// </summary>
/// <param name="contextAdder"></param>
/// <returns></returns>
public ResponseHandler<Request, Response, Context, ApiException, ResponseType> ContextAdder(
Func<ResponseType, Context, ResponseType> contextAdder)
{
this.contextAdder = contextAdder;
return this;
}
/// <summary>
/// This applies all the configurations, processes the http response and returns expected response
/// </summary>
/// <typeparam name="ReturnType"> Real expected type from the API endpoint </typeparam>
/// <param name="context"></param>
/// <param name="returnTypeCreator"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
internal ReturnType Result<ReturnType>(CoreContext<CoreRequest, CoreResponse> context, Func<Response, ResponseType, ReturnType> returnTypeCreator)
{
if (context.IsFailure())
{
if (nullOn404 && context.Response.StatusCode == 404)
{
return default;
}
throw ResponseError(context);
}
ResponseType result = ConvertResponse(context.Response);
result = contextAdder(result, compatibilityFactory.CreateHttpContext(context.Request, context.Response));
if (returnTypeCreator != null)
{
return returnTypeCreator(compatibilityFactory.CreateHttpResponse(context.Response), result);
}
if (EqualityComparer<ResponseType>.Default.Equals(result, default))
{
return default;
}
if (result is ReturnType convertedResult)
{
return convertedResult;
}
throw new InvalidOperationException($"Unable to transform {typeof(ResponseType)} into {typeof(ReturnType)}. ReturnTypeCreator is not provided.");
}
private ApiException ResponseError(CoreContext<CoreRequest, CoreResponse> context)
{
Context httpContext = compatibilityFactory.CreateHttpContext(context.Request, context.Response);
var statusCode = context.Response.StatusCode.ToString();
if (GetStatusCodeErrorCase(localErrors, statusCode, out var localErrorCase))
{
return localErrorCase.GetError(httpContext);
}
if (GetStatusCodeErrorCase(globalErrors, statusCode, out var globalErrorCase))
{
return globalErrorCase.GetError(httpContext);
}
if (GetDefaultErrorCaseFromErrors(out var defaultErrorCase))
{
return defaultErrorCase.GetError(httpContext);
}
return compatibilityFactory.CreateApiException("HTTP Response Not OK", context);
}
private bool GetStatusCodeErrorCase(Dictionary<string, ErrorCase<Request, Response, Context, ApiException>> errors, string statusCode, out ErrorCase<Request, Response, Context, ApiException> errorCase)
{
if (errors.TryGetValue(statusCode, out errorCase))
{
return true;
}
if (GetRangedStatusCodeErrorCase(errors, statusCode, out errorCase))
{
return true;
}
return false;
}
private bool GetRangedStatusCodeErrorCase(Dictionary<string, ErrorCase<Request, Response, Context, ApiException>> errors, string statusCode, out ErrorCase<Request, Response, Context, ApiException> errorCase)
{
errorCase = default;
if (statusCode.StartsWith("4") || statusCode.StartsWith("5"))
{
var rangedStatusCode = $"{statusCode[0]}XX";
return errors.TryGetValue(rangedStatusCode, out errorCase);
}
return false;
}
private bool GetDefaultErrorCaseFromErrors(out ErrorCase<Request, Response, Context, ApiException> errorCase)
{
if (localErrors.TryGetValue("0", out errorCase))
{
return true;
}
if (globalErrors.TryGetValue("0", out errorCase))
{
return true;
}
return false;
}
private ResponseType ConvertResponse(CoreResponse response)
{
if (HasEmptyResponse(response))
{
return default;
}
if (response.RawBody is ResponseType streamResponse)
{
return streamResponse;
}
if (response.Body is ResponseType stringResponse && AcceptHeader != ContentType.XML)
{
return stringResponse;
}
return deserializer(response.Body);
}
private bool HasEmptyResponse(CoreResponse response)
{
var resType = typeof(ResponseType);
if (typeof(VoidType).IsAssignableFrom(resType))
{
return true;
}
return string.Equals(response.Body?.Trim(), string.Empty) && CoreHelper.IsNullableType(resType);
}
}
}