예제 #1
0
        // ---

        private string CreateUploadFilePayload(APIUploadFile UploadFile)
        {
            // IMPORTANT! DO NOT REMOVE INDENTATION!!
            return($@"
--{BOUNDARY}
Content-Disposition: form-data; name=""file_metadata_0""
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit

{{
    ""AttachableRef"": [
    {{
        ""EntityRef"": {JsonConvert.SerializeObject(UploadFile.EntityRef)}
    }}
    ],
    ""FileName"": ""{UploadFile.FileName}"",
    ""ContentType"": ""{UploadFile.ContentType}""
}}
--{BOUNDARY}
Content-Disposition: form-data; name=""file_content_0""; filename=""{UploadFile.FileName}""
Content-Type: {UploadFile.ContentType}
Content-Transfer-Encoding: base64

{UploadFile.FileContentBase64}
--{BOUNDARY}--
            ");
        }
예제 #2
0
        public async Task <string> UploadFile(APIUploadFile UploadFile)
        {
            var payload = this.CreateUploadFilePayload(UploadFile);

            var response = this.HttpService.POST <string>($"{this.Config.BaseURL}company/{this.Config.CompanyId}/upload", payload, null, new Dictionary <string, string>
            {
                ["Accept"]        = "application/json",
                ["Content-Type"]  = $"multipart/form-data; boundary={BOUNDARY}",
                ["Authorization"] = $"Bearer {this.Config.AccessToken}"
            });

            // Unauthorized (401) - refresh token and try again
            if (!response.Success && response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                await this.RefreshToken();

                response = this.HttpService.POST <string>($"{this.Config.BaseURL}company/{this.Config.CompanyId}/upload", payload, null, new Dictionary <string, string>
                {
                    ["Accept"]        = "application/json",
                    ["Content-Type"]  = $"multipart/form-data; boundary={BOUNDARY}",
                    ["Authorization"] = $"Bearer {this.Config.AccessToken}"
                });
            }

            if (!response.Success)
            {
                throw new APIException(this.ParseError(response.Content));
            }


            var attachableSchema = new
            {
                Attachable = new
                {
                    Id = string.Empty
                }
            };

            var modelSchema = new
            {
                AttachableResponse = new[] { attachableSchema }
            };

            var responseData = JsonConvert.DeserializeAnonymousType(response.Content, modelSchema);

            return(responseData?.AttachableResponse.FirstOrDefault()?.Attachable?.Id);
        }