void FetchJSON(HttpsRequest request, Action<JsonValue> completion, Action<Exception> errorHandler, CancellationToken token) { var op = System.ComponentModel.AsyncOperationManager.CreateOperation(null); request.UserAgent = "FlashcardsWP7/0.1 (" + Environment.OSVersion + "; .NET " + Environment.Version + ")"; try { token.Register(delegate { throw new OperationCanceledException(); }); ThreadPool.QueueUserWorkItem( _ => { Exception error; try { var client = new HttpsClient(Host.Host, Host.Port, Configuration.VerifyCertificates); var response = client.MakeRequest(request); var text = response.DecodeTextBody(); if (response.StatusCode == 204) { op.PostOperationCompleted(delegate { completion(null); }, null); return; } var json = JsonValue.Parse(new StringReader(text)); if (response.StatusCode / 100 != 2) { var dict = (JsonDictionary) json; var errorCode = new JsonContext().FromJson<string>(dict.Items["error"]); var errorText = new JsonContext().FromJson<string>(dict.Items["error_description"]); if (errorCode != null && errorText != null) { // TODO: find error code for needing password if (errorCode == "invalid_access") throw new AccessDeniedException(errorText); if (errorCode == "item_not_found") throw new ItemNotFoundException(errorText); if (errorCode == "item_deleted") throw new ItemDeletedException(errorText); throw new QuizletException(errorText); } throw new QuizletException("The Quizlet request failed (HTTP error " + response.StatusCode.ToString(CultureInfo.InvariantCulture) + ")."); } op.PostOperationCompleted(delegate { completion(json); }, null); return; } catch (QuizletException e) { error = e; } catch (OperationCanceledException e) { error = e; } catch (HttpException e) { error = e; } catch (System.Net.Sockets.SocketException e) { error = e; } catch (IOException e) { error = e; } catch (ArgumentException e) { error = e; } catch (InvalidOperationException e) { error = e; } catch (NotSupportedException e) { error = e; } catch (FormatException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } catch (JsonConvertException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } catch (InvalidCastException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } catch (KeyNotFoundException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } op.PostOperationCompleted(delegate { errorHandler(error); }, null); }); } catch (OperationCanceledException e) { errorHandler(e); } }
void FetchJSON(HttpsRequest request, Action <JsonValue> completion, Action <Exception> errorHandler, CancellationToken token) { var op = System.ComponentModel.AsyncOperationManager.CreateOperation(null); request.UserAgent = "FlashcardsWP7/0.1 (" + Environment.OSVersion + "; .NET " + Environment.Version + ")"; try { token.Register(delegate { throw new OperationCanceledException(); }); ThreadPool.QueueUserWorkItem( _ => { Exception error; try { var client = new HttpsClient(Host.Host, Host.Port, Configuration.VerifyCertificates); var response = client.MakeRequest(request); var text = response.DecodeTextBody(); if (response.StatusCode == 204) { op.PostOperationCompleted(delegate { completion(null); }, null); return; } var json = JsonValue.Parse(new StringReader(text)); if (response.StatusCode / 100 != 2) { var dict = (JsonDictionary)json; var errorCode = new JsonContext().FromJson <string>(dict.Items["error"]); var errorText = new JsonContext().FromJson <string>(dict.Items["error_description"]); if (errorCode != null && errorText != null) { // TODO: find error code for needing password if (errorCode == "invalid_access") { throw new AccessDeniedException(errorText); } if (errorCode == "item_not_found") { throw new ItemNotFoundException(errorText); } if (errorCode == "item_deleted") { throw new ItemDeletedException(errorText); } throw new QuizletException(errorText); } throw new QuizletException("The Quizlet request failed (HTTP error " + response.StatusCode.ToString(CultureInfo.InvariantCulture) + ")."); } op.PostOperationCompleted(delegate { completion(json); }, null); return; } catch (QuizletException e) { error = e; } catch (OperationCanceledException e) { error = e; } catch (HttpException e) { error = e; } catch (System.Net.Sockets.SocketException e) { error = e; } catch (IOException e) { error = e; } catch (ArgumentException e) { error = e; } catch (InvalidOperationException e) { error = e; } catch (NotSupportedException e) { error = e; } catch (FormatException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } catch (JsonConvertException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } catch (InvalidCastException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } catch (KeyNotFoundException e) { error = new QuizletException("The Quizlet server returned an invalid document.", e); } op.PostOperationCompleted(delegate { errorHandler(error); }, null); }); } catch (OperationCanceledException e) { errorHandler(e); } }