Class: DIT3::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dit3/api/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, client_id, client_secret, login, password) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/dit3/api/client.rb', line 11

def initialize(host, client_id, client_secret, , password)
    @api = host + "/api"
    @oauth_client = OAuthClient.new(host, client_id, client_secret)
    @token = get_token @oauth_client, , password
end

Instance Method Details

#get(endpoint) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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)
        raise ApiError, "Failed to execute GET #{endpoint} request - #{request.code}"
    end
    if request.body_permitted?
        JSON.parse request.body
    else
        nil
    end
end

#post(endpoint, data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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
    if request.body_permitted?
        JSON.parse request.body
    else
        nil
    end
end

#tenantsObject



17
18
19
# File 'lib/dit3/api/client.rb', line 17

def tenants
    Wrappers::Tenants.new(self)
end