コード例 #1
0
ファイル: Ged.cs プロジェクト: clasie/T-P
        /// <summary>
        /// UploadFile according to the the doc type
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="entirePathFileToUpload"></param>
        /// <param name="enumDocType"></param>
        /// <returns></returns>
        public GEDJsonClassRequestResponse UploadFile(
            string barcode,
            string entirePathFileToUpload,
            GedService.DocTypesEnum enumDocType,
            bool doubleCheckTheUpload = false
            )
        {
            //File Validation before going further
            FileValidationHelper.ValidateFile(entirePathFileToUpload);

            //GED Web call preparation
            string    requestURL = $"{GEDUrl}{GedConstants.WebSeparator}{GedConstants.ServiceUpload}";
            string    fileName   = entirePathFileToUpload;
            WebClient wc         = new WebClient();

            byte[] bytes = wc.DownloadData(fileName); //in case the file is on another server.
            wc.Dispose();

            string userAgent          = GedConstants.WebUserAgent;
            string returnResponseText = string.Empty;

            int loopsMax = GedConstants.LoopGetTokenMinConterValue;

            while (loopsMax++ < GedConstants.LoopGetTokenMaxConterValue)
            {
                try
                {
                    //Prepare parameters
                    Dictionary <string, object> postParameters = new Dictionary <string, object>();
                    postParameters.Add(GedConstants.ParameterFile, new UploadFileHelper.FileParameter(bytes, Path.GetFileName(fileName), HttpParamContentType));
                    postParameters.Add(GedConstants.ParameterToken, Token);
                    postParameters.Add(GedConstants.ParameterDocType, enumDocType.ToString());
                    postParameters.Add(GedConstants.ParameterBarCode, barcode);
                    postParameters.Add(GedConstants.ParameterProvider, string.Empty);
                    //Prepare call the service
                    HttpWebResponse webResponse =
                        UploadFileHelper.MultipartFormPost(
                            requestURL,
                            userAgent,
                            postParameters,
                            GedConstants.HeaderCacheParam,
                            GedConstants.HeaderCacheValue
                            );
                    //Call the service
                    StreamReader responseReader = new StreamReader(webResponse.GetResponseStream());
                    returnResponseText = responseReader.ReadToEnd();
                    //Analyse the response
                    return(ExceptionCallerHelper.UploadFileTestExecute(returnResponseText, bytes.Length));
                }
                //The token has expired, we try again just once
                catch (GEDTokenException gedTokenException)
                {
                    Token = GetConnectionToken().Token;
                }
            }
            return(new GEDJsonClassRequestResponse()
            {
                error = $"{GedConstants.UnknownDEGDllError}, {this.GetType().Name}.{System.Reflection.MethodBase.GetCurrentMethod().Name}"
            });
        }
コード例 #2
0
ファイル: Ged.cs プロジェクト: clasie/T-P
        /// <summary>
        /// DownloadFile
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="enumDocType"></param>
        /// <param name="majorVersion">Pas pris en compte par la GED actuellement</param>
        /// <param name="minorVersion">Pas pris en compte par la GED actuellement</param>
        /// <returns></returns>
        public GEDJsonClassRequestResponse DownloadFile(
            string barcode,
            GedService.DocTypesEnum enumDocType,
            int majorVersion = GedConstants.MaxVersionMinimal,
            int minorVersion = GedConstants.MinVersionMinimal
            )
        {
            string requestURL = $"{GEDUrl}{GedConstants.WebSeparator}{GedConstants.ServiceDownload}";
            int    loopsMax   = GedConstants.LoopGetTokenMinConterValue;

            while (loopsMax < GedConstants.LoopGetTokenMaxConterValue)
            {
                try
                {
                    using (WebClient client = new WebClient())
                    {
                        //Construire l'url GET
                        Uri url = new Uri(requestURL).
                                  AddQuery(GedConstants.ParameterRequestType, GedConstants.ParameterRequestTypeJson).
                                  AddQuery(GedConstants.ParameterToken, Token).
                                  AddQuery(GedConstants.ParameterDocType, enumDocType.ToString()).
                                  AddQuery(GedConstants.ParameterBarCode, barcode).
                                  //Gestion des versions
                                  AddQuery(GedConstants.ParameterMajorVersion, (GedConstants.MaxVersionMinimal.Equals(majorVersion)) ? string.Empty: majorVersion.ToString()).
                                  AddQuery(GedConstants.ParameterMinorVersion, (GedConstants.MinVersionMinimal.Equals(minorVersion)) ? string.Empty : minorVersion.ToString()).
                                  //Demande d'annexe
                                  AddQuery(GedConstants.ParameterAnnexe, (GedService.DocTypesEnum.AFAC.Equals(enumDocType))?true.ToString().ToLower():false.ToString().ToLower());

                        //Ask the GED
                        byte[] responsebytes = client.DownloadData(url.ToString());
                        string responsebody  = Encoding.UTF8.GetString(responsebytes);

                        //Analyse the response
                        return(ExceptionCallerHelper.UploadFileTestExecute(responsebody));
                    }
                }
                catch (GEDTokenException gedTokenException)
                {
                    //The token has expired or is empty, try again but just once
                    Token = GetConnectionToken().Token;
                }
                //Avoid end loop if GED problems
                loopsMax++;
            }
            return(new GEDJsonClassRequestResponse()
            {
                error = $"{GedConstants.UnknownDEGDllError}, {this.GetType().Name}.{System.Reflection.MethodBase.GetCurrentMethod().Name}"
            });
        }
コード例 #3
0
ファイル: Ged.cs プロジェクト: clasie/T-P
 /// <summary>
 /// This method is made to test the connection, it returns the GEDToken instance
 /// class (containing a token and a date) from the GED web service
 /// OR throws the possible following exceptions:
 /// GEDInvalidCredentialsException OR
 /// GEDUserLockedTooMuchFailingAttempsToConnectException OR
 /// GEDConnectionUnknownException.
 /// </summary>
 /// <returns>GEDToken</returns>
 public GedTokenAnswer GetConnectionToken()
 {
     return(ExceptionCallerHelper.TokenTestExecute(
                TokenWebHelper.GetNewToken(UserName, UserPassWord, GEDUrl)));
 }