Ejemplo n.º 1
0
        /// <summary>
        /// Client constructor Auth credentials / ApiKey can be passed through config
        /// </summary>
        /// <param name="config">Config</param>
        /// <param name="Url">String</param>
        /// /// <param name="policy">String</param>
        public Client(IArtesianServiceConfig config, string Url, ArtesianPolicyConfig policy)
        {
            _url    = config.BaseAddress.ToString().AppendPathSegment(Url);
            _apiKey = config.ApiKey;
            _config = config;

            var cfg = new JsonSerializerSettings();

            cfg                  = cfg.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
            cfg                  = cfg.ConfigureForDictionary();
            cfg                  = cfg.ConfigureForNodaTimeRanges();
            cfg.Formatting       = Formatting.Indented;
            cfg.ContractResolver = new DefaultContractResolver();
            cfg.Converters.Add(new StringEnumConverter());
            cfg.TypeNameHandling       = TypeNameHandling.None;
            cfg.ObjectCreationHandling = ObjectCreationHandling.Replace;

            var jsonFormatter = new JsonMediaTypeFormatter
            {
                SerializerSettings = cfg
            };

            _jsonFormatter = jsonFormatter;
            _jsonFormatter.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/problem+json"));

            _msgPackFormatter    = new MessagePackMediaTypeFormatter(CustomCompositeResolver.Instance);
            _lz4msgPackFormatter = new LZ4MessagePackMediaTypeFormatter(CustomCompositeResolver.Instance);
            //Order of formatters important for correct weight in accept header
            var formatters = new MediaTypeFormatterCollection();

            formatters.Clear();
            formatters.Add(_lz4msgPackFormatter);
            formatters.Add(_msgPackFormatter);
            formatters.Add(_jsonFormatter);
            _formatters = formatters;

            _resilienceStrategy = policy.GetResillianceStrategy();

            if (config.ApiKey == null)
            {
                var domain = new Uri(config.Domain);

                var tenantId = domain.Segments
                               .Select(s => s.Trim('/'))
                               .Where(w => !string.IsNullOrWhiteSpace(w))
                               .FirstOrDefault();

                _confidentialClientApplication = ConfidentialClientApplicationBuilder
                                                 .Create(config.ClientId)
                                                 .WithTenantId(tenantId)
                                                 .WithClientSecret(config.ClientSecret)
                                                 .Build();
            }

            _client = new FlurlClient(_url);
            _client.WithTimeout(TimeSpan.FromMinutes(ArtesianConstants.ServiceRequestTimeOutMinutes));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Query service for building a query
 /// </summary>
 /// <param name="cfg">IArtesianServiceConfig</param>
 /// <param name="policy">ArtesianPolicyConfig</param>
 public QueryService(IArtesianServiceConfig cfg, ArtesianPolicyConfig policy)
 {
     _cfg    = cfg;
     _policy = policy;
     _client = new Client(cfg, ArtesianConstants.QueryRoute.AppendPathSegment(ArtesianConstants.QueryVersion), _policy);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Metadata service
 /// </summary>
 /// <param name="cfg">IArtesianServiceConfig</param>
 /// <param name="policy">ArtesianPolicyConfig</param>
 public MetadataService(IArtesianServiceConfig cfg, ArtesianPolicyConfig policy)
 {
     _cfg    = cfg;
     _policy = policy;
     _client = new Client(cfg, ArtesianConstants.MetadataVersion, _policy);
 }