Exemplo n.º 1
0
        public async Task WhenApiKeyIsInvalidAndCalledMultipleTimes_DoesNotBreakAndGivesErrorCode()
        {
            //Arrange
            var client = new TmdbNetworkClient(null, "invalidkeytest1012");

            //Act
            CreateRequestTokenResult result = await client.CreateRequestToken(3, 1000);

            //Assert
            Assert.True(result.HttpStatusCode == System.Net.HttpStatusCode.Unauthorized);
        }
Exemplo n.º 2
0
        // Happy path with retry option set
        // (will only retry when code is manually set for that)
        public async Task WhenApiKeyIsValid_AndWithRetryOptionSet_CreatesRequestToken()
        {
            //Act
            CreateRequestTokenResult result = await _client.CreateRequestToken(3, 1000);

            RequestToken token = JsonConvert.DeserializeObject <RequestToken>(result.Json);

            //Assert
            Assert.True(result.HttpStatusCode == System.Net.HttpStatusCode.OK);
            Assert.True(token.Success);
            Assert.False(string.IsNullOrWhiteSpace(token.Token));
        }
Exemplo n.º 3
0
        public async Task <CreateRequestTokenResult> CreateRequestToken(int retryCount = 0, int delayMilliseconds = 1000)
        {
            string baseUrl = BASE_Address + BASE_Path + REQUEST_TOKEN_Path;

            var query = new Dictionary <string, string>();

            query.Add(API_KEY_Key, ApiKey);

            string requestUri = QueryHelpers.AddQueryString(baseUrl, query);

            CreateRequestTokenResult result = await GetResponse <CreateRequestTokenResult>(retryCount, delayMilliseconds, requestUri);

            return(result);
        }
Exemplo n.º 4
0
        public async Task <CreateRequestTokenResult> ValidateRequestTokenWithLogin(string username, string password, string requestToken, int retryCount = 0, int delayMilliseconds = 1000)
        {
            string baseUrl = BASE_Address + BASE_Path + VALIDATE_REQUEST_TOKEN_W_LOGIN_Path;

            var query = new Dictionary <string, string>();

            query.Add(API_KEY_Key, ApiKey);

            string requestUri = QueryHelpers.AddQueryString(baseUrl, query);
            var    jsonObj    = new { username = username, password = password, request_token = requestToken };
            string json       = JsonConvert.SerializeObject(jsonObj);
            var    content    = new StringContent(json, encoding: Encoding.UTF8, mediaType: "application/json");

            HttpResponseMessage response = null;
            int counter = retryCount;

            try
            {
                response = await HttpClient.PostAsync(requestUri, content);
            }
            catch { }
            while (response?.IsSuccessStatusCode != true && counter > 0)
            {
                await Task.Delay(delayMilliseconds);

                try
                {
                    --counter;
                    response = await HttpClient.PostAsync(requestUri, content);
                }
                catch { }
            }
            CreateRequestTokenResult result = new CreateRequestTokenResult {
                HttpStatusCode = response?.StatusCode ?? HttpStatusCode.ServiceUnavailable
            };

            await ReadResponseAsStringIntoResultWhenSafe(result, response);

            return(result);
        }