Ejemplo n.º 1
0
        public async System.Threading.Tasks.Task <System.Text.Json.JsonElement> SendFilesAsync(System.Collections.Generic.Dictionary <System.String, System.Collections.Generic.List <SoftmakeAll.SDK.Communication.REST.File> > FormData)
        {
            if (System.String.IsNullOrWhiteSpace(this.URL))
            {
                throw new System.Exception(SoftmakeAll.SDK.Communication.REST.EmptyURLErrorMessage);
            }

            this.HasRequestErrors = false;

            using (System.Net.Http.HttpClient HttpClient = new System.Net.Http.HttpClient())
            {
                if (this.Headers.Count > 0)
                {
                    foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> Header in this.Headers)
                    {
                        HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value);
                    }
                }
                this.AddAuthorizationHeader(HttpClient);

                using (System.Net.Http.MultipartFormDataContent MultipartFormDataContent = new System.Net.Http.MultipartFormDataContent())
                {
                    if ((FormData != null) && (FormData.Count > 0))
                    {
                        foreach (System.Collections.Generic.KeyValuePair <System.String, System.Collections.Generic.List <SoftmakeAll.SDK.Communication.REST.File> > Item in FormData)
                        {
                            if ((!(System.String.IsNullOrWhiteSpace(Item.Key))) && (Item.Value != null) && (Item.Value.Any()))
                            {
                                foreach (SoftmakeAll.SDK.Communication.REST.File File in Item.Value)
                                {
                                    System.Net.Http.ByteArrayContent ByteArrayContent = new System.Net.Http.ByteArrayContent(File.Contents);
                                    if (!(System.String.IsNullOrWhiteSpace(File.ContentType)))
                                    {
                                        ByteArrayContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(File.ContentType);
                                    }
                                    MultipartFormDataContent.Add(ByteArrayContent, Item.Key, File.Name);
                                }
                            }
                        }
                    }

                    if (this.Timeout > 0)
                    {
                        HttpClient.Timeout = System.TimeSpan.FromMilliseconds(this.Timeout);
                    }

                    try
                    {
                        System.Net.Http.HttpResponseMessage HttpResponseMessage;
                        switch (this.Method)
                        {
                        case "PATCH":
                        {
                            HttpResponseMessage = await HttpClient.PatchAsync(this.URL, MultipartFormDataContent);

                            break;
                        }

                        case "POST":
                        {
                            HttpResponseMessage = await HttpClient.PostAsync(this.URL, MultipartFormDataContent);

                            break;
                        }

                        case "PUT":
                        {
                            HttpResponseMessage = await HttpClient.PutAsync(this.URL, MultipartFormDataContent);

                            break;
                        }

                        default: throw new System.NotSupportedException($"Method {this.Method} is not supported for sending files. Plese use PATCH, POST or PUT.");
                        }
                        this._StatusCode      = HttpResponseMessage.StatusCode;
                        this.HasRequestErrors = false;

                        System.Byte[] APIResult = await HttpResponseMessage.Content.ReadAsByteArrayAsync();

                        if (APIResult == null)
                        {
                            return(new System.Text.Json.JsonElement());
                        }

                        return(System.Text.Encoding.UTF8.GetString(APIResult).ToJsonElement());
                    }
                    catch (System.Exception ex)
                    {
                        this.HasRequestErrors = ex.InnerException == null;
                        this._StatusCode      = System.Net.HttpStatusCode.InternalServerError;
                        return(new System.Text.Json.JsonElement());
                    }
                }
            }
        }