public DeploymentResponse Restore(string id, Stream fileStream) { return client.PostFile<DeploymentResponse>( $@"/deployment/{id}/Restore", fileStream, GetTimeStampDumpFileName(), "application/octet-stream"); }
public void Can_POST_upload_stream_using_ServiceClient() { try { var client = new JsonServiceClient(ListeningOn); using (var fileStream = new FileInfo("~/TestExistingDir/upload.html".MapProjectPlatformPath()).OpenRead()) { var fileName = "upload.html"; bool isFilterCalled = false; ServiceClientBase.GlobalRequestFilter = request => { isFilterCalled = true; }; var response = client.PostFile <FileUploadResponse>( "/fileuploads", fileStream, fileName, MimeTypes.GetMimeType(fileName)); fileStream.Position = 0; var expectedContents = new StreamReader(fileStream).ReadToEnd(); Assert.That(isFilterCalled); Assert.That(response.Name, Is.EqualTo("file")); Assert.That(response.FileName, Is.EqualTo(fileName)); Assert.That(response.ContentLength, Is.EqualTo(fileStream.Length)); Assert.That(response.ContentType, Is.EqualTo(MimeTypes.GetMimeType(fileName))); Assert.That(response.Contents, Is.EqualTo(expectedContents)); } } finally { ServiceClientBase.GlobalRequestFilter = null; //reset this to not cause side-effects } }
public void Can_POST_upload_file_and_apply_filter_using_ServiceClient() { try { var client = new JsonServiceClient(ListeningOn); var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPath()); bool isFilterCalled = false; ServiceClientBase.GlobalRequestFilter = request => { isFilterCalled = true; }; var response = client.PostFile <FileUploadResponse>( ListeningOn + "/fileuploads", uploadFile, MimeTypes.GetMimeType(uploadFile.Name)); var expectedContents = new StreamReader(uploadFile.OpenRead()).ReadToEnd(); Assert.That(isFilterCalled); Assert.That(response.FileName, Is.EqualTo(uploadFile.Name)); Assert.That(response.ContentLength, Is.EqualTo(uploadFile.Length)); Assert.That(response.ContentType, Is.EqualTo(MimeTypes.GetMimeType(uploadFile.Name))); Assert.That(response.Contents, Is.EqualTo(expectedContents)); } finally { ServiceClientBase.GlobalRequestFilter = null; //reset this to not cause side-effects } }
/// <summary> /// Adds an attachment to an issue. /// </summary> /// <param name="attachmentsUri">The URI of the attachment resource for a given issue.</param> /// <param name="filename">The name of the file to attach.</param> public void AddAttachment(Uri attachmentsUri, string filename) { var fileInfo = new FileInfo(filename); client.Headers.Add("X-Atlassian-Token", "nocheck"); client.PostFile <JsonObject>(attachmentsUri.ToString(), fileInfo, MimeTypes.GetMimeType(fileInfo.Name)); client.Headers.Remove("X-Atlassian-Token"); }
public void Can_POST_upload_file_using_ServiceClient() { IServiceClient client = new JsonServiceClient(ListeningOn); var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPath()); var response = client.PostFile<FileUploadResponse>( ListeningOn + "/fileuploads", uploadFile, MimeTypes.GetMimeType(uploadFile.Name)); var expectedContents = new StreamReader(uploadFile.OpenRead()).ReadToEnd(); Assert.That(response.FileName, Is.EqualTo(uploadFile.Name)); Assert.That(response.ContentLength, Is.EqualTo(uploadFile.Length)); Assert.That(response.ContentType, Is.EqualTo(MimeTypes.GetMimeType(uploadFile.Name))); Assert.That(response.Contents, Is.EqualTo(expectedContents)); }
public void Can_upload_file() { var client = new JsonServiceClient(BaseUrl); var fileInfo = new FileInfo("~/App_Data/file.json".MapProjectPath()); var response = client.PostFile <UploadFileResponse>( "/UploadFile", fileToUpload: fileInfo, mimeType: "application/json"); Assert.That(response.Name, Is.EqualTo("file")); Assert.That(response.FileName, Is.EqualTo("file.json")); Assert.That(response.ContentLength, Is.EqualTo(fileInfo.Length)); Assert.That(response.ContentType, Is.EqualTo("application/json")); Assert.That(response.Contents, Is.EqualTo(fileInfo.ReadAllText())); Assert.That(response.File, Is.Null); }
public void Can_handle_error_on_POST_upload_file_using_ServiceClient() { IServiceClient client = new JsonServiceClient(ListeningOn); var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPlatformPath()); try { client.PostFile <FileUploadResponse>( ListeningOn + "/fileuploads/ThrowError", uploadFile, MimeTypes.GetMimeType(uploadFile.Name)); Assert.Fail("Upload Service should've thrown an error"); } catch (Exception ex) { var webEx = ex as WebServiceException; var response = (FileUploadResponse)webEx.ResponseDto; Assert.That(response.ResponseStatus.ErrorCode, Is.EqualTo(typeof(NotSupportedException).Name)); Assert.That(response.ResponseStatus.Message, Is.EqualTo("ThrowError")); } }
public TResponse PostFile <TResponse>(string relativeOrAbsoluteUrl, FileInfo fileToUpload, string mimeType) { return(client.PostFile <TResponse>(relativeOrAbsoluteUrl, fileToUpload, mimeType)); }
private void _btnExport_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(_txtOutputFileName.Text)) { MessageBox.Show("Please enter an output file name"); return; } if(_chkPublishToWebsite.Checked && string.IsNullOrEmpty(_txtPublishUrl.Text)) { MessageBox.Show("Please enter a publish URL"); return; } var deployStateDirectory = Path.Combine(_workingDirectory,"states"); if(!Directory.Exists(deployStateDirectory)) { MessageBox.Show("No deploy history found"); return; } var stateFileList = Directory.GetFiles(deployStateDirectory, "*.json"); if(stateFileList.Length == 0) { MessageBox.Show("No deploy history found"); return; } var exportDirectory = Path.Combine(_workingDirectory, "exports", Guid.NewGuid().ToString()); if (!Directory.Exists(exportDirectory)) { Directory.CreateDirectory(exportDirectory); } foreach (var fileName in stateFileList) { string exportFileName = Path.Combine(exportDirectory, Path.GetFileName(fileName)); File.Copy(fileName, exportFileName); } string zipPath = _txtOutputFileName.Text; var zipper = _diFactory.CreateInjectedObject<IZipper>(); zipper.ZipDirectory(exportDirectory, zipPath); if(_chkPublishToWebsite.Checked) { var url = _txtPublishUrl.Text; if (!url.EndsWith("/")) { url += "/"; } if (!url.EndsWith("/api/", StringComparison.CurrentCultureIgnoreCase)) { url += "api/"; } var deployFile = new DeployFileDto { FileData = File.ReadAllBytes(zipPath), FileName = Path.GetFileName(zipPath) }; Cookie authCookie = null; if (!string.IsNullOrEmpty(_txtUserName.Text) && !string.IsNullOrEmpty(_txtPassword.Text)) { authCookie = GetAuthCookie(url, _txtUserName.Text, _txtPassword.Text); } using (var client = new JsonServiceClient(url)) { var fileToUpload = new FileInfo(zipPath); client.AllowAutoRedirect = false; client.Credentials = System.Net.CredentialCache.DefaultCredentials; client.Timeout = TimeSpan.FromMinutes(2); client.ReadWriteTimeout = TimeSpan.FromMinutes(2); if(authCookie != null) { client.CookieContainer.Add(authCookie); } var offlineDeployment = client.Get<OfflineDeployment>(url + "deploy/offline?deployBatchRequestId=" + _batchRequest.Id); if(offlineDeployment == null) { throw new Exception("Could not find offline deployment record for batch request ID " + _batchRequest.Id); } var fileResponse = client.PostFile<DeployFileDto>(url + "file", fileToUpload, MimeTypes.GetMimeType(fileToUpload.Name)); var updateRequest = new OfflineDeployment { Id = offlineDeployment.Id, DeployBatchRequestId = _batchRequest.Id, ResultFileId = fileResponse.Id }; client.Post<OfflineDeployment>(url + "deploy/offline", updateRequest); } } MessageBox.Show("Export Complete"); } catch (Exception err) { WinFormsHelper.DisplayError("Error exporting history", err); } }
private DeployBuild PublishZip(string apiUrl, string projectId, string componentId, string branchId, string version, string zipPath, string userName, string password) { string url = apiUrl; if (!url.EndsWith("/")) { url += "/"; } if (!url.EndsWith("/api/", StringComparison.CurrentCultureIgnoreCase)) { url += "api/"; } var deployFile = new DeployFileDto { FileData = File.ReadAllBytes(zipPath), FileName = Path.GetFileName(zipPath) }; branchId = FormatBranch(branchId, version); Cookie authCookie = null; if(!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { authCookie = GetAuthCookie(apiUrl, userName, password); } using (var client = new JsonServiceClient(url)) { _logger.Debug("Posting file {0} to URL {1}", zipPath, url); //var x = client.Send<DeployFileDto>(deployFile); var fileToUpload = new FileInfo(zipPath); string fileUrl = url + "file"; client.Credentials = System.Net.CredentialCache.DefaultCredentials; client.Timeout = TimeSpan.FromMinutes(5); client.ReadWriteTimeout = TimeSpan.FromMinutes(5); if(authCookie != null) { client.CookieContainer.Add(authCookie); } var fileResponse = client.PostFile<DeployFileDto>(fileUrl, fileToUpload, MimeTypes.GetMimeType(fileToUpload.Name)); _logger.Debug("Done posting file {0} to URL {1}, returned fileId {2} and fileStorageId {3}", zipPath, url, fileResponse.Id, fileResponse.FileStorageId); var buildRequest = new DeployBuild { FileId = fileResponse.Id, ProjectId = projectId, ProjectBranchId = branchId, ProjectComponentId = componentId, Version = version }; _logger.Debug("Posting DeployBuild object to URL {0}, sending{1}", url, buildRequest.ToJson()); string buildUrl = url + "build"; try { var buildResponse = client.Post<DeployBuild>(buildUrl, buildRequest); _logger.Debug("Posting DeployBuild object to URL {0}, returned {1}", url, buildRequest.ToJson()); return buildResponse; } catch(Exception err) { _logger.WarnException(string.Format("Error posting DeployBuild object to URL {0}: {1}, ERROR: {2}", url, buildRequest.ToJson(), err.ToString()), err); throw; } } }
public static void DeployApp(string endpoint, string appName) { try { JsonServiceClient client = new JsonServiceClient(endpoint); Console.WriteLine("----> Compressing files"); byte[] folder = CompressionHelper.CompressFolderToBytes(Environment.CurrentDirectory); Console.WriteLine("----> Uploading files (" + ((float)folder.Length / (1024.0f*1024.0f)) + " MB)"); File.WriteAllBytes("deploy.gzip", folder); client.PostFile<int>("/API/Deploy/" + appName, new FileInfo("deploy.gzip"), "multipart/form-data"); File.Delete("deploy.gzip"); DeployAppStatusRequest request = new DeployAppStatusRequest() { AppName = appName }; DeployAppStatusResponse response = client.Get(request); while (!response.Completed) { Console.Write(response.Log); response = client.Get(request); } } catch (WebServiceException e) { Console.WriteLine(e.ResponseBody); Console.WriteLine(e.ErrorMessage); Console.WriteLine(e.ServerStackTrace); } }