示例#1
0
        public async Task <IRemoteFileMetadata> UploadFileAsync(ILocalResource source, IRemoteResource target)
        {
            await this.FetchCsrfTokenAsync().ConfigureAwait(false);

            HttpResponseMessage responseMessage;
            string content;

            var subPath = await target.GetSubPathAsync();

            try
            {
                var request = new HttpRequestMessage();

                request.RequestUri = new Uri(subPath, UriKind.Relative);
                request.Method     = System.Net.Http.HttpMethod.Post;
                request.Headers.Add("x-csrf-token", this.CSRFToken);
                request.Headers.Add("Accept", "application/xml");
                request.Headers.Add("odata-no-response-payload", "true");

                string requestcontent = "{\"TypeCode\": \"" + target.TypeCode + "\",\"Name\": \"" + source.GetFileName() + "\",\"CategoryCode\": \"2\",\"Binary\": \"" + source.GetBase64SourceString() + "\"}";

                request.Content = new StringContent(requestcontent, Encoding.UTF8, "application/json");

                responseMessage = await this.httpClient.SendAsync(request);

                responseMessage.EnsureSuccessStatusCode();

                await responseMessage.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                throw new C4CClientException("Error occured while uploading file", ex);
            }

            // we send a second request to read the metadata of the newly uploaded file
            HttpHeaders          headers = responseMessage.Headers;
            IEnumerable <string> values;
            string metadataUri = string.Empty;

            if (headers.TryGetValues("Location", out values))
            {
                metadataUri = values.First();
            }

            if (string.IsNullOrEmpty(metadataUri))
            {
                throw new C4CClientException("Location-header not provided by remote host");
            }

            // read back file metadata
            string metadataRequestUri = metadataUri + "?$select=UUID,MimeType,Name,DocumentLink,CategoryCode,LastUpdatedOn";

            responseMessage = await this.httpClient.GetAsync(metadataRequestUri);

            content = await responseMessage.Content.ReadAsStringAsync();

            C4CRemoteFileMetadata metadata = new C4CRemoteFileMetadata(content);

            return(metadata);
        }
示例#2
0
        public async Task <C4CRemoteFileListing> GetFileListingAsync(IRemoteResource source)
        {
            // await fetchCsrfTokenAsync();
            HttpResponseMessage responseMessage;

            string query = "?$select=UUID,MimeType,Name,DocumentLink,CategoryCode,LastUpdatedOn&$orderby=Name";

            string sourceSubPath = await source.GetSubPathAsync();

            try
            {
                responseMessage = await this.httpClient.GetAsync(sourceSubPath + query);

                responseMessage.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                throw new C4CClientException("Error occured while receiving file listing", ex);
            }

            var content = await responseMessage.Content.ReadAsStringAsync();

            C4CRemoteFileListing fileListing = new C4CRemoteFileListing(content);

            return(fileListing);
        }