|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using DevProxy.Abstractions.Utils; |
| 6 | +using DevProxy.Abstractions.Proxy; |
| 7 | +using DevProxy.Abstractions.Plugins; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using System.Text.Json; |
| 10 | +using DevProxy.Abstractions.Models; |
| 11 | + |
| 12 | +namespace DevProxy.Plugins.Behavior; |
| 13 | + |
| 14 | +internal sealed class LanguageModelRateLimitingCustomResponseLoader( |
| 15 | + HttpClient httpClient, |
| 16 | + ILogger<LanguageModelRateLimitingCustomResponseLoader> logger, |
| 17 | + LanguageModelRateLimitConfiguration configuration, |
| 18 | + IProxyConfiguration proxyConfiguration) : |
| 19 | + BaseLoader(httpClient, logger, proxyConfiguration) |
| 20 | +{ |
| 21 | + private readonly LanguageModelRateLimitConfiguration _configuration = configuration; |
| 22 | + |
| 23 | + protected override string FilePath => _configuration.CustomResponseFile; |
| 24 | + |
| 25 | + protected override void LoadData(string fileContents) |
| 26 | + { |
| 27 | + try |
| 28 | + { |
| 29 | + var response = JsonSerializer.Deserialize<MockResponseResponse>(fileContents, ProxyUtils.JsonSerializerOptions); |
| 30 | + if (response is not null) |
| 31 | + { |
| 32 | + _configuration.CustomResponse = response; |
| 33 | + } |
| 34 | + } |
| 35 | + catch (Exception ex) |
| 36 | + { |
| 37 | + Logger.LogError(ex, "An error has occurred while reading {ConfigurationFile}:", _configuration.CustomResponseFile); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments