/// <summary> /// Gets the next card enqueued to be printed /// </summary> /// <param name="printerId">The ID of the printer who is requesting the card</param> /// <returns></returns> public MabelCard GetNextJob(int printerId) { MabelRequest request = new MabelRequest(this, "cardHandler.getNextPrintJob", new MabelGetNextJobParams(printerId)); MabelResponse response = MakeRequest(request); if (response.code == 200) { if (response.results == null) { // The API is OK, there's just no cards to print :) return(null); } } else { //Something has gone wrong, best ignore and hope for the best, but exit out this time around // throw an exception probably? throw new Exception("Problem with request to API: " + response.message); } // otherwise... MabelCard card = new MabelCard(); card.card_id = (int)response.results.SelectToken("cardId"); card.member_id = response.results.SelectToken("memberId").ToString(); card.card_front_image_encoded = (string)response.results.SelectToken("imageFront"); card.card_back_image_encoded = (string)response.results.SelectToken("imageBack"); return(card); }
/// <summary> /// Gets the next card enqueued to be printed /// </summary> /// <param name="printerId">The ID of the printer who is requesting the card</param> /// <returns></returns> public MabelCard GetNextJob(int printerId) { MabelRequest request = new MabelRequest(this, "cardHandler.getNextPrintJob", new MabelGetNextJobParams(printerId)); MabelResponse response = MakeRequest(request); if (response.code == 200) { if (response.results == null) { // The API is OK, there's just no cards to print :) return null; } } else { //Something has gone wrong, best ignore and hope for the best, but exit out this time around // throw an exception probably? throw new Exception("Problem with request to API: " + response.message); } // otherwise... MabelCard card = new MabelCard(); card.card_id = (int) response.results.SelectToken("cardId"); card.member_id = response.results.SelectToken("memberId").ToString(); card.card_front_image_encoded = (string)response.results.SelectToken("imageFront"); card.card_back_image_encoded = (string)response.results.SelectToken("imageBack"); return card; }
/// <summary> /// Makess a request to the Mabel API, using the specified request object /// </summary> /// <param name="mabelRequest">A build MabelRequest object with the desired params for the function call.</param> /// <returns></returns> private MabelResponse MakeRequest(MabelRequest mabelRequest) { ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true; String url = mabelRequest.buildURL(); WebRequest request = WebRequest.Create(url); request.Timeout = 3000; //request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; //request.Credentials = new NetworkCredential(Properties.Settings.Default.Username, Properties.Settings.Default.Password, Properties.Settings.Default.Domain); //request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; // Get the response. lastRequest = mabelRequest; MabelResponse mabelResponse = new MabelResponse(); HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); lastResponse = mabelResponse; } catch (Exception ex) { mabelResponse.isError = true; mabelResponse.message = "HTTP EXCEPTION:" + ex.Message; // this may be overridden later... ? } if (response != null) { Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. // Clean up the streams and the response. reader.Close(); response.Close(); mabelResponse._raw = responseFromServer; } else { // if there's no content, then the HTTP exception is as good as we're going to get OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return(mabelResponse); } // Get the stream containing content returned by the server. JObject o; try { o = JObject.Parse(mabelResponse._raw); } catch (Exception ex) { mabelResponse.isError = true; mabelResponse.message = "JSON Exception: " + ex.Message; OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return(mabelResponse); } try { mabelResponse.code = (int)o["meta"]["status"]; mabelResponse.message = (string)o["meta"]["msg"]; mabelResponse.count = (int)o["meta"]["count"]; } catch (Exception ex2) { mabelResponse.isError = true; mabelResponse.message = "JSON Exception: meta fields: " + ex2.Message; OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return(mabelResponse); } if (mabelResponse.code != 200) { // it's an error, return it now, don't bother looking for results mabelResponse.isError = true; OnError(new MabelEventArgs(url, mabelRequest, mabelResponse)); } else { mabelResponse.isError = false; } // otherwise, hand back the results :) if (o["result"].Type != JTokenType.Null) { mabelResponse.results = o["result"]; } else { mabelResponse.results = null; } OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return(mabelResponse); }
public MabelEventArgs(String URL, MabelRequest request, MabelResponse response) { this.URL = URL; this.request = request; this.response = response; }
public MabelEventArgs(String URL, MabelRequest request, MabelResponse response) { this.URL = URL; this.request = request; this.response = response; }
/// <summary> /// Makess a request to the Mabel API, using the specified request object /// </summary> /// <param name="mabelRequest">A build MabelRequest object with the desired params for the function call.</param> /// <returns></returns> private MabelResponse MakeRequest( MabelRequest mabelRequest) { ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true; String url = mabelRequest.buildURL(); WebRequest request = WebRequest.Create(url); request.Timeout = 3000; //request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; //request.Credentials = new NetworkCredential(Properties.Settings.Default.Username, Properties.Settings.Default.Password, Properties.Settings.Default.Domain); //request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; // Get the response. lastRequest = mabelRequest; MabelResponse mabelResponse = new MabelResponse(); HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); lastResponse = mabelResponse; } catch (Exception ex) { mabelResponse.isError = true; mabelResponse.message = "HTTP EXCEPTION:" + ex.Message; // this may be overridden later... ? } if (response != null) { Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. // Clean up the streams and the response. reader.Close(); response.Close(); mabelResponse._raw = responseFromServer; } else { // if there's no content, then the HTTP exception is as good as we're going to get OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return mabelResponse; } // Get the stream containing content returned by the server. JObject o; try { o = JObject.Parse(mabelResponse._raw); } catch (Exception ex) { mabelResponse.isError = true; mabelResponse.message = "JSON Exception: " + ex.Message; OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return mabelResponse; } try { mabelResponse.code = (int)o["meta"]["status"]; mabelResponse.message = (string)o["meta"]["msg"]; mabelResponse.count = (int)o["meta"]["count"]; } catch (Exception ex2) { mabelResponse.isError = true; mabelResponse.message = "JSON Exception: meta fields: " + ex2.Message; OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return mabelResponse; } if (mabelResponse.code != 200) { // it's an error, return it now, don't bother looking for results mabelResponse.isError = true; } else { mabelResponse.isError = false; } // otherwise, hand back the results :) if (o["result"].Type != JTokenType.Null) { mabelResponse.results = o["result"]; } else { mabelResponse.results = null; } OnDebug(new MabelEventArgs(url, mabelRequest, mabelResponse)); return mabelResponse; }