Skip to content

Instantly share code, notes, and snippets.

@n3rd
Last active December 30, 2015 17:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save n3rd/7861565 to your computer and use it in GitHub Desktop.
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