<commit_before>Request = require "./request"
{type} = require "fairmont"
{Evie} = require "evie"

module.exports = class Action

  constructor: (@client, @name, @definition) ->
    {@api} = @client

    {request, response} = @definition
    @status = response?.status || 200
    if request?.type?
      @request_schema = @api.schema_manager.find mediaType: request.type

    if response?.type?
      @response_schema = @api.schema_manager.find mediaType: response.type

    @_base_headers = @base_headers(@definition)


  base_headers: ->
    headers =
      "User-Agent": "patchboard-js"

    # FIXME: We should probably always set these headers when the 
    # definition includes response and request types.
    if @request_schema?
      headers["Content-Type"] = @request_schema.mediaType

    # FIXME:  we should also check for definition.accept
    if @response_schema?
      headers["Accept"] = @response_schema.mediaType

    headers


  create_request: (resource, url, args...) ->
    options = @process_args(args)
    request =
      url: url
      method: @definition.method
      headers: {}

    if (body = @prepare_body(options))?
      request.body = body

    for key, value of @_base_headers
      request.headers[key] = value

    auth_type = @definition.request?.authorization
    if auth_type? && @client.context.authorizer?
      credential = @client.context.authorizer(schemes, @name)
      request.headers["Authorization"] = "#{auth_type} #{credential}"<commit_msg>request.authorization may be a string or an array<commit_after>Request = require "./request"
{type} = require "fairmont"
{Evie} = require "evie"

module.exports = class Action

  constructor: (@client, @name, @definition) ->
    {@api} = @client

    {request, response} = @definition
    @status = response?.status || 200
    if request?.type?
      @request_schema = @api.schema_manager.find mediaType: request.type

    if response?.type?
      @response_schema = @api.schema_manager.find mediaType: response.type

    @_base_headers = @base_headers(@definition)


  base_headers: ->
    headers =
      "User-Agent": "patchboard-js"

    # FIXME: We should probably always set these headers when the 
    # definition includes response and request types.
    if @request_schema?
      headers["Content-Type"] = @request_schema.mediaType

    # FIXME:  we should also check for definition.accept
    if @response_schema?
      headers["Accept"] = @response_schema.mediaType

    headers


  create_request: (resource, url, args...) ->
    options = @process_args(args)
    request =
      url: url
      method: @definition.method
      headers: {}

    if (body = @prepare_body(options))?
      request.body = body

    for key, value of @_base_headers
      request.headers[key] = value

    schemes = @definition.request?.authorization
    if schemes?.constructor == String
      schemes = [schemes]

    if schemes? && @client.context.authorizer?
      result = @client.context.authorizer(schemes, @name)
      if result?
        {scheme, credential} = result
        request.headers["Authorization"] = "#{scheme} #{credential}"
{'fraction_non_alphanumeric': 0.11943643932116554, 'fraction_numerical': 0.0019212295869356388, 'mean_word_length': 3.2330623306233064, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, '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': '7202937', 'n_tokens_mistral': 922, 'n_tokens_neox': 890, 'n_words': 234}