Revize 522ba9b4
Přidáno uživatelem Pultak před asi 2 roky(ů)
ld_client/LDClient/network/ApiClient.cs | ||
---|---|---|
50 | 50 |
try { |
51 | 51 |
Stopwatch stopWatch = new(); |
52 | 52 |
stopWatch.Start(); |
53 |
|
|
54 |
var json = JsonConvert.SerializeObject(ExampleInfo); |
|
55 |
if (json is null) { |
|
56 |
Program.DefaultLogger.Error($"Failed to serialize object: {ExampleInfo}"); |
|
57 |
return; |
|
58 |
} |
|
53 |
|
|
59 | 54 |
var response = await _client.PostAsJsonAsync(_uri, payload, new JsonSerializerOptions { |
60 | 55 |
Converters = { |
61 | 56 |
new JsonStringEnumConverter( JsonNamingPolicy.CamelCase) |
... | ... | |
63 | 58 |
|
64 | 59 |
}); |
65 | 60 |
stopWatch.Stop(); |
66 |
Response2Log(json, response, stopWatch.ElapsedMilliseconds);
|
|
61 |
CreateRequestLog(payload, response, stopWatch.ElapsedMilliseconds);
|
|
67 | 62 |
|
68 | 63 |
response.EnsureSuccessStatusCode(); |
69 |
var serverResponse = await response.Content.ReadAsStringAsync(); |
|
70 |
CheckResponse(serverResponse); |
|
71 | 64 |
} catch (Exception e) { |
72 | 65 |
Program.DefaultLogger.Error($"Failed to send {payload} to the server. Due to: {e.Message}"); |
66 |
CachePayload(payload); |
|
73 | 67 |
} |
74 | 68 |
} |
75 |
|
|
76 |
private static bool CheckResponse(string response) { |
|
77 |
dynamic json = JObject.Parse(response); |
|
78 | 69 |
|
79 |
if (json.statusCode < 400) { |
|
80 |
return true; |
|
81 |
} |
|
82 |
throw new Exception($"Server responded with error code: {json.statusCode}"); |
|
83 |
} |
|
84 |
|
|
85 |
private static void Response2Log(string json, HttpResponseMessage response, long durationMs) { |
|
70 |
private static void CreateRequestLog(Payload payload, HttpResponseMessage response, long durationMs) { |
|
86 | 71 |
var responseToLog = new { |
87 | 72 |
statusCode = response.StatusCode, |
88 | 73 |
content = response.Content, |
... | ... | |
91 | 76 |
}; |
92 | 77 |
|
93 | 78 |
Program.DefaultLogger.Info($"Request completed in {durationMs} ms,\n" + |
94 |
$"Request body: {json},\n" + |
|
95 |
$"Response: {JsonConvert.SerializeObject(responseToLog)}"); |
|
79 |
$"Request body: {payload},\n" + |
|
80 |
$"Response: {responseToLog}"); |
|
81 |
} |
|
82 |
|
|
96 | 83 |
|
97 | 84 |
private async Task ResendPayloadsAsync() { |
98 | 85 |
var numberOfPayloadsToResend = Math.Min(_maxRetries, _cache.EstimatedCountOfItemsInQueue); |
Také k dispozici: Unified diff
re #9441 manager refactoring - removal of useless functions and variables