Example #1
0
        public async Task UploadAttachment()
        {
            IPlanGridApi client  = PlanGridClient.Create();
            var          docName = Guid.NewGuid().ToString();
            FileUpload   request = await client.CreateAttachmentUploadRequest(TestData.Project2Uid, new AttachmentUpload
            {
                ContentType = AttachmentUpload.Pdf,
                Name        = docName,
                Folder      = "test folder"
            });

            Stream     payload    = typeof(AttachmentTests).Assembly.GetManifestResourceStream("PlanGrid.Api.Tests.TestData.Sample.pdf");
            Attachment attachment = await client.Upload <Attachment>(request, payload);

            Assert.AreEqual(docName, attachment.Name);
            Assert.AreEqual("test folder", attachment.Folder);
            Assert.AreEqual(TestData.ApiTestsUserUid, attachment.CreatedBy.Uid);
            Assert.AreNotEqual(attachment.CreatedAt, default(DateTime));
            Assert.AreEqual(request.Uid, attachment.Uid);

            using (var downloader = new HttpClient(new HttpClientHandler {
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, AllowAutoRedirect = true
            }))
            {
                Stream returnedPayload = await downloader.GetStreamAsync(attachment.Url);

                payload = typeof(AttachmentTests).Assembly.GetManifestResourceStream("PlanGrid.Api.Tests.TestData.Sample.pdf");
                var payloadBytes = new MemoryStream();
                await payload.CopyToAsync(payloadBytes);

                var returnedBytes = new MemoryStream();
                await returnedPayload.CopyToAsync(returnedBytes);

                Assert.IsTrue(payloadBytes.ToArray().SequenceEqual(returnedBytes.ToArray()));
            }

            Attachment retrievedAttachment = await client.GetAttachment(TestData.Project2Uid, attachment.Uid);

            Assert.IsFalse(retrievedAttachment.IsDeleted);
            await client.RemoveAttachment(TestData.Project2Uid, attachment.Uid);

            Attachment removedAttachment = await client.GetAttachment(TestData.Project2Uid, attachment.Uid);

            Assert.IsTrue(removedAttachment.IsDeleted);
        }
Example #2
0
        public async Task ReferenceAttachment()
        {
            IPlanGridApi client    = PlanGridClient.Create();
            var          rfiInsert = new RfiUpsert
            {
                Question   = "test question",
                Answer     = "test answer",
                AssignedTo = new[] { TestData.ApiTestsUserUid },
                DueAt      = new DateTime(2020, 1, 1),
                IsLocked   = false,
                SentAt     = new DateTime(2019, 1, 1),
                StatusUid  = TestData.Project2DraftRfiStatusUid,
                Title      = "test title"
            };
            Rfi rfi = await client.CreateRfi(TestData.Project2Uid, rfiInsert);

            FileUpload request = await client.CreateAttachmentUploadRequest(TestData.Project2Uid, new AttachmentUpload
            {
                ContentType = AttachmentUpload.Pdf,
                Name        = "test name",
                Folder      = "test folder"
            });

            Stream     payload    = typeof(AttachmentTests).Assembly.GetManifestResourceStream("PlanGrid.Api.Tests.TestData.Sample.pdf");
            Attachment attachment = await client.Upload <Attachment>(request, payload);

            await client.ReferenceAttachmentFromRfi(TestData.Project2Uid, rfi.Uid, new AttachmentReference { AttachmentUid = attachment.Uid });

            Page <Attachment> attachments = await client.GetRfiAttachments(TestData.Project2Uid, rfi.Uid);

            Attachment rfiAttachment = attachments.Data.Single();

            Assert.AreEqual(attachment.Uid, rfiAttachment.Uid);

            await client.RemoveAttachmentFromRfi(TestData.Project2Uid, rfi.Uid, rfiAttachment.Uid);

            attachments = await client.GetRfiAttachments(TestData.Project2Uid, rfi.Uid);

            Assert.AreEqual(0, attachments.Data.Length);
        }
        public static async Task <Photo> UploadPhoto(this IPlanGridApi api, string projectUid, string contentType, string title, Stream payload, CancellationToken cancellationToken = default(CancellationToken))
        {
            FileUpload request = await api.CreatePhotoUploadRequest(projectUid, new PhotoUpload { ContentType = contentType, Title = title });

            return(await api.Upload <Photo>(request, payload, cancellationToken));
        }
        public static async Task <Attachment> UploadAttachment(this IPlanGridApi api, string projectUid, string contentType, string name, Stream payload, string folder = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            FileUpload request = await api.CreateAttachmentUploadRequest(projectUid, new AttachmentUpload { ContentType = contentType, Name = name, Folder = folder });

            return(await api.Upload <Attachment>(request, payload, cancellationToken));
        }
        private static async Task RequestFileUpload(this IPlanGridApi api, FileUploadRequest request, VirtualFile file)
        {
            FileUpload fileUpload = await api.Post <FileUpload>(request.Url, new UploadFile { FileName = file.FileName });

            await api.Upload <object>(fileUpload, file.Data);
        }