/// <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; }
/// <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"); }
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"); }
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"); }
/// <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; } }
/// <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; } }
public void ApiMethodsDefaultToNullContentType() { var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country)); Assert.IsNull(resolver.ContentType); }
public void ApiMethodsDefaultToGetHttpMethod() { var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country)); Assert.AreEqual(HttpMethod.Get, resolver.HttpMethod); }
public void ApiMethodsDefaultToNullContentType() { var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country)); Assert.IsNull(resolver.ContentType); }
public void ApiMethodsDefaultToGetHttpMethod() { var resolver = new CountryResolverCommand("test", new MockApiRequestHandler(Resources.country)); Assert.AreEqual(HttpMethod.Get, resolver.HttpMethod); }
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"); }