<commit_before>ENV['RACK_ENV'] ||= 'development'

require 'rubygems'
require 'sinatra/base'
require 'json'
require 'pp'
require 'logger'
require 'colorize'

require 'bundler'
Bundler.require :default, ENV['RACK_ENV'].to_sym

$stdout.sync = true
$stderr.sync = true

class ServiceInstance
  attr_reader :provision_data, :fetch_count, :deleted

  def initialize(opts={})
    @provision_data = opts.fetch(:provision_data)
    @fetch_count = opts.fetch(:fetch_count, 0)
    @deleted = opts.fetch(:deleted, false)
  end

  def plan_id
    @provision_data['plan_id']
  end

  def update!(updated_data)
    @provision_data.merge!(updated_data)
    @fetch_count = 0
    self
  end

  def delete!
    @deleted = true
    @fetch_count = 0
    self
  end

  def increment_fetch_count
    @fetch_count += 1
  end

  def to_json(opts={})
    {
      provision_data: provision_data,
      fetch_count: fetch_count,
      deleted: deleted
    }.to_json(opts)
  end
end

class DataSource
  attr_reader :data

  def initialize(data = nil)
    @data = data || JSON.parse(File.read(File.absolute_path('data.json')))
  end

  def max_fetch_service_instance_requests
    @data['max_fetch_service_instance_requests'] || 1
  end

  def service_instance_by_id(cc_id)
    @data['service_instances'][cc_id]
  end

  def create_service_instance(cc_id, json_data)
    service_instance = ServiceInstance.new(
      provision_data: json_data,
    )

    @data['service_instances'][cc_id] = service_instance

    service_instance
  end

  def create_service_binding(instance_id, binding_id, binding_data)
    @data['service_instances'][binding_id] = {
      'binding_data' => binding_data,
      'instance_id' => instance_id,
    }
  end

  def delete_service_binding(binding_id)
    @data['service_instances'].delete(binding_id)
  end

  def merge!(data)
    data = data.dup
    data['service_instances'] = data.fetch('service_instances', {}).inject({}) do |service_instances, (guid, instance_data)|
      symbolized_data = instance_data.inject({}) do |memo,(k,v)|
        memo[k.to_sym] = v
        memo
      end

      service_instances[guid] = ServiceInstance.new(symbolized_data)
      service_instances
    end

    data.each_pair do |key, value|
      if @data[key] && @data[key].is_a?(Hash)
        @data[key].merge!(value)
      else
        @data[key] = value
      end
    end
  end

  def without_instances_or_bindings
    @data.reject { |key| %w(service_instances service_bindings).include?(key) }
  end

  def behavior_for_type(type, plan_id)
    plans_or_default_behavior = @data['behaviors'][type.to_s]

    return plans_or_default_behavior if type == :catalog

    raise "Behavior object is missing key: #{type} (tried to lookup plan_id #{plan_id})" unless plans_or_default_behavior

    if plan_id && plans_or_default_behavior.has_key?(plan_id)
      plans_or_default_behavior[plan_id]
    else
      $log.info("Could not find response for plan id: #{plan_id}")
      return plans_or_default_behavior['default'] if plans_or_default_behavior['default']
      raise "Behavior for #{type} is missing response for plan_id #{plan_id} and default response."
    end
  end
end

class ServiceBroker < Sinatra::Base
  set :logging, true

  configure :production, :development, :test do
    $datasource = DataSource.new
    $log = Logger.new(STDOUT)
    $log.level = Logger::INFO
    $log.formatter = proc do |severity, datetime, progname, msg|
      "#{severity}: #{msg}\n"
    end
  end

  def log(request)
    $log.info "#{request.env['REQUEST_METHOD']} #{request.env['PATH_INFO']} #{request.env['QUERY_STRING']}".yellow
    request.body.rewind
    $log.info "Request body: #{request.body.read}".yellow
    request.body.rewind
  end

  def log_response(status, body)
    $log.info "Response: status=#{status}, body=#{body}".green
    body
  end

  def respond_with_behavior(behavior, accepts_incomplete=false)
    sleep behavior['sleep_seconds']

    if behavior['async_only'] && !accepts_incomplete
      respond_async_required
    else
      respond_from_config(behavior)
    end
  end

  def respond_async_required
    status 422
    log_response(status, {
      'error' => 'AsyncRequired',
      'description' => 'This service plan requires client support for asynchronous service operations.'
     }.to_json)
  end<commit_msg>CATS broker logs request headers<commit_after>ENV['RACK_ENV'] ||= 'development'

require 'rubygems'
require 'sinatra/base'
require 'json'
require 'pp'
require 'logger'
require 'colorize'

