コード例 #1
0
 public AblySimpleRestClient(AblyOptions options, IAblyHttpClient httpClient)
 {
     _options        = options;
     _httpClient     = httpClient;
     _protocol       = _options.UseBinaryProtocol == false ? Protocol.Json : Protocol.MsgPack;
     _messageHandler = new MessageHandler(_protocol);
 }
コード例 #2
0
ファイル: RestTests.cs プロジェクト: Const-me/ably-dotnet
        public void ClientWithExistingTokenReusesItForMakingRequests()
        {
            var options = new AblyOptions
            {
                ClientId          = "test",
                Key               = "best",
                UseBinaryProtocol = false
            };
            var rest  = new RestClient(options);
            var token = new TokenDetails("123")
            {
                Expires = DateTimeOffset.UtcNow.AddHours(1)
            };

            rest.CurrentToken = token;

            rest.ExecuteHttpRequest = request =>
            {
                //Assert
                request.Headers["Authorization"].Should().Contain(token.Token.ToBase64());
                return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                });
            };

            rest.Stats();
            rest.Stats();
            rest.Stats();
        }
コード例 #3
0
ファイル: RestTests.cs プロジェクト: Const-me/ably-dotnet
        public void ClientWithExpiredTokenAutomaticallyCreatesANewOne()
        {
            Config.Now = () => DateTimeOffset.UtcNow;
            var newTokenRequested = false;
            var options           = new AblyOptions
            {
                AuthCallback = (x) => {
                    Console.WriteLine("Getting new token.");
                    newTokenRequested = true; return(new TokenDetails("new.token")
                    {
                        Expires = DateTimeOffset.UtcNow.AddDays(1)
                    });
                },
                UseBinaryProtocol = false
            };
            var rest = new RestClient(options);

            rest.ExecuteHttpRequest = request =>
            {
                Console.WriteLine("Getting an AblyResponse.");
                return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                });
            };
            rest.CurrentToken = new TokenDetails()
            {
                Expires = DateTimeOffset.UtcNow.AddDays(-2)
            };

            Console.WriteLine("Current time:" + Config.Now());
            rest.Stats();
            newTokenRequested.Should().BeTrue();
            rest.CurrentToken.Token.Should().Be("new.token");
        }
コード例 #4
0
        private static string GetHost(AblyOptions options)
        {
            if (options.Host.IsNotEmpty())
            {
                return(options.Host);
            }

            return(Config.DefaultHost);
        }
コード例 #5
0
ファイル: RestTests.cs プロジェクト: Const-me/ably-dotnet
        public void Init_WithKeyNoClientIdAndAuthTokenId_SetsCurrentTokenWithSuppliedId()
        {
            AblyOptions options = new AblyOptions {
                Key = ValidKey, ClientId = "123", Token = "222"
            };
            var client = new RestClient(options);

            Assert.Equal(options.Token, client.CurrentToken.Token);
        }
コード例 #6
0
        public AblyOptions CreateOptions(string key)
        {
            var opts = new AblyOptions()
            {
                Key = key
            };

            FillInOptions(opts);
            return(opts);
        }
コード例 #7
0
        private RestClient GetAbly()
        {
            var testData = TestsSetup.TestData;

            var options = new AblyOptions
            {
                Key = testData.keys.First().keyStr,
                UseBinaryProtocol = _protocol == Protocol.MsgPack,
                Environment       = AblyEnvironment.Sandbox
            };
            var ably = new RestClient(options);

            return(ably);
        }
コード例 #8
0
ファイル: RestTests.cs プロジェクト: Const-me/ably-dotnet
        public void Init_WithAuthUrl_CallsTheUrlOnFirstRequest()
        {
            bool called  = false;
            var  options = new AblyOptions
            {
                AuthUrl           = "http://testUrl",
                UseBinaryProtocol = false
            };

            var rest = new RestClient(options);

            rest.ExecuteHttpRequest = request =>
            {
                if (request.Url.Contains(options.AuthUrl))
                {
                    called = true;
                    return(new AblyResponse()
                    {
                        TextResponse = "{}"
                    });
                }

                if (request.Url.Contains("requestToken"))
                {
                    return(new AblyResponse {
                        TextResponse = "{ \"access_token\": { \"expires\": \"" + DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeInMilliseconds() + "\"}}"
                    });
                }

                return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                });
            };

            rest.Stats();

            Assert.True(called, "Rest with Callback needs to request token using callback");
        }
コード例 #9
0
ファイル: RestTests.cs プロジェクト: Const-me/ably-dotnet
        public void Init_WithCallback_ExecutesCallbackOnFirstRequest()
        {
            bool called  = false;
            var  options = new AblyOptions
            {
                AuthCallback      = (x) => { called = true; return(new TokenDetails()
                    {
                        Expires = DateTimeOffset.UtcNow.AddHours(1)
                    }); },
                UseBinaryProtocol = false
            };

            var rest = new RestClient(options);

            rest.ExecuteHttpRequest = delegate { return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                }); };

            rest.Stats();

            Assert.True(called, "Rest with Callback needs to request token using callback");
        }
コード例 #10
0
 public AblySimpleRestClient(AblyOptions options)
     : this(options, new AblyHttpClient(GetHost(options), options.Port, options.Tls, options.Environment))
 {
 }
コード例 #11
0
 public void FillInOptions(AblyOptions opts)
 {
     opts.Host = restHost;
     opts.Port = restPort;
     opts.Tls  = tls;
 }