/// <summary>
        /// Initializes a new instance of the <see cref="CountryResolver" /> class.
        /// </summary>
        /// <param name="appId">The App ID obtained from api.developer.nokia.com</param>
        /// <param name="requestHandler">The request handler.</param>
        /// <param name="requestId">A unique id to associate with this request.</param>
        /// <remarks>
        /// Allows custom requestHandler for testing purposes
        /// </remarks>
        internal CountryResolver(string appId, IApiRequestHandler requestHandler, Guid? requestId = null)
        {
            if (string.IsNullOrEmpty(appId))
            {
                throw new ApiCredentialsRequiredException();
            }

            this._requestHandler = requestHandler;
            this._command = new CountryResolverCommand(appId, requestHandler);
            if (requestId.HasValue)
            {
                this._command.RequestId = requestId.Value;
            }
        }
        /// <summary>
        /// Validates that the Nokia MixRadio API is available for a country
        /// </summary>
        /// <param name="countryCode">The country code.</param>
        /// <returns>
        /// A Response containing whether the API is available or not
        /// </returns>
        public async Task<bool> CheckAvailabilityAsync(string countryCode)
        {
            if (!this.ValidateCountryCode(countryCode))
            {
                throw new InvalidCountryCodeException();
            }

            CountryResolverCommand command = new CountryResolverCommand(this.ClientId, this.RequestHandler)
            {
                CountryCode = countryCode,
                RequestId = new Guid()
            };

            var response = await command.InvokeAsync();
            return response.Result;
        }
Esempio n. 3
0
        /// <summary>
        /// Validates that the Nokia Music API is available for a country
        /// </summary>
        /// <param name="countryCode">The country code.</param>
        /// <returns>
        /// A Response containing whether the API is available or not
        /// </returns>
        public async Task <bool> CheckAvailabilityAsync(string countryCode)
        {
            if (!this.ValidateCountryCode(countryCode))
            {
                throw new InvalidCountryCodeException();
            }

            CountryResolverCommand command = new CountryResolverCommand(this.ClientId, this.RequestHandler)
            {
                CountryCode = countryCode,
                RequestId   = new Guid()
            };

            var response = await command.InvokeAsync();

            return(response.Result);
        }
        public async Task AttemptToGetJsonFromRealButNonJsonUri()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new ApiUriBuilder());

            var command = new CountryResolverCommand("test", null) { BaseApiUri = "http://mixrad.io/gb/en/badurl" };

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode.Value, "Expected 404");
        }
Esempio n. 5
0
        public async Task AttemptToGetJsonFromRealButNonJsonUri()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new ApiUriBuilder());

            var command = new CountryResolverCommand("test", null)
            {
                BaseApiUri = "http://mixrad.io/gb/en/badurl"
            };

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode.Value, "Expected 404");
        }
Esempio n. 6
0
        public async Task AttemptToGetJsonFromRealUriAndClientError()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new ApiUriBuilder());

            var command = new CountryResolverCommand("test", null)
            {
                BaseApiUri = MusicClientCommand.DefaultBaseApiUri
            };

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNull(result.Error, "Expected no error");
            Assert.IsNotNull(result.Result, "Expected a result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
            Assert.AreEqual(HttpStatusCode.Unauthorized, result.StatusCode.Value, "Expected 401");
        }
Esempio n. 7
0
        /// <summary>
        /// Validates that the MixRadio API is available for a country
        /// </summary>
        /// <param name="countryCode">The country code.</param>
        /// <param name="cancellationToken">An optional CancellationToken</param>
        /// <returns>
        /// A Response containing whether the API is available or not
        /// </returns>
        public async Task <bool> CheckAvailabilityAsync(string countryCode, CancellationToken?cancellationToken = null)
        {
            if (!this.ValidateCountryCode(countryCode))
            {
                throw new InvalidCountryCodeException();
            }

            CountryResolverCommand command = new CountryResolverCommand(this.ClientId, this.RequestHandler)
            {
                CountryCode = countryCode,
                RequestId   = new Guid()
            };

            var response = await command.ExecuteAsync(cancellationToken);

            if (response.Error == null)
            {
                return(response.Result);
            }
            else
            {
                throw response.Error;
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Validates that the MixRadio API is available for a country
        /// </summary>
        /// <param name="countryCode">The country code.</param>
        /// <param name="cancellationToken">An optional CancellationToken</param>
        /// <returns>
        /// A Response containing whether the API is available or not
        /// </returns>
        public async Task<bool> CheckAvailabilityAsync(string countryCode, CancellationToken? cancellationToken = null)
        {
            if (!this.ValidateCountryCode(countryCode))
            {
                throw new InvalidCountryCodeException();
            }

            CountryResolverCommand command = new CountryResolverCommand(this.ClientId, this.RequestHandler)
            {
                CountryCode = countryCode,
                RequestId = new Guid()
            };

            var response = await command.ExecuteAsync(cancellationToken);

            if (response.Error == null)
            {
                return response.Result;
            }
            else
            {
                throw response.Error;
            }
        }
Esempio n. 9
0
        public void ApiMethodsDefaultToNullContentType()
        {
            var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country));

            Assert.IsNull(resolver.ContentType);
        }
Esempio n. 10
0
        public void ApiMethodsDefaultToGetHttpMethod()
        {
            var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country));

            Assert.AreEqual(HttpMethod.Get, resolver.HttpMethod);
        }
Esempio n. 11
0
 public void ApiMethodsDefaultToNullContentType()
 {
     var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country));
     Assert.IsNull(resolver.ContentType);
 }
Esempio n. 12
0
 public void ApiMethodsDefaultToGetHttpMethod()
 {
     var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country));
     Assert.AreEqual(HttpMethod.Get, resolver.HttpMethod);
 }
Esempio n. 13
0
        public async Task AttemptToGetJsonFromRealUriAndClientError()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new ApiUriBuilder());

            var command = new CountryResolverCommand("test", null) { BaseApiUri = MusicClientCommand.DefaultBaseApiUri };

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNull(result.Error, "Expected no error");
            Assert.IsNotNull(result.Result, "Expected a result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
            Assert.AreEqual(HttpStatusCode.Unauthorized, result.StatusCode.Value, "Expected 401");
        }