示例#1
0
        public void DeleteFile(Uri serverUrl, string serverFileName, string taskId)
        {
            HttpResponseMessage response = null;
            var responseContent          = string.Empty;

            try
            {
                var link = StringHelpers.Invariant($"{serverUrl}{Settings.V1}/upload/delete");

                using (var http = new HttpClient())
                {
                    AddAuthorizationHeader(http);

                    var multipartFormDataContent = new MultipartFormDataContent();

                    var deleteRequest = new BaseTaskRequest();

                    deleteRequest.FormData.Add("task", taskId);
                    deleteRequest.FormData.Add("server_filename", serverFileName);

                    SetMultiPartFormData(deleteRequest.FormData, multipartFormDataContent);

                    response = http.PostAsync(link, multipartFormDataContent).Result;

                    responseContent = response.Content.ReadAsStringAsync().Result;

                    response.EnsureSuccessStatusCode();
                }
            }
            catch (Exception e)
            {
                throw ParseRequestErrors(response, responseContent, e);
            }
        }
示例#2
0
        public UploadTaskResponse UploadFile(Uri serverUrl, Uri url, string taskId)
        {
            HttpResponseMessage response = null;
            var responseContent          = string.Empty;

            var link = StringHelpers.Invariant($"{serverUrl}{Settings.V1}/upload");

            try
            {
                using (var httpClient = new HttpClient())
                {
                    AddAuthorizationHeader(httpClient);
                    var multiPart = new MultipartFormDataContent();
                    var request   = new BaseTaskRequest();
                    request.FormData.Add("cloud_file", url.AbsoluteUri);
                    request.FormData.Add("task", taskId);
                    SetMultiPartFormData(request.FormData, multiPart);

                    response = httpClient.PostAsync(link, multiPart).Result;

                    responseContent = response.Content.ReadAsStringAsync().Result;

                    response.EnsureSuccessStatusCode();

                    return(JsonConvert.DeserializeObject <UploadTaskResponse>(responseContent));
                }
            }
            catch (Exception e)
            {
                throw ParseRequestErrors(response, responseContent, e);
            }
        }
示例#3
0
        public UploadTaskResponse UploadFile(Uri serverUrl, Byte[] fileByteArray, String fileName, String taskId)
        {
            HttpResponseMessage response = null;
            var responseContent          = String.Empty;

            var link = StringHelpers.Invariant($"{serverUrl}{Settings.V1}/upload");

            try
            {
                using (var httpClient = new HttpClient())
                {
                    addAuthorizationHeader(httpClient);
                    var multiPart     = new MultipartFormDataContent();
                    var uploadRequest = new BaseTaskRequest();

                    uploadRequest.FormData.Add("task", taskId);
                    uploadRequest.FormData.Add("file", new FileParameter(fileByteArray, fileName));
                    setMultiPartFormData(uploadRequest.FormData, multiPart);

                    response = httpClient.PostAsync(link, multiPart).Result;

                    responseContent = response.Content.ReadAsStringAsync().Result;

                    response.EnsureSuccessStatusCode();

                    return(JsonConvert.DeserializeObject <UploadTaskResponse>(responseContent));
                }
            }
            catch (Exception e)
            {
                throw parseRequestErrors(response, responseContent, e);
            }
        }
示例#4
0
        public ConnectTaskResponse ConnectTask(String parentTaskId, String tool)
        {
            HttpResponseMessage response = null;
            var responseContent          = String.Empty;

            try
            {
                using (var httpClient = new HttpClient())
                {
                    addAuthorizationHeader(httpClient);

                    var link = StringHelpers.Invariant($"{Settings.StartUrl}/{Settings.V1}/task/next");

                    var multipartFormDataContent = new MultipartFormDataContent();

                    var request = new BaseTaskRequest();
                    request.FormData.Add("task", parentTaskId);
                    request.FormData.Add("tool", tool);

                    setMultiPartFormData(request.FormData, multipartFormDataContent);

                    response        = httpClient.PostAsync(link, multipartFormDataContent).Result;
                    responseContent = response.Content.ReadAsStringAsync().Result;

                    response.EnsureSuccessStatusCode();

                    return(JsonConvert.DeserializeObject <ConnectTaskResponse>(responseContent));
                }
            }
            catch (Exception e)
            {
                throw parseRequestErrors(response, responseContent, e);
            }
        }
示例#5
0
        public UploadTaskResponse UploadFileByChunk(Uri serverUrl, FileInfo fileInfo, String taskId)
        {
            UploadTaskResponse  results;
            HttpResponseMessage response = null;
            var responseContent          = String.Empty;

            var link = StringHelpers.Invariant($"{serverUrl}{Settings.V1}/upload");

            try
            {
                using (var httpClient = new HttpClient())
                {
                    addAuthorizationHeader(httpClient);

                    var uploadRequest = new BaseTaskRequest();
                    uploadRequest.FormData.Add("task", taskId);

                    //set default values
                    uploadRequest.FormData.Add("file", "");
                    uploadRequest.FormData.Add("chunk", "");

                    List <Byte[]> chunksToUpload;

                    using (var fs = fileInfo.OpenRead())
                    {
                        chunksToUpload = toChunks(fs);

                        uploadRequest.FormData.Add("chunks",
                                                   chunksToUpload.Count.ToString(CultureInfo.InvariantCulture));
                    }

                    for (var i = 0; i < chunksToUpload.Count; i++)
                    {
                        using (var multipartFormDataContent = new MultipartFormDataContent())
                        {
                            uploadRequest.FormData["chunk"] = i.ToString(CultureInfo.InvariantCulture);
                            uploadRequest.FormData["file"]  = new FileParameter(chunksToUpload[i], fileInfo.Name);

                            setMultiPartFormData(uploadRequest.FormData, multipartFormDataContent);

                            response = httpClient.PostAsync(link, multipartFormDataContent).Result;

                            responseContent = response.Content.ReadAsStringAsync().Result;

                            response.EnsureSuccessStatusCode();
                        }
                    }

                    results = JsonConvert.DeserializeObject <UploadTaskResponse>(responseContent);
                }
            }
            catch (Exception e)
            {
                throw parseRequestErrors(response, responseContent, e);
            }

            return(results);
        }