public static CreateDocumentOKResponse CreateDocument(ServiceHelper serviceHelper, DocumentInfo documentInfo, CaseInfo caseInfo, ProjectInfo projectInfo) { Byte[] bytes = File.ReadAllBytes(@$ "{documentInfo.File_Path}"); String base64file = Convert.ToBase64String(bytes); CreateDocumentArgsParameterFilesItem file = new CreateDocumentArgsParameterFilesItem() { Title = documentInfo.File_Title, Format = documentInfo.File_Format, Base64Data = base64file }; List <CreateDocumentArgsParameterFilesItem> files = new List <CreateDocumentArgsParameterFilesItem>(); files.Add(file); CreateCaseWithProjectResponse createCaseWithProjectResponse = null; if (documentInfo.CreateCase) { createCaseWithProjectResponse = CreateCase(serviceHelper, caseInfo, projectInfo); documentInfo.CaseNumber = createCaseWithProjectResponse.createCaseOKResponse.CaseNumber; } CreateDocumentArgs createDocumentArgs = new CreateDocumentArgs(new CreateDocumentArgsParameter() { CaseNumber = documentInfo.CaseNumber, Title = documentInfo.Title, UnofficialTitle = documentInfo.UnofficialTitle, DocumentDate = documentInfo.DocumentDate, JournalDate = documentInfo.JournalDate, DispatchedDate = documentInfo.DispatchedDate, Category = documentInfo.Category, Status = documentInfo.Status, Files = files }); CreateDocumentOKResponse createDocumentOKResponse = serviceHelper.DocumentService.CreateDocument(createDocumentArgs); CreateDocumentWithCaseResponse createDocumentWithFileStreamResponse = new CreateDocumentWithCaseResponse() { createDocumentOKResponse = createDocumentOKResponse, createCaseWithProjectResponse = createCaseWithProjectResponse ?? new CreateCaseWithProjectResponse() }; return(createDocumentOKResponse); }
/// <summary> /// /// Create document /// /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='createDocumentArgs'> /// (See model for parameter info) /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <CreateDocumentOKResponse> CreateDocumentAsync(this IDocumentService operations, CreateDocumentArgs createDocumentArgs = default(CreateDocumentArgs), CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.CreateDocumentWithHttpMessagesAsync(createDocumentArgs, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// /// Create document /// /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='createDocumentArgs'> /// (See model for parameter info) /// </param> public static CreateDocumentOKResponse CreateDocument(this IDocumentService operations, CreateDocumentArgs createDocumentArgs = default(CreateDocumentArgs)) { return(Task.Factory.StartNew(s => ((IDocumentService)s).CreateDocumentAsync(createDocumentArgs), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
/// <summary> /// /// Create document /// /// </summary> /// <param name='createDocumentArgs'> /// (See model for parameter info) /// </param> /// <param name='customHeaders'> /// Headers that will be added to request. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> /// <return> /// A response object containing the response body and response headers. /// </return> public async Task <HttpOperationResponse <CreateDocumentOKResponse> > CreateDocumentWithHttpMessagesAsync(CreateDocumentArgs createDocumentArgs = default(CreateDocumentArgs), Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) { // Tracing bool _shouldTrace = ServiceClientTracing.IsEnabled; string _invocationId = null; if (_shouldTrace) { _invocationId = ServiceClientTracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("createDocumentArgs", createDocumentArgs); tracingParameters.Add("cancellationToken", cancellationToken); ServiceClientTracing.Enter(_invocationId, this, "CreateDocument", tracingParameters); } // Construct URL var _baseUrl = this.BaseUri.AbsoluteUri; var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "CreateDocument").ToString(); // Create HTTP transport objects HttpRequestMessage _httpRequest = new HttpRequestMessage(); HttpResponseMessage _httpResponse = null; _httpRequest.Method = new HttpMethod("POST"); _httpRequest.RequestUri = new Uri(_url); // Set Headers if (customHeaders != null) { foreach (var _header in customHeaders) { if (_httpRequest.Headers.Contains(_header.Key)) { _httpRequest.Headers.Remove(_header.Key); } _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); } } // Serialize Request string _requestContent = null; if (createDocumentArgs != null) { _requestContent = SafeJsonConvert.SerializeObject(createDocumentArgs, this.SerializationSettings); _httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8); _httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); //File.AppendAllText("C:/Users/360admin/Downloads/test/test.txt", _requestContent); } // Set Credentials if (this.Credentials != null) { cancellationToken.ThrowIfCancellationRequested(); await this.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); } // Send Request if (_shouldTrace) { ServiceClientTracing.SendRequest(_invocationId, _httpRequest); } cancellationToken.ThrowIfCancellationRequested(); _httpResponse = await this.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); if (_shouldTrace) { ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); } HttpStatusCode _statusCode = _httpResponse.StatusCode; cancellationToken.ThrowIfCancellationRequested(); string _responseContent = null; if ((int)_statusCode != 200) { var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode)); _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); if (_shouldTrace) { ServiceClientTracing.Error(_invocationId, ex); } _httpRequest.Dispose(); if (_httpResponse != null) { _httpResponse.Dispose(); } throw ex; } // Create Result var _result = new HttpOperationResponse <CreateDocumentOKResponse>(); _result.Request = _httpRequest; _result.Response = _httpResponse; // Deserialize Response if ((int)_statusCode == 200) { _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); try { _result.Body = SafeJsonConvert.DeserializeObject <CreateDocumentOKResponse>(_responseContent, this.DeserializationSettings); } catch (JsonException ex) { _httpRequest.Dispose(); if (_httpResponse != null) { _httpResponse.Dispose(); } throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); } } if (_shouldTrace) { ServiceClientTracing.Exit(_invocationId, _result); } return(_result); }
public static CreateDocumentWithFileUploadResponse CreateDocumentWithFileUpload(ServiceHelper serviceHelper, DocumentInfo documentInfo, CaseInfo caseInfo, ProjectInfo projectInfo) { Byte[] bytes = File.ReadAllBytes(@$ "{documentInfo.File_Path}"); UploadArgs uploadArgsParameter = new UploadArgs(new UploadArgsParameter() { FileData = bytes.Select(b => b as object).ToList() }); if (String.IsNullOrWhiteSpace(documentInfo.File_Format) == false) { uploadArgsParameter.Parameter.FileFormat = documentInfo.File_Format; } if (String.IsNullOrWhiteSpace(documentInfo.File_Title) == false) { uploadArgsParameter.Parameter.FileName = documentInfo.File_Title; } if (String.IsNullOrWhiteSpace(documentInfo.AdContextUser) == false) { uploadArgsParameter.Parameter.ADContextUser = documentInfo.AdContextUser; } if (String.IsNullOrWhiteSpace(documentInfo.User) == false) { uploadArgsParameter.Parameter.User = documentInfo.User; } UploadOKResponse uploadOKResponse = serviceHelper.FileService.Upload(uploadArgsParameter); CreateDocumentArgsParameterFilesItem file = new CreateDocumentArgsParameterFilesItem() { Title = documentInfo.File_Title, Format = documentInfo.File_Format, UploadedFileReference = uploadOKResponse.FileReference }; List <CreateDocumentArgsParameterFilesItem> files = new List <CreateDocumentArgsParameterFilesItem>(); files.Add(file); CreateDocumentArgsParameterFilesFromTemplateItem template = null; if (String.IsNullOrWhiteSpace(documentInfo.TemplateId) == false && String.IsNullOrWhiteSpace(documentInfo.TemplateTitle) == false) { template = new CreateDocumentArgsParameterFilesFromTemplateItem() { TemplateId = documentInfo.TemplateId, Title = documentInfo.TemplateTitle }; } CreateCaseWithProjectResponse createCaseWithProjectResponse = null; if (documentInfo.CreateCase) { createCaseWithProjectResponse = CreateCase(serviceHelper, caseInfo, projectInfo); documentInfo.CaseNumber = createCaseWithProjectResponse.createCaseOKResponse.CaseNumber; } CreateDocumentArgs createDocumentArgs = new CreateDocumentArgs(new CreateDocumentArgsParameter() { CaseNumber = documentInfo.CaseNumber, Title = documentInfo.Title, UnofficialTitle = documentInfo.UnofficialTitle, DocumentDate = documentInfo.DocumentDate, JournalDate = documentInfo.JournalDate, DispatchedDate = documentInfo.DispatchedDate, Category = documentInfo.Category, Status = documentInfo.Status, Files = files }); if (template != null) { createDocumentArgs.Parameter.FilesFromTemplate = new[] { template }; } CreateDocumentOKResponse createDocumentOKResponse = serviceHelper.DocumentService.CreateDocument(createDocumentArgs); CreateDocumentWithFileUploadResponse createDocumentWithFileStreamResponse = new CreateDocumentWithFileUploadResponse() { uploadOKResponse = uploadOKResponse, createDocumentOKResponse = createDocumentOKResponse, createCaseWithProjectResponse = createCaseWithProjectResponse ?? new CreateCaseWithProjectResponse() }; return(createDocumentWithFileStreamResponse); }