Class: TerraformEnterprise::API::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/terraform-enterprise/client/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, host: 'https://app.terraform.io/api/v2') ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
15
# File 'lib/terraform-enterprise/client/request.rb', line 8

def initialize(api_key:, host: 'https://app.terraform.io/api/v2')
  @base    = host
  @api_key = api_key
  @headers = {
    'Authorization' => "Bearer #{@api_key}",
    'Content-Type' => 'application/vnd.api+json'
  }
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



6
7
8
# File 'lib/terraform-enterprise/client/request.rb', line 6

def base
  @base
end

Instance Method Details

#delete(*path) ⇒ Object



22
23
24
25
# File 'lib/terraform-enterprise/client/request.rb', line 22

def delete(*path)
  data = path.pop if path.last.is_a?(Hash)
  request(:delete, path,data)
end

#get(*path) ⇒ Object



17
18
19
20
# File 'lib/terraform-enterprise/client/request.rb', line 17

def get(*path)
  data = path.pop if path.last.is_a?(Hash)
  request(:get, path, data)
end

#patch(*path) ⇒ Object



37
38
39
40
# File 'lib/terraform-enterprise/client/request.rb', line 37

def patch(*path)
  data = path.pop if path.last.is_a?(Hash)
  request(:patch,path,data)
end

#post(*path) ⇒ Object



27
28
29
30
# File 'lib/terraform-enterprise/client/request.rb', line 27

def post(*path)
  data = path.pop if path.last.is_a?(Hash)
  request(:post, path, data)
end

#put(*path) ⇒ Object



32
33
34
35
# File 'lib/terraform-enterprise/client/request.rb', line 32

def put(*path)
  data = path.pop if path.last.is_a?(Hash)
  request(:put,data,data)
end

#request(method, path, data = {}, headers = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/terraform-enterprise/client/request.rb', line 42

def request(method, path, data={}, headers={})
  request = {
    method:  method,
    url:     uri(path),
    headers: @headers.merge(headers || {})
  }
  if data
    if method==:get || method==:delete
      request[:headers][:params] = data
    else
      request[:payload] = data.is_a?(String) ? data : data.to_json
    end
  end

  response = begin
    RestClient::Request.execute(request)
  rescue RestClient::ExceptionWithResponse => ex
    ex.response
  end

  TerraformEnterprise::API::Response.new(response)
end