Module: Jets::Gems::Api::Core

Extended by:
Memoist
Included in:
Jets::Gems::Api
Defined in:
lib/jets/gems/api/core.rb

Instance Method Summary collapse

Instance Method Details

#accountObject



84
85
86
# File 'lib/jets/gems/api/core.rb', line 84

def 
  sts.get_caller_identity. rescue nil
end

#build_request(klass, url, data = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/jets/gems/api/core.rb', line 15

def build_request(klass, url, data={})
  req = klass.new(url) # url includes query string and uri.path does not, must used url
  set_headers!(req)
  if [Net::HTTP::Delete, Net::HTTP::Patch, Net::HTTP::Post, Net::HTTP::Put].include?(klass)
    text = JSON.dump(data)
    req.body = text
    req.content_length = text.bytesize
  end
  req
end

#delete(path, data = {}) ⇒ Object



80
81
82
# File 'lib/jets/gems/api/core.rb', line 80

def delete(path, data={})
  request(Net::HTTP::Delete, path, data)
end

#get(path) ⇒ Object



68
69
70
# File 'lib/jets/gems/api/core.rb', line 68

def get(path)
  request(Net::HTTP::Get, path)
end

#httpObject



54
55
56
57
58
59
60
# File 'lib/jets/gems/api/core.rb', line 54

def http
  uri = URI(endpoint)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = http.read_timeout = 30
  http.use_ssl = true if uri.scheme == 'https'
  http
end

#load_json(url, res) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jets/gems/api/core.rb', line 36

def load_json(url, res)
  uri = URI(url)
  if ok?(res.code)
    JSON.load(res.body)
  else
    puts "Error: Non-successful http response status code: #{res.code}"
    puts "headers: #{res.each_header.to_h.inspect}"
    puts "ServerlessGems API #{url}" if ENV['SG_DEBUG']
    raise "ServerlessGems API called failed: #{uri.host}"
  end
end

#ok?(http_code) ⇒ Boolean

Note: 422 is Unprocessable Entity. This means an invalid data payload was sent. We want that to error and raise

Returns:

  • (Boolean)


50
51
52
# File 'lib/jets/gems/api/core.rb', line 50

def ok?(http_code)
  http_code =~ /^20/ || http_code =~ /^40/
end

#patch(path, data = {}) ⇒ Object



76
77
78
# File 'lib/jets/gems/api/core.rb', line 76

def patch(path, data={})
  request(Net::HTTP::Patch, path, data)
end

#post(path, data = {}) ⇒ Object



72
73
74
# File 'lib/jets/gems/api/core.rb', line 72

def post(path, data={})
  request(Net::HTTP::Post, path, data)
end

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

Always translate raw json response to ruby Hash



8
9
10
11
12
13
# File 'lib/jets/gems/api/core.rb', line 8

def request(klass, path, data={})
  url = url(path)
  req = build_request(klass, url, data)
  resp = http.request(req) # send request
  load_json(url, resp)
end

#set_headers!(req) ⇒ Object



26
27
28
29
30
# File 'lib/jets/gems/api/core.rb', line 26

def set_headers!(req)
  req['Authorization'] = token if token
  req['x-account'] =  if 
  req['Content-Type'] = "application/vnd.api+json"
end

#stsObject



89
90
91
# File 'lib/jets/gems/api/core.rb', line 89

def sts
  Aws::STS::Client.new
end

#tokenObject



32
33
34
# File 'lib/jets/gems/api/core.rb', line 32

def token
  Jets::Gems::Config.instance.data['key']
end

#url(path) ⇒ Object

API does not include the /. IE: app.terraform.io/api/v2



64
65
66
# File 'lib/jets/gems/api/core.rb', line 64

def url(path)
  "#{endpoint}/#{path}"
end