public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddOptions(); services.AddJob <StatsLoaderJob>(); services.AddConfiguration <JobConfiguration>("JobConfiguration"); services.AddConfiguration <WorkerConfiguration>("WorkerConfiguration"); services.AddConfiguration <MongoDbConfiguration>("DataConfiguration"); services.AddSingleton <IETLService, ETLService>(); services.AddSingleton <IPUBGApiCaller, PUBGApiCaller>(); services.AddSingleton <IReadOnlyMongoDbService, ReadOnlyMongoDbService>(); services.AddSingleton <IWriteOnlyMongoDbService, WriteOnlyMongoDbService>(); services.AddSingleton <IDocumentExtractorService, DocumentExtractorService>(); services.AddSingleton((collection) => { HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", collection.GetService <IOptions <WorkerConfiguration> >().Value.PUBGApiKey); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/vnd.api+json")); return(httpClient); }); CallGovernor.Init(); }
public async Task <PlayerLifetimeStats> GetPlayerLifetimeStatsByNameAsync(string name, CancellationToken cancellationToken) { try { string playerId = await GetPlayerIdAsync(name, cancellationToken); HttpResponseMessage apiResponse = await CallGovernor.Call(async() => { return(await _httpClient.GetAsync( $"https://api.pubg.com/shards/steam/players/{playerId}/seasons/lifetime", cancellationToken)); }, cancellationToken); string raw = await apiResponse.Content.ReadAsStringAsync(); PlayerLifetimeStats playerLifetimeStats = _documentExtractorService.GetPlayerLifetimeStatsDocumentFromRaw(raw, playerId, name); return(playerLifetimeStats); } catch (Exception ex) { Log.Error("Error getting data from API", ex); throw; } }
public async Task Call_WhenTooManyRequests_ThenWait() { Stopwatch sw = new Stopwatch(); //Arrange Mock <HttpMessageHandler> handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() .SetupSequence <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.TooManyRequests, Headers = { { RemainingCallHeader, new[] { "0" } }, { ResetLimitHeader, new[] { GetUnixFromDateTime(DateTime.UtcNow.AddSeconds(10)).ToString() } } } }).ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(string.Empty) }); var httpClient = new HttpClient(handlerMock.Object); Func <Task <HttpResponseMessage> > mockCall = async() => { HttpResponseMessage apiResponse = await httpClient.GetAsync("https://fluendo.com/en/"); return(apiResponse); }; //Act sw.Start(); HttpResponseMessage testResponse = await CallGovernor.Call(mockCall, CancellationToken.None); sw.Stop(); //Assert sw.Elapsed.Seconds.Should().BeGreaterThan(5).And.BeLessThan(15); testResponse.StatusCode.Should().Be(HttpStatusCode.OK); }
private async Task <string> GetPlayerIdAsync(string name, CancellationToken cancellationToken) { try { HttpResponseMessage apiResponse = await CallGovernor.Call(async() => { return(await _httpClient.GetAsync($"https://api.pubg.com/shards/steam/players?filter[playerNames]={name}", cancellationToken)); }, cancellationToken); string raw = await apiResponse.Content.ReadAsStringAsync(); JObject jResponse = JsonConvert.DeserializeObject <JObject>(raw); JToken data = jResponse[DataJsonPath].ToObject <JArray>().First(); string id = data[IdJsonPath].Value <string>(); return(id); } catch (Exception ex) { Log.Error("Error getting data from API", ex); throw; } }
public async Task <Leaderboard> GetLeaderBoardAsync(string gameMode, CancellationToken cancellationToken) { try { HttpResponseMessage apiResponse = await CallGovernor.Call(async() => { return(await _httpClient.GetAsync( $"https://api.pubg.com/shards/steam/leaderboards/{gameMode}?page[number]=1", cancellationToken)); }, cancellationToken); string raw = await apiResponse.Content.ReadAsStringAsync(); Leaderboard leaderboard = _documentExtractorService.GetLeaderboardDocumentFromRaw(raw, gameMode); return(leaderboard); } catch (Exception ex) { Log.Error("Error getting data from API", ex); throw; } }
public CallGovernorTests() { CallGovernor.Init(); }