Exemple #1
0
        public void must_provide_required_args_to_builder()
        {
            var content = new CapturedMultipartContent();

            Assert.Throws <ArgumentNullException>(() => content.AddStringParts(null));
            Assert.Throws <ArgumentNullException>(() => content.AddString("other", null));
            Assert.Throws <ArgumentException>(() => content.AddString(null, "hello!"));
            Assert.Throws <ArgumentException>(() => content.AddFile("  ", "path"));
        }
Exemple #2
0
        /**
         * @param string $method
         * @param string $path
         * @param object $query
         * @param object $body
         * @param object files
         *
         * @return Task<string>
         */
        private async Task <HttpResponseMessage> Request(string method, string path, object query, object body, object files)
        {
            Url url = new Url($"{this.url}/v3/{path}");

            FlurlClient Request = url
                                  .SetQueryParams(query)
                                  .WithOAuthBearerToken(this.accessToken)
                                  .WithHeader("User-Agent", "signaturit-net-sdk 1.1.0");

            switch (method)
            {
            case "get":
                return(await Request.GetAsync());

            case "post":
                if (files == null)
                {
                    body = body == null ? new {} : body;

                    return(await Request.PostJsonAsync(body as object));
                }

                var content = new CapturedMultipartContent();

                foreach (string file in files as IList <string> )
                {
                    string name = System.IO.Path.GetFileName(file);
                    string ext  = System.IO.Path.GetExtension(file);
                    string mime = ext == "pdf" ? "application/pdf" : "application/msword";

                    content.AddFile($"files[{name}]", file, mime);
                }

                captureMultipartContentInObject(content, body, "");

                return(await Request.PostAsync(content));

            case "patch":
                body = body == null ? new {} : body;

                return(await Request.PatchJsonAsync(body as object));

            case "delete":
                return(await Request.DeleteAsync());
            }

            return(null);
        }
Exemple #3
0
        public async Task <IEnumerable <TemporaryAttachment> > AttachTemporaryFileToServiceDeskAsync(string serviceDeskId, IEnumerable <string> filePaths)
        {
            var content = new CapturedMultipartContent();

            foreach (var filePath in filePaths)
            {
                content.AddFile("file", File.OpenRead(filePath), Path.GetFileName(filePath));
            }

            var response = await GetServiceDeskUrl(serviceDeskId)
                           .AppendPathSegment("/attachTemporaryFile")
                           .SendAsync(HttpMethod.Post, content)
                           .ConfigureAwait(false);

            return(await HandleResponseAsync <IEnumerable <TemporaryAttachment> >(response, s => JsonConvert.DeserializeObject <TemporaryAttachmentsResult>(s).TemporaryAttachments).ConfigureAwait(false));
        }
        // dev references for IFormFile and CapturedMultipartContent:
        // https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.iformfile?view=aspnetcore-2.2
        // https://github.com/tmenier/Flurl/issues/113
        // https://stackoverflow.com/questions/50340234/sending-rest-request-with-files-and-json-restsharp-on-framework-and-standard#50374486
        public async Task <IEnumerable <TemporaryAttachment> > AttachTemporaryFileToServiceDeskAsync(string serviceDeskId, IList <IFormFile> files)
        {
            var content = new CapturedMultipartContent();

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    content.AddFile(file.Name, file.OpenReadStream(), file.FileName);
                }
            }

            var response = await GetServiceDeskUrl(serviceDeskId)
                           .AppendPathSegment("/attachTemporaryFile")
                           .SendAsync(HttpMethod.Post, content)
                           .ConfigureAwait(false);

            return(await HandleResponseAsync <IEnumerable <TemporaryAttachment> >(response, s => JsonConvert.DeserializeObject <TemporaryAttachmentsResult>(s).TemporaryAttachments).ConfigureAwait(false));
        }