// Request completed public void RequestFinished(GlitchRequest request) { Console.WriteLine("Request Finished!"); // Null checks and type check if (request != null && request.response != null && request.response.GetType().Equals(typeof(Dictionary<String, Object>))) { // Cast to dictionary Dictionary<String, Object> response = request.response as Dictionary<String, Object>; // Declare playerName to be set later object playerName; // Try to get player_name from the dictionary if (response.TryGetValue("player_name", out playerName)) { // Check if it is a string before writing it to the console if (playerName.GetType().Equals(typeof(String))) { Console.WriteLine("Hello " + playerName as String); } } } resetEvent.Set(); // Allow the program to exit }
// Request failed with associated exception public void RequestFailed(GlitchRequest request, Exception exception) { resetEvent.Set(); // Allow the program to exit if (exception != null) { throw exception; // Throw exception, handle this how you want } }
// Call this to execute your request // Pass in the delegate which will be called when events occur that are related to this object public void Execute(IGlitchRequester glitchRequester) { // Set local IGlitchRequester for callbacks requester = glitchRequester; // Concatenate the full url String fullUrl = API_URL + method; // If we don't have parameters, create the dictionary if (parameters == null) { parameters = new Dictionary <String, String>(); } // Add access token to dictionary if (glitch.accessToken != null) { parameters.Add("oauth_token", glitch.accessToken); } // Serialize our full url with our parameters fullUrl = SerializeURL(fullUrl, parameters); // Create the request and set its method request = HttpWebRequest.Create(fullUrl) as HttpWebRequest; request.Method = WebRequestMethods.Http.Get; // Begin the response with the associated callback request.BeginGetResponse(r => { // Callback // r is the IAsyncResult // Get the associated Glitch Request object GlitchRequest glitchRequest = r.AsyncState as GlitchRequest; // Attempt to parse the response to check for ok try { // Get the associated response object HttpWebResponse httpWebResponse = request.EndGetResponse(r) as HttpWebResponse; // Read the response into a string StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream()); String result = reader.ReadToEnd(); reader.Dispose(); // Serialize the response string into an object JavaScriptSerializer serializer = new JavaScriptSerializer(); object response = serializer.DeserializeObject(result); // Check the type before checking for ok if (response.GetType().Equals(typeof(Dictionary <String, Object>))) { // Cast to a dictionary Dictionary <String, Object> responseDict = response as Dictionary <String, Object>; // Declare ok object object ok; // Attempt to get the ok value if (responseDict.TryGetValue("ok", out ok)) { // If ok is an int if (ok.GetType().Equals(typeof(int))) { // Check if it is 1, which means the request was ok if ((int)ok == 1) { // Set the response for the request glitchRequest.response = response; // Call the requester with the request requester.RequestFinished(glitchRequest); return; } } // If the service did not return ok or did not return ok = 1 requester.RequestFailed(glitchRequest, new Exception("Service did not return OK")); } } } catch (Exception ex) { // If there was an exception somewhere in the parsing process, send it back to the requester requester.RequestFailed(glitchRequest, ex); } // If we get to this point, we didn't encounter an exception but we didn't get a valid parsed response requester.RequestFailed(glitchRequest, new Exception("An unknown error occurred with the request!")); }, this); }