/// <summary> /// Creates a call back with the API call args. /// </summary> /// <param name="e">The API call event</param> protected virtual void OnAPICall(APICallEventArgs e) { if (APICall != null) { // Invokes the delegates. APICall(this, e); } }
/// <summary> /// Manage API call event. /// </summary> private void OnAPICall(object sender, APICallEventArgs e) { DebugTextBox.Text = ""; AppendText(DebugTextBox, "API Call:\n", Color.Brown, true); AppendText(DebugTextBox, "Url: ", Color.DarkGreen, true); AppendText(DebugTextBox, e.Url + "\n", Color.Blue, false); AppendText(DebugTextBox, "Response: ", Color.DarkOrange, true); AppendText(DebugTextBox, e.Response + "\n", Color.Blue, false); }
/// <summary> /// Creates a call back with the API call args. /// </summary> /// <param name="e">The API call event</param> protected void OnAPICall(object sender, APICallEventArgs e) { // Fire this event to the next layer if (APICall != null) { // Invokes the delegates. APICall(this, e); } }
/// <summary> /// Make an HTTP request, with the given query args /// </summary> private string MakeRequest(Uri url, HttpVerb httpVerb, List <KeyValuePair <string, string> > args, JSONObject body, ContentType contentType = ContentType.JSON) { // Prepare HTTP url url = PrepareUrl(url, AccessToken, args); // Set request HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = httpVerb.ToString(); if ((httpVerb == HttpVerb.POST) || (httpVerb == HttpVerb.PUT)) { // Prepare HTTP body string postData = EncodeBody(body, contentType); ASCIIEncoding encoding = new ASCIIEncoding(); byte[] postDataBytes = encoding.GetBytes(postData); // Set content type & length if (contentType == ContentType.JSON) { request.ContentType = "application/json"; } else { request.ContentType = "application/x-www-form-urlencoded"; } request.ContentLength = postDataBytes.Length; // Call API Stream requestStream = request.GetRequestStream(); requestStream.Write(postDataBytes, 0, postDataBytes.Length); requestStream.Close(); } // Resolve the API response try { using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Read response data StreamReader reader = new StreamReader(response.GetResponseStream()); string responseBody = reader.ReadToEnd(); // Throw the API call event APICallEventArgs apiCallEvent = new APICallEventArgs(); if (body != null) { apiCallEvent.Body = body.ToString(); } apiCallEvent.Response = responseBody; apiCallEvent.Url = url.ToString(); OnAPICall(apiCallEvent); // Return API response body return(responseBody); } } catch (WebException e) { JSONObject response = null; try { // Try throwing a well-formed api error Stream stream = e.Response.GetResponseStream(); StreamReader reader = new StreamReader(stream); response = JSONObject.CreateFromString(reader.ReadToEnd().Trim()); int status = Convert.ToInt16(response.GetJSONStringAttribute("status")); string error = response.GetJSONStringAttribute("error"); string message = response.GetJSONStringAttribute("message"); // optional: cause string cause = ""; try { cause = response.Dictionary["cause"].Dictionary["message"].String; } catch { } throw new RESTAPIException(status, error, message, cause); } catch (RESTAPIException restEx) { throw restEx; // this is a well-formed error message } catch { throw new RESTAPIException(999, e.Status.ToString(), e.Message); // this is not a well-formed message } } }