Пример #1
0
        public void DefaultOptions()
        {
            var options = new ClientOptions();

            options.FullRealtimeHost().Should().Be("realtime.ably.io");
            options.FullRestHost().Should().Be("rest.ably.io");
            options.Port.Should().Be(80);
            options.TlsPort.Should().Be(443);
            Assert.Equal(Defaults.FallbackHosts, options.GetFallbackHosts());
            options.Tls.Should().BeTrue();
        }
Пример #2
0
        public void Options_WithCustomRestHost()
        {
            var options = new ClientOptions
            {
                RestHost = "test.org"
            };

            options.FullRestHost().Should().Be("test.org");
            options.FullRealtimeHost().Should().Be("test.org");
            options.Port.Should().Be(80);
            options.TlsPort.Should().Be(443);
            Assert.Empty(options.GetFallbackHosts());
            options.Tls.Should().BeTrue();
        }
Пример #3
0
        public void Options_WithCustomEnvironment()
        {
            var options = new ClientOptions
            {
                Environment = "sandbox"
            };

            options.FullRealtimeHost().Should().Be("sandbox-realtime.ably.io");
            options.FullRestHost().Should().Be("sandbox-rest.ably.io");
            options.Port.Should().Be(80);
            options.TlsPort.Should().Be(443);
            Assert.Equal(Defaults.GetEnvironmentFallbackHosts("sandbox"), options.GetFallbackHosts());
            options.Tls.Should().BeTrue();
        }
Пример #4
0
        public void Options_WithProductionEnvironment()
        {
            var options = new ClientOptions
            {
                Environment = "production"
            };

            options.FullRealtimeHost().Should().Be("realtime.ably.io");
            options.FullRestHost().Should().Be("rest.ably.io");
            options.Port.Should().Be(80);
            options.TlsPort.Should().Be(443);
            Assert.Equal(Defaults.FallbackHosts, options.GetFallbackHosts());
            options.Tls.Should().BeTrue();
        }
Пример #5
0
        public void Options_WithCustomEnvironment_And_NonDefaultPorts()
        {
            var options = new ClientOptions
            {
                Environment = "local",
                Port        = 8080,
                TlsPort     = 8081
            };

            options.FullRealtimeHost().Should().Be("local-realtime.ably.io");
            options.FullRestHost().Should().Be("local-rest.ably.io");
            options.Port.Should().Be(8080);
            options.TlsPort.Should().Be(8081);
            Assert.Empty(options.GetFallbackHosts());
            options.Tls.Should().BeTrue();
        }
Пример #6
0
        internal static async Task <TransportParams> Create(string host, AblyAuth auth, ClientOptions options, string connectionKey = null, long?connectionSerial = null, ILogger logger = null)
        {
            var result = new TransportParams
            {
                Host                 = host,
                Tls                  = options.Tls,
                Port                 = options.Tls ? options.TlsPort : options.Port,
                ClientId             = options.GetClientId(),
                ConnectionKey        = connectionKey,
                ConnectionSerial     = connectionSerial,
                EchoMessages         = options.EchoMessages,
                FallbackHosts        = options.GetFallbackHosts(),
                UseBinaryProtocol    = options.UseBinaryProtocol,
                RecoverValue         = options.Recover,
                Logger               = logger ?? options.Logger,
                AdditionalParameters = StringifyParameters(options.TransportParams),
                AuthMethod           = auth.AuthMethod,
            };

            if (result.AuthMethod == AuthMethod.Basic)
            {
                result.AuthValue = ApiKey.Parse(options.Key).ToString();
            }
            else
            {
                var token = await auth.GetCurrentValidTokenAndRenewIfNecessaryAsync();

                if (token == null)
                {
                    throw new AblyException("There is no valid token. Can't authenticate", ErrorCodes.Unauthorized, HttpStatusCode.Unauthorized);
                }

                result.AuthValue = token.Token;
            }

            return(result);

            Dictionary <string, string> StringifyParameters(Dictionary <string, object> originalParams)
            {
                if (originalParams is null)
                {
                    return(new Dictionary <string, string>());
                }

                return(originalParams.ToDictionary(x => x.Key, x => ConvertValue(x.Key, x.Value)));

                string ConvertValue(string key, object value)
                {
                    switch (value)
                    {
                    case bool boolValue:
                        return(boolValue.ToString().ToLower());

                    case null:
                        return(string.Empty);

                    default:
                        try
                        {
                            return(value.ToString());
                        }
                        catch (Exception e)
                        {
                            logger?.Error($"Error converting custom transport parameter '{key}'. Error: {e.Message}");

                            return(string.Empty);
                        }
                    }
                }
            }
        }