/// <summary> /// Método que chequea si el resultado de la llamada asíncrona a la API falló /// </summary> /// <param name="asyncResult">Resultado de la llamada asícrona</param> /// <returns>Una intacia de la clase ErrorInfo con información del error. Null en caso de que no haya fallado</returns> private ErrorInfo CheckForError(string result) { // check for error in api response, throw Exception if positive ErrorInfo errorInfo = null; // parse response and fill errorInfo... //response = "{\"opstat\":\"error\",\"err\":{\"code\":1150,\"msg\":\"'api_key' is a required field\"}}"; object resultJson = JsonConvert.DeserializeObject(result); string responseType = (((Newtonsoft.Json.Linq.JObject)resultJson).Property("opstat")).Value.ToString(); responseType = responseType.Replace("\"", ""); if (responseType.Equals("error")) { JObject errObject = (JObject)(((Newtonsoft.Json.Linq.JObject)resultJson).Property("err")).Value; string errorMessage = errObject.Property("msg").Value.ToString(); string errorCode = errObject.Property("code").Value.ToString(); errorMessage = errorMessage.Replace("\"", ""); errorInfo = new ErrorInfo(ErrorType.ApiError, errorMessage, errorCode); } return errorInfo; }
/// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void ViewJob(ViewJobCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback, string pJob_Id, string pPre_mt) { try { // set callback handlers viewJobSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create async caller object ViewJobMethodCaller caller = null; if (!_modeOffline) caller = new ViewJobMethodCaller(myGengoClient.myGengoClient.TranslateJobsGetByIdRaw); else caller = new ViewJobMethodCaller(ViewJobOffLineData); bool pHTMLEncode = false; IAsyncResult result = caller.BeginInvoke(pJob_Id, pPre_mt, pHTMLEncode, new AsyncCallback(ViewJobCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
// async callback method /// <summary> /// /// </summary> /// <param name="ar"></param> private void CancelJobCallBack(IAsyncResult ar) { try { // get result CancelJobMethodCaller caller = (CancelJobMethodCaller)ar.AsyncState; // look for error in response string returnValue = caller.EndInvoke(ar); ErrorInfo error = CheckForError(returnValue); if (error != null) { methodErrorCallback.Invoke(error); return; } // map result to object bool objectResult = (bool)ConvertResultToObject(returnValue, ApiMethod.CancelJob); // call success callback method cancelJobSuccessCallback.Invoke(objectResult); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
/// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void ReviewJob(ReviewJobCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback, string pJob_Id) { try { // set callback handlers reviewJobSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create async caller object ReviewJobMethodCaller caller = null; if (!_modeOffline) caller = new ReviewJobMethodCaller(myGengoClient.myGengoClient.GetJobPreview); else caller = new ReviewJobMethodCaller(ReviewJobOffLineData); //string specificFolder = string.Format(Properties.Resources.AppDirectory, _specificFolderName); //string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + specificFolder + "Previews"; //if (!System.IO.Directory.Exists(folder)) // System.IO.Directory.CreateDirectory(folder); string folder = GetFolderPathPreviewImage(); string impagePath = string.Format("{0}/preview{1}.jpg", folder, pJob_Id); string imagePathPrefix = ""; IAsyncResult result = caller.BeginInvoke(pJob_Id, impagePath, imagePathPrefix, new AsyncCallback(ReviewJobCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
/// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void TranslateJob(TranslateJobCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback, string pType, string pSlug, string pBodySrc, string pLc_src, string pLc_tgt, string pTier, string pAutoApprove, string pCustomData, string pComment) { try { // set callback handlers translateJobSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create async caller object TranslateJobMethodCaller caller = null; if (!_modeOffline) caller = new TranslateJobMethodCaller(myGengoClient.myGengoClient.Translate_JobsRaw); else caller = new TranslateJobMethodCaller(TranslateJobOffLineData); IAsyncResult result = caller.BeginInvoke(pType, pSlug, pBodySrc, pLc_src, pLc_tgt, pTier, pAutoApprove, pCustomData, pComment, new AsyncCallback(TranslateJobCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
/// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void GetMyJobs(GetMyJobsCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback, string pStatus, string pTimespan, string pCount, bool pLazyLoad) { try { // set callback handlers getMyJobsSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create async caller object GetMyJobsMethodCaller caller = null; if (!_modeOffline) caller = new GetMyJobsMethodCaller(myGengoClient.myGengoClient.GetMyJobs); else caller = new GetMyJobsMethodCaller(GetMyJobsOffLineData); string jobCount = ConfigurationManager.AppSettings["wGengo_jobCount"]; IAsyncResult result = caller.BeginInvoke(pStatus, pTimespan, jobCount, pLazyLoad, new AsyncCallback(GetMyJobsCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
/// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void RejectJob(RejectJobCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback, string pJob_Id, string pReason, string pComment, string pCaptcha, string pFollowUp) { try { // set callback handlers rejectJobSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create async caller object RejectJobMethodCaller caller = null; if (!_modeOffline) caller = new RejectJobMethodCaller(myGengoClient.myGengoClient.RejectJob); else caller = new RejectJobMethodCaller(RejectJobOffLineData); IAsyncResult result = caller.BeginInvoke(pJob_Id, pReason, pComment, pCaptcha, pFollowUp, new AsyncCallback(RejectJobCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
/// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void GetLanguages(GetLanguagesCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback) { try { // set callback handlers getLanguagesSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create async caller object GetLanguagesMethodCaller caller = null; if (!_modeOffline) caller = new GetLanguagesMethodCaller(myGengoClient.myGengoClient.Translate_Service_LanguageRaw); else caller = new GetLanguagesMethodCaller(GetLanguagesOffLineData); string jobCount = ConfigurationManager.AppSettings["wGengo_jobCount"]; IAsyncResult result = caller.BeginInvoke(new AsyncCallback(GetLanguagesCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
//public delegate string AsyncMethodCaller(string jobId); /// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void GetAccountBalance(GetAccountBalanceCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback) { try { // set callback handlers getAccountBalanceSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create ayns caller object GetAccountBalanceMethodCaller caller = null; if (!_modeOffline) caller = new GetAccountBalanceMethodCaller(myGengoClient.myGengoClient.Account_BalanceRaw); else caller = new GetAccountBalanceMethodCaller(TestMethodOffLineData); // call async IAsyncResult result = caller.BeginInvoke(new AsyncCallback(GetAccountBalanceCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
/// <summary> /// Method that is called by wrapper client code used to make de async call to the API /// </summary> /// <param name="pSuccessCallback">Reference to a method that will be invoked when the async API callBack finish successfully</param> /// <param name="pErrorCallback">Reference to a method that will be invoked when the async API callBack retunrs an error</param> public void CorrectJob(CorrectJobCallBackDelegate pSuccessCallback, ErrorCallBackDelegate pErrorCallback, string pJob_Id, string pRequestCorrection) { try { // set callback handlers correctJobSuccessCallback = pSuccessCallback; methodErrorCallback = pErrorCallback; // create async caller object CorrectJobMethodCaller caller = null; if (!_modeOffline) caller = new CorrectJobMethodCaller(myGengoClient.myGengoClient.RequestCorrection); else caller = new CorrectJobMethodCaller(CorrectJobOffLineData); IAsyncResult result = caller.BeginInvoke(pJob_Id, pRequestCorrection, new AsyncCallback(CorrectJobCallBack), caller); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
// async callback method /// <summary> /// /// </summary> /// <param name="ar"></param> private void ReviewJobCallBack(IAsyncResult ar) { try { // get result ReviewJobMethodCaller caller = (ReviewJobMethodCaller)ar.AsyncState; // look for error in response string returnValue = caller.EndInvoke(ar); ErrorInfo error = CheckForError(returnValue); if (error != null) { methodErrorCallback.Invoke(error); return; } // map result to object Job objectResult = (Job)ConvertResultToObject(returnValue, ApiMethod.ReviewJob); string imagePath; string previewfolder; string captchaPath; string captchaFolder; if (!_modeOffline) { //string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Properties.Resources.AppDirectory + "Previews"; //if (!System.IO.Directory.Exists(folder)) // System.IO.Directory.CreateDirectory(folder); string folder = GetFolderPathPreviewImage(); imagePath = string.Format("{0}/preview{1}.jpg", folder, objectResult.Job_Id); } else { string parentFolder = AppDomain.CurrentDomain.BaseDirectory + "OfflineData\\"; previewfolder = parentFolder + "Previews"; imagePath = string.Format("{0}/previewMock.jpg", previewfolder); captchaFolder = parentFolder + "Reject"; captchaPath = string.Format("{0}/captchaMock.jpg", captchaFolder); objectResult.CaptchaURL = captchaPath; } objectResult.PreviewImage = imagePath; // call success callback method reviewJobSuccessCallback.Invoke(objectResult); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }
// async callback method /// <summary> /// /// </summary> /// <param name="ar"></param> private void GetLanguagesCallBack(IAsyncResult ar) { try { // get result GetLanguagesMethodCaller caller = (GetLanguagesMethodCaller)ar.AsyncState; // look for error in response string returnValue = caller.EndInvoke(ar); ErrorInfo error = CheckForError(returnValue); if (error != null) { methodErrorCallback.Invoke(error); return; } // map result to object List<Language> objectResult = (List<Language>)ConvertResultToObject(returnValue, ApiMethod.GetLanguages); // call success callback method getLanguagesSuccessCallback.Invoke(objectResult); } catch (Exception ex) { ErrorInfo error = new ErrorInfo(ErrorType.AppError, ex.Message); methodErrorCallback.Invoke(error); } }