Example #1
0
    public static async Task Upload(string path, AppVersion version, AppArch arch)
    {
        try
        {
            var t   = DateTime.Now;
            var api = new AppsClient {
                BaseUrl = "https://elyspio.fr/updater"
            };
            var bytes = File.ReadAllBytes(path);
            Console.WriteLine("Adding " + path);
            await api.AddAsync("Elytools", version.ToString(), arch, bytes);

            Console.WriteLine($"Added {path} in {(DateTime.Now - t).Seconds}s");
        }

        catch (Exception e)
        {
            Console.WriteLine($"Error in upload of {path}, retry in 1s");
            await Task.Delay(1000);
            await Upload(path, version, arch);
        }
    }
Example #2
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="ApiException">A server side error occurred.</exception>
        public async System.Threading.Tasks.Task AddAsync(string name, string version, AppArch arch, byte[] body, System.Threading.CancellationToken cancellationToken)
        {
            if (name == null)
            {
                throw new System.ArgumentNullException("name");
            }

            if (version == null)
            {
                throw new System.ArgumentNullException("version");
            }

            if (arch == null)
            {
                throw new System.ArgumentNullException("arch");
            }

            if (body == null)
            {
                throw new System.ArgumentNullException("body");
            }

            var urlBuilder_ = new System.Text.StringBuilder();

            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/apps/{name}/{arch}/{version}");
            urlBuilder_.Replace("{name}", System.Uri.EscapeDataString(ConvertToString(name, System.Globalization.CultureInfo.InvariantCulture)));
            urlBuilder_.Replace("{version}", System.Uri.EscapeDataString(ConvertToString(version, System.Globalization.CultureInfo.InvariantCulture)));
            urlBuilder_.Replace("{arch}", System.Uri.EscapeDataString(ConvertToString(arch, System.Globalization.CultureInfo.InvariantCulture)));

            var client_        = new System.Net.Http.HttpClient();
            var disposeClient_ = true;

            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value));
                    content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json-patch+json");
                    request_.Content             = content_;
                    request_.Method = new System.Net.Http.HttpMethod("POST");

                    PrepareRequest(client_, request_, urlBuilder_);

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

                    PrepareRequest(client_, request_, url_);

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

                    var disposeResponse_ = true;
                    try
                    {
                        var headers_ = System.Linq.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;
                        if (status_ == 200)
                        {
                            return;
                        }
                        else
                        {
                            var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
                        }
                    }
                    finally
                    {
                        if (disposeResponse_)
                        {
                            response_.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (disposeClient_)
                {
                    client_.Dispose();
                }
            }
        }
Example #3
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="ApiException">A server side error occurred.</exception>
        public async System.Threading.Tasks.Task <AppVersion> GetLatestArchSpecificVersionAsync(string name, AppArch arch, System.Threading.CancellationToken cancellationToken)
        {
            if (name == null)
            {
                throw new System.ArgumentNullException("name");
            }

            if (arch == null)
            {
                throw new System.ArgumentNullException("arch");
            }

            var urlBuilder_ = new System.Text.StringBuilder();

            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/apps/{name}/{arch}/version");
            urlBuilder_.Replace("{name}", System.Uri.EscapeDataString(ConvertToString(name, System.Globalization.CultureInfo.InvariantCulture)));
            urlBuilder_.Replace("{arch}", System.Uri.EscapeDataString(ConvertToString(arch, System.Globalization.CultureInfo.InvariantCulture)));

            var client_        = new System.Net.Http.HttpClient();
            var disposeClient_ = true;

            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    request_.Method = new System.Net.Http.HttpMethod("GET");
                    request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

                    PrepareRequest(client_, request_, urlBuilder_);

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

                    PrepareRequest(client_, request_, url_);

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

                    var disposeResponse_ = true;
                    try
                    {
                        var headers_ = System.Linq.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;
                        if (status_ == 200)
                        {
                            var objectResponse_ = await ReadObjectResponseAsync <AppVersion>(response_, headers_, cancellationToken).ConfigureAwait(false);

                            if (objectResponse_.Object == null)
                            {
                                throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
                            }
                            return(objectResponse_.Object);
                        }
                        else
                        {
                            var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
                        }
                    }
                    finally
                    {
                        if (disposeResponse_)
                        {
                            response_.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (disposeClient_)
                {
                    client_.Dispose();
                }
            }
        }
Example #4
0
 /// <returns>Success</returns>
 /// <exception cref="ApiException">A server side error occurred.</exception>
 public System.Threading.Tasks.Task <AppVersion> GetLatestArchSpecificVersionAsync(string name, AppArch arch)
 {
     return(GetLatestArchSpecificVersionAsync(name, arch, System.Threading.CancellationToken.None));
 }
Example #5
0
 /// <returns>Success</returns>
 /// <exception cref="ApiException">A server side error occurred.</exception>
 public System.Threading.Tasks.Task AddAsync(string name, string version, AppArch arch, byte[] body)
 {
     return(AddAsync(name, version, arch, body, System.Threading.CancellationToken.None));
 }
Example #6
0
 /// <returns>Success</returns>
 /// <exception cref="ApiException">A server side error occurred.</exception>
 public System.Threading.Tasks.Task <IResult> GetBinaryAsync(string name, string version, AppArch arch)
 {
     return(GetBinaryAsync(name, version, arch, System.Threading.CancellationToken.None));
 }
Example #7
0
 /// <returns>Success</returns>
 /// <exception cref="ApiException">A server side error occurred.</exception>
 public System.Threading.Tasks.Task DeleteAsync(string name, string version, AppArch arch)
 {
     return(DeleteAsync(name, version, arch, System.Threading.CancellationToken.None));
 }