Example #1
0
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Success</returns>
        /// <exception cref="ApiClientException">A server side error occurred.</exception>
        public async Task UploadModAsync(FileParameter archiveUpload, CancellationToken cancellationToken)
        {
            var urlBuilder_ = new StringBuilder();

            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/v1/mods/upload");

            var client_ = _httpClient;

            using (var request_ = new HttpRequestMessage())
            {
                var boundary_ = Guid.NewGuid().ToString();
                var content_  = new MultipartFormDataContent(boundary_);
                content_.Headers.Remove("Content-Type");
                content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);
                if (archiveUpload != null)
                {
                    var content_archiveUpload_ = new StreamContent(archiveUpload.Data);
                    if (!string.IsNullOrEmpty(archiveUpload.ContentType))
                    {
                        content_archiveUpload_.Headers.ContentType = MediaTypeHeaderValue.Parse(archiveUpload.ContentType);
                    }
                    content_.Add(content_archiveUpload_, "ArchiveUpload", archiveUpload.FileName ?? "ArchiveUpload");
                }
                request_.Content = content_;
                request_.Method  = new HttpMethod("POST");

                PrepareRequest(client_, request_, urlBuilder_);
                var url_ = urlBuilder_.ToString();
                request_.RequestUri = new Uri(url_, UriKind.RelativeOrAbsolute);
                PrepareRequest(client_, request_, url_);

                var response_ = await client_.SendAsync(request_, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

                try
                {
                    var headers_ = Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                    if (response_.Content != null && response_.Content.Headers != null)
                    {
                        foreach (var item_ in response_.Content.Headers)
                        {
                            headers_[item_.Key] = item_.Value;
                        }
                    }

                    ProcessResponse(client_, response_);

                    var status_ = ((int)response_.StatusCode).ToString();
                    if (status_ == "200")
                    {
                    }
                    else
                    if (status_ != "200" && status_ != "204")
                    {
                        var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                        throw new ApiClientException("The HTTP status code of the response was not expected (" + (int)response_.StatusCode + ").", (int)response_.StatusCode, responseData_, headers_, null);
                    }
                }
                finally
                {
                    if (response_ != null)
                    {
                        response_.Dispose();
                    }
                }
            }
        }
Example #2
0
 /// <returns>Success</returns>
 /// <exception cref="ApiClientException">A server side error occurred.</exception>
 public Task UploadModAsync(FileParameter archiveUpload)
 {
     return(UploadModAsync(archiveUpload, CancellationToken.None));
 }