module WolfCore::HttpOperations

def async_http_get(**args)

def async_http_get(**args)
  log_object "starting async_http_get"
  log_object args, title: "async_http_get args are"
  run_async do
    response = http_get(**args)
    log_object parse_http_response(response), title: "async_http_get response is"
  end
end

def async_http_patch(**args)

def async_http_patch(**args)
  log_object "starting async_http_patch"
  log_object args, title: "async_http_patch args are"
  run_async do
    response = http_patch(**args)
    log_object parse_http_response(response), title: "async_http_patch response is"
  end
end

def async_http_post(**args)

def async_http_post(**args)
  log_object "starting async_http_post"
  log_object args, title: "async_http_post args are"
  run_async do
    response = http_post(**args)
    log_object parse_http_response(response), title: "async_http_post response is"
  end
end

def async_http_put(**args)

def async_http_put(**args)
  log_object "starting async_http_put"
  log_object args, title: "async_http_put args are"
  run_async do
    response = http_put(**args)
    log_object parse_http_response(response), title: "async_http_put response is"
  end
end

def http_get(url:, headers: {}, query: nil)

def http_get(url:, headers: {}, query: nil)
  WolfCore::HttpDataSource.http_get(url: url, headers: headers, query: query)
end

def http_patch(url:, headers: {}, query: nil)

def http_patch(url:, headers: {}, query: nil)
  WolfCore::HttpDataSource.http_patch(url: url, headers: headers, query: query)
end

def http_post(url:, headers: {}, body: nil, query: nil)

def http_post(url:, headers: {}, body: nil, query: nil)
  WolfCore::HttpDataSource.http_post(url: url, headers: headers, query: query, body: body)
end

def http_put(url:, headers: {}, body: nil, query: nil)

def http_put(url:, headers: {}, body: nil, query: nil)
  WolfCore::HttpDataSource.http_put(url: url, headers: headers, query: query, body: body)
end

def parse_http_response(response)

def parse_http_response(response)
  body = begin
    JSON.parse(response.body)
  rescue StandardError
    response.body
  end
  OpenStruct.new({
                   code: response.code,
                   body: body,
                   message: response.message
                 })
end

def parsed_http_get(url:, headers: {}, query: nil)

def parsed_http_get(url:, headers: {}, query: nil)
  response = http_get(url: url, headers: headers, query: query)
  parse_http_response(response)
end

def parsed_http_patch(url:, headers: {}, query: nil)

def parsed_http_patch(url:, headers: {}, query: nil)
  response = http_patch(url: url, headers: headers, query: query)
  parse_http_response(response)
end

def parsed_http_post(url:, body: nil, headers: {}, query: nil)

def parsed_http_post(url:, body: nil, headers: {}, query: nil)
  response = http_post(url: url, headers: headers, query: query, body: body)
  parse_http_response(response)
end

def parsed_http_put(url:, body: nil, headers: {}, query: nil)

def parsed_http_put(url:, body: nil, headers: {}, query: nil)
  response = http_put(url: url, headers: headers, query: query, body: body)
  parse_http_response(response)
end

def safe_http_get(url:, headers: {}, query: nil, error_message: nil, title: nil)

def safe_http_get(url:, headers: {}, query: nil, error_message: nil, title: nil)
  response = http_get(url: url, headers: headers, query: query)
  response = parse_http_response(response)
  log_object response, title: title if title.present?
  error_message ||= "Error on safe_http_get"
  validate_http_response(response: response, message: error_message)
  response
end

def safe_http_patch(url:, headers: {}, query: nil, error_message: nil, title: nil)

def safe_http_patch(url:, headers: {}, query: nil, error_message: nil, title: nil)
  response = http_patch(url: url, headers: headers, query: query)
  response = parse_http_response(response)
  log_object response, title: title if title.present?
  error_message ||= "Error on safe_http_patch"
  validate_http_response(response: response, message: error_message)
  response
end

def safe_http_post(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil, error_data: nil)

def safe_http_post(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil, error_data: nil)
  response = http_post(url: url, headers: headers, body: body, query: query)
  response = parse_http_response(response)
  title ||= "safe_http_post response is"
  log_object response, title: title
  error_message ||= "Error on safe_http_post"
  validate_http_response(response: response, message: error_message, error_data: error_data)
  response
end

def safe_http_put(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil, error_data: nil)

def safe_http_put(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil, error_data: nil)
  response = http_put(url: url, headers: headers, body: body, query: query)
  response = parse_http_response(response)
  title ||= "safe_http_put response is"
  log_object response, title: title
  error_message ||= "Error on safe_http_put"
  validate_http_response(response: response, message: error_message, error_data: error_data)
  response
end

def validate_http_response(response:, message:, error_data: nil)

def validate_http_response(response:, message:, error_data: nil)
  return if response.code == 200
  error_data = {
    message: message,
    response: parse_http_response(response)
  }.merge(error_data || {})
  raise_service_error(error_data)
end