• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ThreeMammals / Ocelot / 20926763792

12 Jan 2026 04:23PM UTC coverage: 93.397% (-0.1%) from 93.503%
20926763792

Pull #1183

github

web-flow
Merge 94e251d0b into 9fc4e78d3
Pull Request #1183: #651 Merge custom JSON properties across multiple `ocelot.X.json` configuration files using Newtonsoft's `JToken` merge functionality

6549 of 7012 relevant lines covered (93.4%)

2996.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
src/Ocelot.Provider.Consul/ConsulFileConfigurationRepository.cs
1
using Microsoft.Extensions.Options;
2
using Newtonsoft.Json;
3
using Ocelot.Cache;
4
using Ocelot.Configuration;
5
using Ocelot.Configuration.File;
6
using Ocelot.Configuration.Repository;
7
using Ocelot.Logging;
8
using Ocelot.Provider.Consul.Interfaces;
9
using Ocelot.Responses;
10
using System.Text;
11

12
namespace Ocelot.Provider.Consul;
13

14
public class ConsulFileConfigurationRepository : IFileConfigurationRepository
15
{
16
    private readonly IOcelotCache<FileConfiguration> _cache;
17
    private readonly string _configurationKey;
18
    private readonly IConsulClient _consul;
19
    private readonly IOcelotLogger _logger;
20

21
    public ConsulFileConfigurationRepository(
7✔
22
        IOptions<FileConfiguration> fileConfiguration,
7✔
23
        IOcelotCache<FileConfiguration> cache,
7✔
24
        IConsulClientFactory factory,
7✔
25
        IOcelotLoggerFactory loggerFactory)
7✔
26
    {
27
        _logger = loggerFactory.CreateLogger<ConsulFileConfigurationRepository>();
7✔
28
        _cache = cache;
7✔
29

30
        var provider = fileConfiguration.Value.GlobalConfiguration.ServiceDiscoveryProvider;
7✔
31
        _configurationKey = string.IsNullOrWhiteSpace(provider.ConfigurationKey)
7✔
32
            ? nameof(InternalConfiguration)
7✔
33
            : provider.ConfigurationKey;
7✔
34

35
        var config = new ConsulRegistryConfiguration(provider.Scheme, provider.Host,
7✔
36
            provider.Port, _configurationKey, provider.Token);
7✔
37
        _consul = factory.Get(config);
7✔
38
    }
7✔
39

40
    public async Task<FileConfiguration> GetAsync()
41
    {
42
        var config = _cache.Get(_configurationKey, _configurationKey);
5✔
43
        if (config != null)
5✔
44
        {
45
            return config;
1✔
46
        }
47

48
        var queryResult = await _consul.KV.Get(_configurationKey);
4✔
49
        if (queryResult.Response == null)
4✔
50
        {
51
            return null;
1✔
52
        }
53

54
        var bytes = queryResult.Response.Value;
3✔
55
        var json = Encoding.UTF8.GetString(bytes);
3✔
56
        var consulConfig = JsonConvert.DeserializeObject<FileConfiguration>(json);
3✔
57

58
        return consulConfig;
3✔
59
    }
5✔
60

61
    public async Task SetAsync(FileConfiguration ocelotConfiguration)
62
    {
63
        var json = JsonConvert.SerializeObject(ocelotConfiguration, Formatting.Indented);
1✔
64
        var bytes = Encoding.UTF8.GetBytes(json);
1✔
65
        var kvPair = new KVPair(_configurationKey)
1✔
66
        {
1✔
67
            Value = bytes,
1✔
68
        };
1✔
69

70
        var result = await _consul.KV.Put(kvPair);
1✔
71
        if (!result.Response)
1✔
72
        {
73
            throw new UnableToSetConfigInConsulException(
×
74
                $"Unable to set {nameof(FileConfiguration)} in {nameof(Consul)}! Response status code from {nameof(Consul)} was {result.StatusCode} being returned in {result.RequestTime.TotalMilliseconds} ms.");
×
75
        }
76

77
        _cache.AddOrUpdate(_configurationKey, ocelotConfiguration, _configurationKey, TimeSpan.FromSeconds(3));
1✔
78
    }
1✔
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc