Class: DIT3::Api::Client
- Inherits:
-
Object
- Object
- DIT3::Api::Client
- Defined in:
- lib/dit3/api/client.rb
Instance Method Summary collapse
- #accounts ⇒ Object
- #delete(endpoint) ⇒ Object
- #exec_request(method, endpoint) {|request| ... } ⇒ Object
- #get(endpoint) ⇒ Object
-
#initialize(host, client_id, client_secret) ⇒ Client
constructor
A new instance of Client.
- #post(endpoint, data) ⇒ Object
- #put(endpoint, data) ⇒ Object
- #tenants ⇒ Object
Constructor Details
#initialize(host, client_id, client_secret) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dit3/api/client.rb', line 12 def initialize(host, client_id, client_secret) @api = host + "/api" @oauth_client = OAuthClient.new(host, client_id, client_secret) @token = get_token @oauth_client @request_factory = { "GET" => method(:get_request), "POST" => method(:post_request), "DELETE" => method(:delete_request), "PUT" => method(:put_request) } end |
Instance Method Details
#accounts ⇒ Object
28 29 30 |
# File 'lib/dit3/api/client.rb', line 28 def accounts Wrappers::Accounts.new(self) end |
#delete(endpoint) ⇒ Object
68 69 70 |
# File 'lib/dit3/api/client.rb', line 68 def delete endpoint exec_request("DELETE", endpoint) end |
#exec_request(method, endpoint) {|request| ... } ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dit3/api/client.rb', line 32 def exec_request method, endpoint uri = URI(@api + endpoint) request = @request_factory[method].call(uri) http = get_http uri yield(request) if block_given? response = http.request request if (!response.kind_of? Net::HTTPSuccess) raise ApiError.new(response), "Failed to execute #{method} #{endpoint} request - #{response.code}" end if !response.body.nil? JSON.parse response.body else nil end end |
#get(endpoint) ⇒ Object
50 51 52 |
# File 'lib/dit3/api/client.rb', line 50 def get endpoint exec_request("GET", endpoint) end |
#post(endpoint, data) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/dit3/api/client.rb', line 54 def post endpoint, data exec_request("POST", endpoint) do |request| request['Content-Type'] = 'application/json' request.body = data.to_json end end |
#put(endpoint, data) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/dit3/api/client.rb', line 61 def put endpoint, data exec_request("PUT", endpoint) do |request| request['Content-Type'] = 'application/json' request.body = data.to_json end end |
#tenants ⇒ Object
24 25 26 |
# File 'lib/dit3/api/client.rb', line 24 def tenants Wrappers::Tenants.new(self) end |