Make GetConfig async

Fixes #236
This commit is contained in:
Michael Bisbjerg 2018-01-21 13:38:01 +01:00
parent 049fba9f72
commit ae2c8c5e13
8 changed files with 30 additions and 25 deletions

View File

@ -9,6 +9,7 @@ using RestClient = TMDbLib.Rest.RestClient;
using RestRequest = TMDbLib.Rest.RestRequest;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace TMDbLib.Client
{
@ -165,9 +166,9 @@ namespace TMDbLib.Client
throw new UserSessionRequiredException();
}
public void GetConfig()
public async Task<TMDbConfig> GetConfigAsync()
{
TMDbConfig config = _client.Create("configuration").ExecuteGet<TMDbConfig>(CancellationToken.None).Result;
TMDbConfig config = await _client.Create("configuration").ExecuteGet<TMDbConfig>(CancellationToken.None);
if (config == null)
throw new Exception("Unable to retrieve configuration");
@ -175,6 +176,8 @@ namespace TMDbLib.Client
// Store config
Config = config;
HasConfig = true;
return config;
}
public Uri GetImageUrl(string size, string filePath, bool useSsl = false)

View File

@ -84,7 +84,7 @@ namespace TMDbLibTests
public void TestCollectionsImages()
{
// Get config
Config.Client.GetConfig();
Config.Client.GetConfigAsync().Sync();
// Test image url generator
ImagesWithId images = Config.Client.GetCollectionImagesAsync(IdHelper.JamesBondCollection).Result;

View File

@ -100,7 +100,7 @@ namespace TMDbLibTests
IgnoreMissingJson(" / movies");
// Get config
Config.Client.GetConfig();
Config.Client.GetConfigAsync().Sync();
// Test image url generator
Company company = Config.Client.GetCompanyAsync(IdHelper.TwentiethCenturyFox).Result;

View File

@ -426,7 +426,7 @@ namespace TMDbLibTests
public void TestMoviesImages()
{
// Get config
Config.Client.GetConfig();
Config.Client.GetConfigAsync().Sync();
// Test image url generator
ImagesWithId images = Config.Client.GetMovieImagesAsync(IdHelper.AGoodDayToDieHard).Result;

View File

@ -270,7 +270,7 @@ namespace TMDbLibTests
public void TestPersonsImages()
{
// Get config
Config.Client.GetConfig();
Config.Client.GetConfigAsync().Sync();
// Get images
ProfileImages images = Config.Client.GetPersonImagesAsync(IdHelper.BruceWillis).Result;
@ -298,7 +298,7 @@ namespace TMDbLibTests
public void TestPersonsTaggedImages()
{
// Get config
Config.Client.GetConfig();
Config.Client.GetConfigAsync().Sync();
// Get images
TestHelpers.SearchPages(i => Config.Client.GetPersonTaggedImagesAsync(IdHelper.BruceWillis, i).Result);

View File

@ -16,7 +16,7 @@ namespace TMDbLibTests
public void GetConfigTest()
{
Assert.False(Config.Client.HasConfig);
Config.Client.GetConfig();
Config.Client.GetConfigAsync().Sync();
Assert.True(Config.Client.HasConfig);
Assert.NotNull(Config.Client.Config);
@ -28,7 +28,7 @@ namespace TMDbLibTests
TestConfig config = new TestConfig(true);
Assert.False(config.Client.HasConfig);
config.Client.GetConfig();
config.Client.GetConfigAsync().Sync();
Assert.True(config.Client.HasConfig);
Assert.NotNull(config.Client.Config);
@ -60,16 +60,16 @@ namespace TMDbLibTests
public void ClientConstructorUrlTest()
{
TMDbClient clientA = new TMDbClient(TestConfig.APIKey, false, "http://api.themoviedb.org") { MaxRetryCount = 2 };
clientA.GetConfig();
clientA.GetConfigAsync().Sync();
TMDbClient clientB = new TMDbClient(TestConfig.APIKey, true, "http://api.themoviedb.org") { MaxRetryCount = 2 };
clientB.GetConfig();
clientB.GetConfigAsync().Sync();
TMDbClient clientC = new TMDbClient(TestConfig.APIKey, false, "https://api.themoviedb.org") { MaxRetryCount = 2 };
clientC.GetConfig();
clientC.GetConfigAsync().Sync();
TMDbClient clientD = new TMDbClient(TestConfig.APIKey, true, "https://api.themoviedb.org") { MaxRetryCount = 2 };
clientD.GetConfig();
clientD.GetConfigAsync().Sync();
}
[Fact]

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using TMDbLib.Client;
using TMDbLib.Objects.General;
@ -13,7 +14,7 @@ namespace TestApplication
{
public class Program
{
private static void Main(string[] args)
private static async Task Main(string[] args)
{
// Instantiate a new client, all that's needed is an API key, but it's possible to
// also specify if SSL should be used, and if another server address should be used.
@ -21,21 +22,21 @@ namespace TestApplication
// We need the config from TMDb in case we want to get stuff like images
// The config needs to be fetched for each new client we create, but we can cache it to a file (as in this example).
FetchConfig(client);
await FetchConfig(client);
// Try fetching a movie
FetchMovieExample(client);
await FetchMovieExample(client);
// Once we've got a movie, or person, or so on, we can display images.
// TMDb follow the pattern shown in the following example
// This example also shows an important feature of most of the Get-methods.
FetchImagesExample(client);
await FetchImagesExample(client);
Console.WriteLine("Done.");
Console.ReadLine();
}
private static void FetchConfig(TMDbClient client)
private static async Task FetchConfig(TMDbClient client)
{
FileInfo configJson = new FileInfo("config.json");
@ -45,23 +46,23 @@ namespace TestApplication
{
Console.WriteLine("Using stored config");
string json = File.ReadAllText(configJson.FullName, Encoding.UTF8);
client.SetConfig(JsonConvert.DeserializeObject<TMDbConfig>(json));
}
else
{
Console.WriteLine("Getting new config");
client.GetConfig();
var config = await client.GetConfigAsync();
Console.WriteLine("Storing config");
string json = JsonConvert.SerializeObject(client.Config);
string json = JsonConvert.SerializeObject(config);
File.WriteAllText(configJson.FullName, json, Encoding.UTF8);
}
Spacer();
}
private static void FetchImagesExample(TMDbClient client)
private static async Task FetchImagesExample(TMDbClient client)
{
const int movieId = 76338; // Thor: The Dark World (2013)
@ -77,7 +78,7 @@ namespace TestApplication
// Note: Each method normally corresponds to a property on the resulting object. If you haven't requested the information, the property will most likely be null.
// Also note, that while we could have used 'client.GetMovieImagesAsync()' - it was better to do it like this because we also wanted the Title of the movie.
Movie movie = client.GetMovieAsync(movieId, MovieMethods.Images).Result;
Movie movie = await client.GetMovieAsync(movieId, MovieMethods.Images);
Console.WriteLine("Fetching images for '" + movie.Title + "'");
@ -116,13 +117,13 @@ namespace TestApplication
}
}
private static void FetchMovieExample(TMDbClient client)
private static async Task FetchMovieExample(TMDbClient client)
{
string query = "Thor";
// This example shows the fetching of a movie.
// Say the user searches for "Thor" in order to find "Thor: The Dark World" or "Thor"
SearchContainer<SearchMovie> results = client.SearchMovieAsync(query).Result;
SearchContainer<SearchMovie> results = await client.SearchMovieAsync(query);
// The results is a list, currently on page 1 because we didn't specify any page.
Console.WriteLine("Searched for movies: '" + query + "', found " + results.TotalResults + " results in " +

View File

@ -10,6 +10,7 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>