internal static async Task <TransportParams> Create(string host, AblyAuth auth, ClientOptions options, string connectionKey = null, long?connectionSerial = null)
        {
            var result = new TransportParams();

            result.Host       = host;
            result.Tls        = options.Tls;
            result.Port       = options.Tls ? options.TlsPort : options.Port;
            result.ClientId   = options.GetClientId();
            result.AuthMethod = auth.AuthMethod;
            if (result.AuthMethod == AuthMethod.Basic)
            {
                result.AuthValue = options.Key;
            }
            else
            {
                var token = await auth.GetCurrentValidTokenAndRenewIfNecessaryAsync();

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

                result.AuthValue = token.Token;
            }
            result.ConnectionKey     = connectionKey;
            result.ConnectionSerial  = connectionSerial;
            result.EchoMessages      = options.EchoMessages;
            result.FallbackHosts     = Defaults.FallbackHosts;
            result.UseBinaryProtocol = options.UseBinaryProtocol;
            result.RecoverValue      = options.Recover;

            return(result);
        }
Exemple #2
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);
                        }
                    }
                }
            }
        }