Module: Jets::Gems::Api::Core
Instance Method Summary collapse
- #account ⇒ Object
- #build_request(klass, url, data = {}) ⇒ Object
- #delete(path, data = {}) ⇒ Object
- #get(path) ⇒ Object
- #http ⇒ Object
- #load_json(url, res) ⇒ Object
-
#ok?(http_code) ⇒ Boolean
Note: 422 is Unprocessable Entity.
- #patch(path, data = {}) ⇒ Object
- #post(path, data = {}) ⇒ Object
-
#request(klass, path, data = {}) ⇒ Object
Always translate raw json response to ruby Hash.
- #set_headers!(req) ⇒ Object
- #sts ⇒ Object
- #token ⇒ Object
-
#url(path) ⇒ Object
API does not include the /.
Instance Method Details
#account ⇒ Object
84 85 86 |
# File 'lib/jets/gems/api/core.rb', line 84 def account sts.get_caller_identity.account 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 |
#http ⇒ Object
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
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'] = account if account req['Content-Type'] = "application/vnd.api+json" end |
#sts ⇒ Object
89 90 91 |
# File 'lib/jets/gems/api/core.rb', line 89 def sts Aws::STS::Client.new end |
#token ⇒ Object
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 |