-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathValuesController.cs
61 lines (55 loc) · 2.36 KB
/
ValuesController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Reactive.Linq;
using System.Net.Http;
using System.Reactive.Threading.Tasks;
using System.Reactive.Concurrency;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
namespace ReactiveDotNetCoreApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ILogger<ValuesController> _logger;
static readonly HttpClient _client = new HttpClient();
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
}
// GET api/values
[HttpGet]
public Task<IActionResult> Get()
{
return Observable
.FromAsync(() => _client.GetAsync("https://jsonplaceholder.typicode.com/todos"))
.SubscribeOn(TaskPoolScheduler.Default)
.Retry(5)
.Timeout(TimeSpan.FromMilliseconds(80))
.Do(x => _logger?.LogInformation($"Message Successful? :{x.IsSuccessStatusCode}"))
.SelectMany(
async x =>
{
JArray parsedTodos = null;
using(var stream = await x.Content.ReadAsStreamAsync().ConfigureAwait(false))
using(var sr = new StreamReader(stream))
using(var jtr = new JsonTextReader(sr))
{
parsedTodos = await JArray.LoadAsync(jtr).ConfigureAwait(false);
}
return parsedTodos.Select(pt => pt.SelectToken("title")).ToList();
})
.Select(x => new JsonResult(x))
.Catch<IActionResult, TimeoutException>(ex => Observable.Return(StatusCode(StatusCodes.Status503ServiceUnavailable)))
.Catch<IActionResult, Exception>(ex => Observable.Return(StatusCode(StatusCodes.Status500InternalServerError)))
.ToTask();
}
}
}