class DIT3::Api::Client
Public Class Methods
new(host, client_id, client_secret, login, password)
click to toggle source
# File lib/dit3/api/client.rb, line 11 def initialize(host, client_id, client_secret, login, password) @api = host + "/api" @oauth_client = OAuthClient.new(host, client_id, client_secret) @token = get_token @oauth_client, login, password end
Public Instance Methods
get(endpoint)
click to toggle source
# File lib/dit3/api/client.rb, line 21 def get endpoint uri = URI(@api + endpoint) request = get_request uri http = get_http uri response = http.request request if (!response.kind_of? Net::HTTPSuccess) return nil end if request.body_permitted? JSON.parse request.body else nil end end
post(endpoint, data)
click to toggle source
# File lib/dit3/api/client.rb, line 37 def post endpoint, data uri = URI(@api + endpoint) request = post_request uri http = get_http uri request['Content-Type'] = 'application/json' request.body = data.to_json response = http.request request if (!response.kind_of? Net::HTTPSuccess) raise ApiError, "Failed to execute POST #{endpoint} request - #{request.code}" end JSON.parse request.body end
tenants()
click to toggle source
# File lib/dit3/api/client.rb, line 17 def tenants Wrappers::Tenants.new(self) end
Private Instance Methods
get_http(endpoint_uri)
click to toggle source
# File lib/dit3/api/client.rb, line 71 def get_http endpoint_uri http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port) if (endpoint_uri.scheme == 'https') http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end http end
get_request(endpoint_uri)
click to toggle source
# File lib/dit3/api/client.rb, line 63 def get_request endpoint_uri Net::HTTP::Get.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}") end
get_token(oauth_client, login, password)
click to toggle source
# File lib/dit3/api/client.rb, line 54 def get_token oauth_client, login, password response = oauth_client.password.get_token(login, password) if (!response.kind_of? Net::HTTPSuccess) raise ApiError, "Failed to get access token: #{response.body}" end response_body = JSON.parse(response.body) response_body['access_token'] end
post_request(endpoint_uri)
click to toggle source
# File lib/dit3/api/client.rb, line 67 def post_request endpoint_uri Net::HTTP::Post.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}") end