class Gitlab::Cli::Api

Public Class Methods

new(options) click to toggle source
# File lib/gitlab/cli/api.rb, line 9
def initialize(options)
  p "working with gitlab: #{options}"
  @api_url = options['api_url'] || raise("Make sure you provide value for 'api_url' in config.yml")
  @token = options['token'] if options['token']
end

Public Instance Methods

http_delete(api_path) click to toggle source
# File lib/gitlab/cli/api.rb, line 21
def http_delete(api_path)
  make_api_call(api_path, 'DELETE')
end
http_get(api_path) click to toggle source
# File lib/gitlab/cli/api.rb, line 15
def http_get(api_path)
  json_response = JSON.parse(make_api_call(api_path, 'GET').body)
  p "GET JSON Response : #{json_response}"
  json_response
end
http_post(api_path, body) click to toggle source
# File lib/gitlab/cli/api.rb, line 25
def http_post(api_path, body)
  make_api_call(api_path, 'POST', body)
end

Private Instance Methods

construct_request(uri, method, body = nil) click to toggle source
# File lib/gitlab/cli/api.rb, line 31
def construct_request(uri, method, body = nil)
  p "Making #{method} API call: #{uri}"

  case method
    when 'POST'
      request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
    when 'GET'
      request = Net::HTTP::Get.new(uri)
    when 'DELETE'
      request = Net::HTTP::Delete.new(uri)
    else
      raise "unknown http request type #{method}"
  end

  request['Private-Token'] = @token
  if body
    request.body = body
    p "Request body is:#{body}"
  end
  p "#{method} Request : #{request}"
  request
end
make_api_call(api_path, method, body = nil) click to toggle source
# File lib/gitlab/cli/api.rb, line 54
def make_api_call(api_path, method, body = nil)
  uri = URI.parse("#{@api_url}/#{api_path}")
  req_options = {
    use_ssl: uri.scheme == 'https'
  }

  request = construct_request(uri, method, body)

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  p "HTTP #{method} Response: #{response.code} => #{response}"
  response
end