<commit_before>class OsApiController < ::AjaxController

  # Example how to use in browser
  # fetch("os-api/SERVICE_NAME/SERVICE_PATH?headers=HEADERS_AS_JSON")

  # This method implements a reverse proxy to openstack API
  def reverse_proxy 
    # get http method from request
    method = request.method.downcase
    # get path from request
    path = params[:path]
    # we remove the first part of path. It is the openstack service name
    path_tokens = path.split("/")
    service_name = path_tokens.shift
    # the rest is the current path
    path = path_tokens.join("/")

    headers = {"Content-Type" => request.headers["Content-Type"] || "application/json"}
    request.headers.each do |name,value| 
      if name.start_with?("HTTP_OS_API") 
        headers[name.gsub("HTTP_OS_API_","").gsub("_","-")] = value 
      end
    end
    
    # get api client for the given service name
    service = services.os_api.service(service_name)

    # filter the relevant params for the api client
    elektronParams = request.query_parameters
    # call the openstack api endpoint with given path, params and headers
    # for http methods POST, PUT, PATCH we have to consider the body parameter
    response = if ["post","put","patch"].include?(method)
      body = JSON.parse(request.body.read ) rescue request.body.read 
      service.public_send(method,path,elektronParams, headers: headers) do 
        body 
      end.body      
    else
      # GET, HEAD, DELETE case
      service.public_send(method,path, elektronParams, headers: headers).body      
    end
    
    # byebug
    # render response as json
    render json: response
  end
end<commit_msg>os-api: reverse proxy headers from openstack API<commit_after>class OsApiController < ::AjaxController

  # Example how to use in browser
  # fetch("os-api/SERVICE_NAME/SERVICE_PATH?headers=HEADERS_AS_JSON")

  # This method implements a reverse proxy to openstack API
  def reverse_proxy 
    # get http method from request
    method = request.method.downcase
    # get path from request
    path = params[:path]
    # we remove the first part of path. It is the openstack service name
    path_tokens = path.split("/")
    service_name = path_tokens.shift
    # the rest is the current path
    path = path_tokens.join("/")

    headers = {"Content-Type" => request.headers["Content-Type"] || "application/json"}
    request.headers.each do |name,value| 
      if name.start_with?("HTTP_OS_API") 
        headers[name.gsub("HTTP_OS_API_","").gsub("_","-")] = value 
      end
    end
    
    # get api client for the given service name
    service = services.os_api.service(service_name)
    # filter the relevant params for the api client
    elektron_params = request.query_parameters
    # call the openstack api endpoint with given path, params and headers
    # for http methods POST, PUT, PATCH we have to consider the body parameter
    elektron_response = if ["post","put","patch"].include?(method)
      body = JSON.parse(request.body.read ) rescue request.body.read 
      service.public_send(method,path,elektron_params, headers: headers) do 
        body 
      end      
    else
      # GET, HEAD, DELETE case
      service.public_send(method,path, elektron_params, headers: headers)   
    end
    
    # render response as json
    elektron_response.header.each_header do |key, value|
      response.headers[key] = value if key.start_with? "x-"
    end
    render json: elektron_response.body
  end
end
{'fraction_non_alphanumeric': 0.09296987087517934, 'fraction_numerical': 0.0, 'mean_word_length': 3.1352313167259784, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3501369', 'n_tokens_mistral': 1022, 'n_tokens_neox': 1000, 'n_words': 312}