Created
November 16, 2010 17:13
-
-
Save anaisbetts/702092 to your computer and use it in GitHub Desktop.
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
IObservable<KeyValuePair<string, bool>> ValidateUriAsync(string Uri) | |
{ | |
var request = WebRequest.Create(Uri); | |
// This gives us a "GetResponseAsync" function whose prototype is | |
// IObservable[WebResponse] GetResponseAsync() - just like the sync | |
// version, but the return value wrapped in IObservable. | |
var response_fetcher = Observable.FromAsyncPattern<WebResponse>( | |
request.BeginGetResponse, request.EndGetResponse); | |
// The clever part here is the Catch - if the response_fetcher IObservable | |
// ends with OnError, we'll instead act as if it didn't end and splice in | |
// a "completed successfully" IObservable who returns null and ends | |
return response_fetcher() | |
.Catch(Observable.Return<WebResponse>(null)) | |
.Select(resp => new KeyValuePair<string, bool>(Uri, resp != null && resp.StatusCode == HttpStatusCode.OK)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment