Class: DIT3::Api::Client
- Inherits:
-
Object
- Object
- DIT3::Api::Client
- Defined in:
- lib/dit3/api/client.rb
Instance Method Summary collapse
- #get(endpoint) ⇒ Object
-
#initialize(host, client_id, client_secret, login, password) ⇒ Client
constructor
A new instance of Client.
- #post(endpoint, data) ⇒ Object
- #tenants ⇒ Object
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, login, password) @api = host + "/api" @oauth_client = OAuthClient.new(host, client_id, client_secret) @token = get_token @oauth_client, login, 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) return nil 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 |
# 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 |