class Gitea::Repository::Contributors::GetService < Gitea::ClientService
  attr_reader :owner, :repo_name, :page, :limit

  def initialize(owner, repo_name, params={})
    @owner = owner 
    @repo_name = repo_name
    @page = params[:page] || 1
    @limit = params[:limit] || 20
  end

  def call
    response = get(url, params, true)
    render_result(response)
  end

  private 
  def params
    Hash.new.merge(token: owner.gitea_token, page: page, limit: limit)
  end

  def url 
    "/repos/#{owner.login}/#{repo_name}/contributors"
  end

  def render_result(response)
    case response.status
    when 200
      result = {}
      headers = response.headers.to_hash
      body = JSON.parse(response.body)
      total_count = headers["x-total"]
      result.merge(total_count: total_count.to_i, body: body)
    else
      nil
      # {status: -1, message: "#{body['message']}"}
    end
  end
end