Module: Datarobot::AiApi
- Defined in:
- lib/datarobot/ai_api.rb,
lib/datarobot/ai_api/ai.rb,
lib/datarobot/ai_api/page.rb,
lib/datarobot/ai_api/task.rb,
lib/datarobot/ai_api/output.rb,
lib/datarobot/ai_api/dataset.rb,
lib/datarobot/ai_api/deployment.rb,
lib/datarobot/ai_api/evaluation.rb,
lib/datarobot/ai_api/prediction.rb,
lib/datarobot/ai_api/learning_session.rb
Defined Under Namespace
Modules: Refreshable Classes: AI, ApiError, Dataset, Deployment, Evaluation, LearningSession, NotFoundError, Output, Page, Prediction, Task, UnauthorizedError
Constant Summary collapse
- @@api_key =
''- @@base_url =
'https://developers.datarobot.com'
Class Method Summary collapse
-
.add_headers(request, additional_headers = {}) ⇒ Net::Http::*
Adds default headers to a net http request.
- .api_key ⇒ Object
- .api_key=(v) ⇒ Object
-
.configure!(api_key: '', base_url: nil) ⇒ Datarobot::AiApi
Sets global API key.
-
.determine_error(response_obj) ⇒ Object
Uses request data to determin error classes Only raises values.
-
.determine_object(url) ⇒ Datarobot::AiApi::*
Given an arbitrary url, return the correct AiApi object.
-
.get(bare_uri, &block) ⇒ Object
Gets a bare uri.
-
.handle_response(response) {|data| ... } ⇒ Object
Common error handling and response parsing.
-
.ping ⇒ Hash
Checks the ‘ping` endpoint.
-
.ping_me ⇒ Hash
Checks the ‘ping/me` endpoint.
-
.request_endpoint(endpoint, method: 'get', body: {}, headers: {}, params: {}, &block) ⇒ Object
Makes a request to the given endpoint.
-
.upload_to_endpoint(endpoint, file_path, &block) ⇒ Object
Converts a file into an octet stream, and uploads it to given endpoint.
-
.with_https(uri, params = {}) {|uri, http| ... } ⇒ Object
Make a request with https.
Class Method Details
.add_headers(request, additional_headers = {}) ⇒ Net::Http::*
Adds default headers to a net http request
43 44 45 46 47 48 49 50 51 |
# File 'lib/datarobot/ai_api.rb', line 43 def self.add_headers(request, additional_headers={}) request["User-Agent"] = "datarobot-ai/ruby" request["Content-Type"] = "application/json" request["Authorization"] = "Bearer #{@@api_key}" additional_headers.each do |k, v| request[k] = v end request end |
.api_key ⇒ Object
15 |
# File 'lib/datarobot/ai_api.rb', line 15 def self.api_key; @@api_key end |
.api_key=(v) ⇒ Object
16 |
# File 'lib/datarobot/ai_api.rb', line 16 def self.api_key= v; @@api_key = v end |
.configure!(api_key: '', base_url: nil) ⇒ Datarobot::AiApi
Sets global API key
21 22 23 24 25 |
# File 'lib/datarobot/ai_api.rb', line 21 def self.configure!(api_key: '', base_url: nil) @@api_key = api_key @@base_url = base_url if base_url self end |
.determine_error(response_obj) ⇒ Object
Uses request data to determin error classes Only raises values. Will never return
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/datarobot/ai_api.rb', line 146 def self.determine_error(response_obj) parsed = JSON.parse(response_obj.body) msg = parsed["error"] || parsed["message"] case response_obj.code when "403" raise Datarobot::AiApi::, msg when "404" raise Datarobot::AiApi::NotFoundError, msg else raise Datarobot::AiApi::ApiError, msg end end |
.determine_object(url) ⇒ Datarobot::AiApi::*
Given an arbitrary url, return the correct AiApi object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/datarobot/ai_api.rb', line 162 def self.determine_object(url) case url when /\/ais/ Datarobot::AiApi::AI when /\/datasets/ Datarobot::AiApi::Dataset when /\/learningSessions/ Datarobot::AiApi::LearningSession when /\/outputs/ Datarobot::AiApi::Output when /\/status/ Datarobot::AiApi::Task when /\/deployment/ Datarobot::AiApi::Deployment end end |
.get(bare_uri, &block) ⇒ Object
Gets a bare uri. Useful for requests with ‘result` objects
72 73 74 75 76 77 78 79 80 |
# File 'lib/datarobot/ai_api.rb', line 72 def self.get(, &block) with_https() do |uri, http| request = Net::HTTP::Get.new(uri) request = add_headers(request) response = http.request(request) handle_response(response, &block) end end |
.handle_response(response) {|data| ... } ⇒ Object
Common error handling and response parsing
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/datarobot/ai_api.rb', line 101 def self.handle_response(response, &block) if response.code.to_i < 300 return nil if response.body.to_s.empty? data = JSON.parse(response.body) if block_given? yield data else data end else determine_error(response) end end |
.ping ⇒ Hash
Checks the ‘ping` endpoint. Useful for debugging
29 30 31 |
# File 'lib/datarobot/ai_api.rb', line 29 def self.ping request_endpoint('/aiapi/ping') end |
.ping_me ⇒ Hash
Checks the ‘ping/me` endpoint. Useful for debugging authentication
35 36 37 |
# File 'lib/datarobot/ai_api.rb', line 35 def self.ping_me request_endpoint('/aiapi/ping/me') end |
.request_endpoint(endpoint, method: 'get', body: {}, headers: {}, params: {}, &block) ⇒ Object
Makes a request to the given endpoint
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/datarobot/ai_api.rb', line 122 def self.request_endpoint(endpoint, method: 'get', body: {}, headers: {}, params: {}, &block) uri_str = "#{@@base_url}#{endpoint}" with_https(uri_str, params) do |uri, http| case method.downcase when 'put' request = Net::HTTP::Put.new(uri.request_uri) request.body = JSON.dump(body) when 'delete' request = Net::HTTP::Delete.new(uri.request_uri) when 'post' request = Net::HTTP::Post.new(uri.request_uri) request.body = JSON.dump(body) else request = Net::HTTP::Get.new(uri) end request = add_headers(request, headers) response = http.request(request) handle_response(response, &block) end end |
.upload_to_endpoint(endpoint, file_path, &block) ⇒ Object
Converts a file into an octet stream, and uploads it to given endpoint
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/datarobot/ai_api.rb', line 86 def self.upload_to_endpoint(endpoint, file_path, &block) uri = "#{@@base_url}#{endpoint}" with_https(uri) do |uri, http| request = Net::HTTP::Post::Multipart.new uri.request_uri, "file" => UploadIO.new(file_path, "application/octet-stream") request["User-Agent"] = "datarobot-ai/ruby" request["Authorization"] = "Bearer #{@@api_key}" response = http.request(request) handle_response(response, &block) end end |
.with_https(uri, params = {}) {|uri, http| ... } ⇒ Object
Make a request with https. This is a helper method to avoid having to write net/http boilerplate overwrite params in the query string of the given uri
60 61 62 63 64 65 66 67 |
# File 'lib/datarobot/ai_api.rb', line 60 def self.with_https(uri, params={}) uri = URI.parse(uri) existing_params = CGI::parse(uri.query.to_s) uri.query = URI.encode_www_form(existing_params.merge params) Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| yield uri, http end end |