This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Reactive.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Rx_Test | |
{ | |
class Gist | |
{ | |
public string url { get; set; } | |
public string description { get; set; } | |
} | |
class Program | |
{ | |
public Program() | |
{ } | |
static void Main(string[] args) | |
{ | |
var p = new Program(); | |
p.Run(); | |
Console.ReadKey(); | |
} | |
private void Run() | |
{ | |
IObservable<Gist> response = Get<Gist>(new string[] { "https://api.github.com/gists/7729259", "https://api.github.com/gists/7204241" }); | |
response.Subscribe(g => Console.WriteLine(g.description), | |
ex => Console.WriteLine("OnError: {0}", ex.Message), | |
() => Console.WriteLine("OnCompleted")); | |
} | |
private static async Task<HttpResponseMessage> GetRequestAsync(string url) | |
{ | |
var httpClient = new HttpClient(); | |
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("MyAgent/1.0"); | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url); | |
var response = await httpClient.SendAsync(request); | |
return response; | |
} | |
private IObservable<T> Get<T>(IEnumerable<string> urls) | |
{ | |
return Observable.Create<T>( | |
async obs => | |
{ | |
foreach(var url in urls) | |
{ | |
var response = await GetRequestAsync(url); | |
obs.OnNext(JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync())); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment