<commit_before>using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reactive.Linq;
using System.Text;
using Bud.NuGet;
using Bud.Util;
using Bud.V1;
using static Bud.V1.Api;

namespace Bud.Dist {
  public class BinTrayDistribution {
    public static IObservable<bool> Distribute(IConf c)
      => Distribute(DistributionArchive[c],
                    ProjectId[c],
                    ProjectId[c],
                    Environment.UserName,
                    ProjectVersion[c],
                    BuildDir[c],
                    PackageMetadata[c]);

    public static IObservable<bool> Distribute(IObservable<string> observedArchive, string repositoryId, string packadeId, string username, string packageVersion, string buildDir, NuGetPackageMetadata packageMetadata)
      => observedArchive.Select(
        archive => PushToBintray(archive, repositoryId, packadeId, packageVersion, username, "zip")
                     .Map(archiveUrl => ChocoDistribution.PushToChoco(repositoryId, packadeId, packageVersion, archiveUrl, username, buildDir, packageMetadata))
                     .GetOrElse(false));

    public static Option<string> PushToBintray(string package,
                                               string repositoryId,
                                               string packageId,
                                               string packageVersion,
                                               string username,
                                               string fileExtension) {
      var packagePublishUrl = BintrayPublishPackageUrl(packageId, repositoryId, username, packageVersion, fileExtension);
      var apiKey = LoadBintrayApiKey(username);
      Console.WriteLine("Starting to upload to bintray...");
      using (var httpClient = new HttpClient()) {
        var credentials = ToBasicAuthCredentials(username, apiKey);
        httpClient.Timeout = TimeSpan.FromMinutes(15);
        Console.WriteLine($"Timeout: {httpClient.Timeout}");
        using (var content = new StreamContent(File.OpenRead(package))) {
          using (var request = new HttpRequestMessage(HttpMethod.Put, packagePublishUrl)) {
            request.Headers.Add("Authorization", $"Basic {credentials}");
            request.Content = content;
            request.Method = HttpMethod.Put;
            var responseTask = httpClient.SendAsync(request);
            responseTask.Wait(TimeSpan.FromMinutes(15));
            using (var response = responseTask.Result) {
              var uploadSuccess = response.StatusCode == HttpStatusCode.Created;
              Console.WriteLine($"Upload to bintray result code: {response.StatusCode}");
              var responseContentTask = response.Content.ReadAsStringAsync();
              responseContentTask.Wait();
              Console.WriteLine($"Upload to bintray response body: {responseContentTask.Result}");
              return uploadSuccess ?
                       BintrayArchiveDownloadUrl(repositoryId, packageId, username, packageVersion) :
                       Option.None<string>();
            }
          }
        }
      }
    }

    private static string BintrayArchiveDownloadUrl(string repositoryId,
                                                    string packageId,
                                                    string username,
                                                    string packageVersion)
      => $"https://dl.bintray.com/{username}/{repositoryId}/{packageId}-{packageVersion}.zip";

    private static string BintrayPublishPackageUrl(string packageId,
                                                   string repositoryId,
                                                   string username,
                                                   string packageVersion,
                                                   string fileExtension)
      => "https://api.bintray.com/" +
         $"content/{username}/{repositoryId}/{packageId}/" +
         $"{packageVersion}/{packageId}-{packageVersion}.{fileExtension}?publish=1";

    private static string LoadBintrayApiKey(string username) {
      var apiKeyFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                    "Bud",
                                    $"{username}@bintray.apikey");
      return File.ReadAllText(apiKeyFile).Trim();
    }

    private static string ToBasicAuthCredentials(string username, string apiKey)
      => Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + apiKey));
  }
}<commit_msg>Adds file extension to the BinTray download URL.<commit_after>using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reactive.Linq;
using System.Text;
using Bud.NuGet;
using Bud.Util;
using Bud.V1;
using static Bud.V1.Api;

namespace Bud.Dist {
  public class BinTrayDistribution {
    public static IObservable<bool> Distribute(IConf c)
      => Distribute(DistributionArchive[c],
                    ProjectId[c],
                    ProjectId[c],
                    Environment.UserName,
                    ProjectVersion[c],
                    BuildDir[c],
                    PackageMetadata[c]);

    public static IObservable<bool> Distribute(IObservable<string> observedArchive, string repositoryId, string packadeId, string username, string packageVersion, string buildDir, NuGetPackageMetadata packageMetadata)
      => observedArchive.Select(
        archive => PushToBintray(archive, repositoryId, packadeId, packageVersion, username, "zip")
                     .Map(archiveUrl => ChocoDistribution.PushToChoco(repositoryId, packadeId, packageVersion, archiveUrl, username, buildDir, packageMetadata))
                     .GetOrElse(false));

    public static Option<string> PushToBintray(string package,
                                               string repositoryId,
                                               string packageId,
                                               string packageVersion,
                                               string username,
                                               string fileExtension) {
      var packagePublishUrl = BintrayPublishPackageUrl(packageId, repositoryId, username, packageVersion, fileExtension);
      var apiKey = LoadBintrayApiKey(username);
      Console.WriteLine("Starting to upload to bintray...");
      using (var httpClient = new HttpClient()) {
        var credentials = ToBasicAuthCredentials(username, apiKey);
        httpClient.Timeout = TimeSpan.FromMinutes(15);
        Console.WriteLine($"Timeout: {httpClient.Timeout}");
        using (var content = new StreamContent(File.OpenRead(package))) {
          using (var request = new HttpRequestMessage(HttpMethod.Put, packagePublishUrl)) {
            request.Headers.Add("Authorization", $"Basic {credentials}");
            request.Content = content;
            request.Method = HttpMethod.Put;
            var responseTask = httpClient.SendAsync(request);
            responseTask.Wait(TimeSpan.FromMinutes(15));
            using (var response = responseTask.Result) {
              var uploadSuccess = response.StatusCode == HttpStatusCode.Created;
              Console.WriteLine($"Upload to bintray result code: {response.StatusCode}");
              var responseContentTask = response.Content.ReadAsStringAsync();
              responseContentTask.Wait();
              Console.WriteLine($"Upload to bintray response body: {responseContentTask.Result}");
              return uploadSuccess ?
                       BintrayArchiveDownloadUrl(repositoryId, packageId, username, packageVersion, fileExtension) :
                       Option.None<string>();
            }
          }
        }
      }
    }

    private static string BintrayArchiveDownloadUrl(string repositoryId,
                                                    string packageId,
                                                    string username,
                                                    string packageVersion,
                                                    string fileExtension)
      => "https://dl.bintray.com/" +
         $"{username}/{repositoryId}/" +
         $"{packageId}-{packageVersion}.{fileExtension}";

    private static string BintrayPublishPackageUrl(string packageId,
                                                   string repositoryId,
                                                   string username,
                                                   string packageVersion,
                                                   string fileExtension)
      => "https://api.bintray.com/" +
         $"content/{username}/{repositoryId}/{packageId}/" +
         $"{packageVersion}/{packageId}-{packageVersion}.{fileExtension}?publish=1";

    private static string LoadBintrayApiKey(string username) {
      var apiKeyFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                    "Bud",
                                    $"{username}@bintray.apikey");
      return File.ReadAllText(apiKeyFile).Trim();
    }

    private static string ToBasicAuthCredentials(string username, string apiKey)
      => Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + apiKey));
  }
}
{'fraction_non_alphanumeric': 0.0870590758014367, 'fraction_numerical': 0.0019298809906722419, 'mean_word_length': 1.8613496932515337, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 59, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '754092', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2037, 'n_words': 446}