Exemplo n.º 1
0
        public async Task ClientWithExistingTokenReusesItForMakingRequests()
        {
            var options = new ClientOptions
            {
                ClientId          = "test",
                Key               = "best",
                UseBinaryProtocol = false,
                NowFunc           = TestHelpers.NowFunc()
            };
            var rest  = new AblyRest(options);
            var token = new TokenDetails("123")
            {
                Expires = DateTimeOffset.UtcNow.AddHours(1)
            };

            rest.AblyAuth.CurrentToken = token;

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

            await rest.StatsAsync();

            await rest.StatsAsync();

            await rest.StatsAsync();
        }
Exemplo n.º 2
0
        public async Task Init_WithAuthUrl_CallsTheUrlOnFirstRequest()
        {
            bool called  = false;
            var  options = new ClientOptions
            {
                AuthUrl           = new Uri("http://testUrl"),
                UseBinaryProtocol = false
            };

            var rest = new AblyRest(options);

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

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

                return("[{}]".ToAblyResponse());
            };

            await rest.StatsAsync();

            Assert.True(called, "Rest with Callback needs to request token using callback");
        }
Exemplo n.º 3
0
        public async Task ClientWithExpiredTokenAutomaticallyCreatesANewOne()
        {
            bool newTokenRequested = false;
            var  options           = new ClientOptions
            {
                AuthCallback = (x) =>
                {
                    newTokenRequested = true;
                    return(Task.FromResult <object>(new TokenDetails("new.token")
                    {
                        Expires = DateTimeOffset.UtcNow.AddDays(1)
                    }));
                },
                UseBinaryProtocol = false,
                NowFunc           = TestHelpers.NowFunc()
            };
            var rest = new AblyRest(options);

            rest.ExecuteHttpRequest    = request => "[{}]".ToAblyResponse();
            rest.AblyAuth.CurrentToken = new TokenDetails()
            {
                Expires = DateTimeOffset.UtcNow.AddDays(-2)
            };

            await rest.StatsAsync();

            newTokenRequested.Should().BeTrue();
            rest.AblyAuth.CurrentToken.Token.Should().Be("new.token");
        }
Exemplo n.º 4
0
        public static async Task RestStatsSamples()
        {
            var rest = new AblyRest("{{API_KEY}}");
            PaginatedResult <Stats> results = await rest.StatsAsync(new StatsRequestParams { Unit = StatsIntervalGranularity.Hour });

            Stats thisHour = results.Items[0];

            Console.WriteLine($"Published this hour {thisHour.Inbound.All.All.Count}");
        }
Exemplo n.º 5
0
        public async Task RestApiSamples()
        {
            var          client  = new AblyRest(PlaceholderKey);
            IRestChannel channel = client.Channels.Get("test");

            try
            {
                await channel.PublishAsync("name", "data");
            }
            catch (AblyException)
            {
                // Log error
            }

            // History
            var historyPage = await channel.HistoryAsync();

            foreach (var message in historyPage.Items)
            {
                // Do something with each message
            }

            // Get next page
            var nextHistoryPage = await historyPage.NextAsync();

            // Current presence
            var presence = await channel.Presence.GetAsync();

            var first            = presence.Items.FirstOrDefault();
            var clientId         = first.ClientId; // 'clientId' of the first member present
            var nextPresencePage = await presence.NextAsync();

            foreach (var presenceMessage in nextPresencePage.Items)
            {
                // Do stuff with next page presence messages
            }

            // Presence history
            var presenceHistory = await channel.Presence.HistoryAsync();

            foreach (var presenceMessage in presenceHistory.Items)
            {
                // Do stuff with presence messages
            }

            var nextPage = await presenceHistory.NextAsync();

            foreach (var presenceMessage in nextPage.Items)
            {
                // Do stuff with next page messages
            }

            // Publishing encrypted messages
            var          secret           = Crypto.GenerateRandomKey();
            IRestChannel encryptedChannel = client.Channels.Get("encryptedChannel", new ChannelOptions(secret));
            await encryptedChannel.PublishAsync("name", "sensitive data"); // Data will be encrypted before publish

            var history = await encryptedChannel.HistoryAsync();

            var data = history.Items.First().Data;

            // "sensitive data" the message will be automatically decrypted once received

            // Generate a token
            var token = await client.Auth.RequestTokenAsync();

            var tokenString = token.Token; // "xVLyHw.CLchevH3hF....MDh9ZC_Q"
            var tokenClient = new AblyRest(new ClientOptions {
                TokenDetails = token
            });

            var tokenRequest = await client.Auth.CreateTokenRequestAsync();

            // Stats
            var stats = await client.StatsAsync();

            var firstItem     = stats.Items.First();
            var nextStatsPage = await stats.NextAsync();

            var time = await client.TimeAsync();
        }