require 'bundler'
Bundler.require :default, ENV['RACK_ENV'].to_sym

$stdout.sync = true
$stderr.sync = true

class ServiceInstance
  attr_reader :provision_data, :fetch_count, :deleted

  def initialize(opts={})
    @provision_data = opts.fetch(:provision_data)
    @fetch_count = opts.fetch(:fetch_count, 0)
    @deleted = opts.fetch(:deleted, false)
  end

  def plan_id
    @provision_data['plan_id']
  end

  def update!(updated_data)
    @provision_data.merge!(updated_data)
    @fetch_count = 0
    self
  end

  def delete!
    @deleted = true
    @fetch_count = 0
    self
  end

  def increment_fetch_count
    @fetch_count += 1
  end

  def to_json(opts={})
    {
      provision_data: provision_data,
      fetch_count: fetch_count,
      deleted: deleted
    }.to_json(opts)
  end
end

class DataSource
  attr_reader :data

  def initialize(data = nil)
    @data = data || JSON.parse(File.read(File.absolute_path('data.json')))
  end

  def max_fetch_service_instance_requests
    @data['max_fetch_service_instance_requests'] || 1
  end

  def service_instance_by_id(cc_id)
    @data['service_instances'][cc_id]
  end

  def create_service_instance(cc_id, json_data)
    service_instance = ServiceInstance.new(
      provision_data: json_data,
    )

    @data['service_instances'][cc_id] = service_instance

    service_instance
  end

  def create_service_binding(instance_id, binding_id, binding_data)
    @data['service_instances'][binding_id] = {
      'binding_data' => binding_data,
      'instance_id' => instance_id,
    }
  end

  def delete_service_binding(binding_id)
    @data['service_instances'].delete(binding_id)
  end

  def merge!(data)
    data = data.dup
    data['service_instances'] = data.fetch('service_instances', {}).inject({}) do |service_instances, (guid, instance_data)|
      symbolized_data = instance_data.inject({}) do |memo,(k,v)|
        memo[k.to_sym] = v
        memo
      end

      service_instances[guid] = ServiceInstance.new(symbolized_data)
      service_instances
    end

    data.each_pair do |key, value|
      if @data[key] && @data[key].is_a?(Hash)
        @data[key].merge!(value)
      else
        @data[key] = value
      end
    end
  end

  def without_instances_or_bindings
    @data.reject { |key| %w(service_instances service_bindings).include?(key) }
  end

  def behavior_for_type(type, plan_id)
    plans_or_default_behavior = @data['behaviors'][type.to_s]

    return plans_or_default_behavior if type == :catalog

    raise "Behavior object is missing key: #{type} (tried to lookup plan_id #{plan_id})" unless plans_or_default_behavior

    if plan_id && plans_or_default_behavior.has_key?(plan_id)
      plans_or_default_behavior[plan_id]
    else
      $log.info("Could not find response for plan id: #{plan_id}")
      return plans_or_default_behavior['default'] if plans_or_default_behavior['default']
      raise "Behavior for #{type} is missing response for plan_id #{plan_id} and default response."
    end
  end
end

class ServiceBroker < Sinatra::Base
  set :logging, true

  configure :production, :development, :test do
    $datasource = DataSource.new
    $log = Logger.new(STDOUT)
    $log.level = Logger::INFO
    $log.formatter = proc do |severity, datetime, progname, msg|
      "#{severity}: #{msg}\n"
    end
  end

  def log(request)
    $log.info "#{request.env['REQUEST_METHOD']} #{request.env['PATH_INFO']} #{request.env['QUERY_STRING']}".yellow
    request.body.rewind
    headers = find_headers(request)
    $log.info "Request headers: #{headers}".light_blue
    $log.info "Request body: #{request.body.read}".yellow
    request.body.rewind
  end

  def find_headers(request)
    request.env.select { |key, _| key =~ /HTTP/ }
  end

  def log_response(status, body)
    $log.info "Response: status=#{status}, body=#{body}".green
    body
  end

  def respond_with_behavior(behavior, accepts_incomplete=false)
    sleep behavior['sleep_seconds']

    if behavior['async_only'] && !accepts_incomplete
      respond_async_required
    else
      respond_from_config(behavior)
    end
  end

  def respond_async_required
    status 422
    log_response(status, {
      'error' => 'AsyncRequired',
      'description' => 'This service plan requires client support for asynchronous service operations.'
     }.to_json)
  end
{'fraction_non_alphanumeric': 0.11395849869599728, 'fraction_numerical': 0.0018142646558566731, 'mean_word_length': 3.7934782608695654, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '1871510', 'n_tokens_mistral': 2985, 'n_tokens_neox': 2904, 'n_words': 624}