<commit_before>require 'net/http'
require 'openssl'

module Cellar
  class CellarAdapter
    def initialize(access_key_id, secret_access_key, bucket)
      @endpoint = URI::HTTPS.build(host: "#{bucket}.cellar.services.clever-cloud.com")
      @signer = AmazonV2RequestSigner.new(access_key_id, secret_access_key, bucket)
    end

    def presigned_url(method:, key:, expires_in:, content_type: '', checksum: '', **query_params)
      query = query_params.merge(
        @signer.url_signature_params(
          method: method,
          key: key,
          expires_in: expires_in,
          content_type: content_type,
          checksum: checksum
        )
      )

      URI::join(@endpoint, "/#{key}", "?#{query.to_query}").to_s
    end

    def session
      Net::HTTP.start(@endpoint.host, @endpoint.port, use_ssl: true) do |http|
        yield Session.new(http, @signer)
      end
    end

    class Session
      def initialize(http, signer)
        @http = http
        @signer = signer
      end

      def upload(key, io, checksum)
        with_io_length(io) do |io, length|
          request = Net::HTTP::Put.new("/#{key}")
          request.content_type = 'application/octet-stream'
          request['Content-MD5'] = checksum
          request['Content-Length'] = length
          request.body_stream = io
          @signer.sign(request, key)
          @http.request(request)
          # TODO: error handling
        end
      end

      def download(key, range: nil)<commit_msg>[#3133] Change signature of upload for cellar to match upstream evolution<commit_after>require 'net/http'
require 'openssl'

module Cellar
  class CellarAdapter
    def initialize(access_key_id, secret_access_key, bucket)
      @endpoint = URI::HTTPS.build(host: "#{bucket}.cellar.services.clever-cloud.com")
      @signer = AmazonV2RequestSigner.new(access_key_id, secret_access_key, bucket)
    end

    def presigned_url(method:, key:, expires_in:, content_type: '', checksum: '', **query_params)
      query = query_params.merge(
        @signer.url_signature_params(
          method: method,
          key: key,
          expires_in: expires_in,
          content_type: content_type,
          checksum: checksum
        )
      )

      URI::join(@endpoint, "/#{key}", "?#{query.to_query}").to_s
    end

    def session
      Net::HTTP.start(@endpoint.host, @endpoint.port, use_ssl: true) do |http|
        yield Session.new(http, @signer)
      end
    end

    class Session
      def initialize(http, signer)
        @http = http
        @signer = signer
      end

      def upload(key, io, checksum: nil, **)
        with_io_length(io) do |io, length|
          request = Net::HTTP::Put.new("/#{key}")
          request.content_type = 'application/octet-stream'
          request['Content-MD5'] = checksum
          request['Content-Length'] = length
          request.body_stream = io
          @signer.sign(request, key)
          @http.request(request)
          # TODO: error handling
        end
      end

      def download(key, range: nil)
{'fraction_non_alphanumeric': 0.11832186168469354, 'fraction_numerical': 0.002622091117666339, 'mean_word_length': 2.472127417519909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4011858', 'n_tokens_mistral': 953, 'n_tokens_neox': 936, 'n_words': 204}