<commit_before>require 'net/http/post/multipart'

class PlatformClient
  class << self
    def auth(email, password)
      response = HTTParty.post(File.join(Pakyow::Config.console.platform_url, 'auth/token'), body: {
        login: email,
        password: password,
      })

      if response.code == 200
        Hash.strhash(JSON.parse(response.body))
      end
    end
  end

  def initialize(email, token, info)
    @email = email
    @token = token
    @info  = info
  end

  def valid?
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects'), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    response.code == 200
  end

  def socket
    if app = @info[:app]
      response = HTTParty.post(File.join(Pakyow::Config.console.platform_url, 'api/projects', app[:id].to_s, 'socket'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      Hash.strhash(JSON.parse(response.body))
    else
      {}
    end
  end

  # TODO: refactor to `projects`
  def apps
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects'), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    JSON.parse(response.body).map { |app|
      # TODO: this blows up when we get a non-hash response back (e.g. in the event of a 401)
      Hash.strhash(app)
    }
  end

  # TODO: refactor to `project`
  def app(id)
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects', id.to_s), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    Hash.strhash(JSON.parse(response.body))
  end

  def events
    if app = @info[:project]
      response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, "api/projects/#{app[:id]}", 'events'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      JSON.parse(response.body).map { |event|
        event = Hash.strhash(event)
        event[:created_at] = DateTime.parse(event[:created_at]).to_time
        event
      }
    else
      []
    end
  end

  def collaborators
    if app = @info[:project]
      response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, "api/projects/#{app[:id]}", 'collaborators'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      JSON.parse(response.body).map { |app|
        Hash.strhash(app)
      }
    else
      []
    end
  end

  def releases
    if app = @info[:project]
      response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, "api/projects/#{app[:id]}", 'releases'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      JSON.parse(response.body).map { |release|
        release = Hash.strhash(release)
        release[:created_at] = DateTime.parse(release[:created_at]).to_time
        release
      }
    end
  end

  def create_release
    response = HTTParty.post(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'releases'), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    Hash.strhash(JSON.parse(response.body))
  end

  def update_release(id, body)
    response = HTTParty.patch(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'releases', id.to_s), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    }, body: { release: body })

    Hash.strhash(JSON.parse(response.body))
  end

  def file(id)
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files', id), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    response.body
  end

  def processed_file(id, params)
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files', id, 'processed'), body: params)
    response.body
  end

  def create_file(data, id: nil)
    uri = File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files')
    url = URI.parse(uri)

    data = if data.is_a?(String)
      StringIO.new(data)
    else
      data.open
      data
    end

    req = Net::HTTP::Post::Multipart.new url.path, "file" => UploadIO.new(data, 'image/jpeg', 'image.jpg'), "file_id" => id
    req.basic_auth @email, @token
    res = Net::HTTP.start(url.host, url.port) do |http|
      http.request(req)
    end
  end

  def remove_file(id)
    response = HTTParty.delete(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files', id), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })
  end
end<commit_msg>support ssl in file uploads<commit_after>require 'net/http/post/multipart'

class PlatformClient
  class << self
    def auth(email, password)
      response = HTTParty.post(File.join(Pakyow::Config.console.platform_url, 'auth/token'), body: {
        login: email,
        password: password,
      })

      if response.code == 200
        Hash.strhash(JSON.parse(response.body))
      end
    end
  end

  def initialize(email, token, info)
    @email = email
    @token = token
    @info  = info
  end

  def valid?
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects'), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    response.code == 200
  end

  def socket
    if app = @info[:app]
      response = HTTParty.post(File.join(Pakyow::Config.console.platform_url, 'api/projects', app[:id].to_s, 'socket'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      Hash.strhash(JSON.parse(response.body))
    else
      {}
    end
  end

  # TODO: refactor to `projects`
  def apps
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects'), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    JSON.parse(response.body).map { |app|
      # TODO: this blows up when we get a non-hash response back (e.g. in the event of a 401)
      Hash.strhash(app)
    }
  end

  # TODO: refactor to `project`
  def app(id)
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects', id.to_s), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    Hash.strhash(JSON.parse(response.body))
  end

  def events
    if app = @info[:project]
      response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, "api/projects/#{app[:id]}", 'events'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      JSON.parse(response.body).map { |event|
        event = Hash.strhash(event)
        event[:created_at] = DateTime.parse(event[:created_at]).to_time
        event
      }
    else
      []
    end
  end

  def collaborators
    if app = @info[:project]
      response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, "api/projects/#{app[:id]}", 'collaborators'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      JSON.parse(response.body).map { |app|
        Hash.strhash(app)
      }
    else
      []
    end
  end

  def releases
    if app = @info[:project]
      response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, "api/projects/#{app[:id]}", 'releases'), basic_auth: {
        username: @email,
        password: <PASSWORD>,
      })

      JSON.parse(response.body).map { |release|
        release = Hash.strhash(release)
        release[:created_at] = DateTime.parse(release[:created_at]).to_time
        release
      }
    end
  end

  def create_release
    response = HTTParty.post(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'releases'), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    Hash.strhash(JSON.parse(response.body))
  end

  def update_release(id, body)
    response = HTTParty.patch(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'releases', id.to_s), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    }, body: { release: body })

    Hash.strhash(JSON.parse(response.body))
  end

  def file(id)
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files', id), basic_auth: {
      username: @email,
      password: <PASSWORD>,
    })

    response.body
  end

  def processed_file(id, params)
    response = HTTParty.get(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files', id, 'processed'), body: params)
    response.body
  end

  def create_file(data, id: nil)
    uri = File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files')
    url = URI.parse(uri)

    data = if data.is_a?(String)
      StringIO.new(data)
    else
      data.open
      data
    end

    req = Net::HTTP::Post::Multipart.new url.path, "file" => UploadIO.new(data, 'image/jpeg', 'image.jpg'), "file_id" => id
    req.basic_auth @email, @token

    http = Net::HTTP.new(url.host, url.port)

    if uri.is_a?(URI::HTTPS)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    http.request(req)
  end

  def remove_file(id)
    response = HTTParty.delete(File.join(Pakyow::Config.console.platform_url, 'api/projects', @info[:project][:id].to_s, 'files', id), basic_auth: {
      username: @email,
      password: @<PASSWORD>,
    })
  end
end
{'fraction_non_alphanumeric': 0.15092967695024412, 'fraction_numerical': 0.001869741352446245, 'mean_word_length': 2.9703092783505154, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7234132', 'n_tokens_mistral': 3323, 'n_tokens_neox': 3230, 'n_words': 613}