Class: BatchlyApi::DataSourcesController

Inherits:
Object
  • Object
show all
Defined in:
lib/batchly_api/controllers/data_sources_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_s_3(model) ⇒ Object

Adds a new S3 datasource to batchly.

Parameters:

Returns:

  • String response from the API call



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/batchly_api/controllers/data_sources_controller.rb', line 9

def add_s_3 model
  # the base uri for api requests

  query_builder = Configuration.BASE_URI.dup

  # prepare query string for API call

  query_builder << "/api/DataSources/S3"

  # validate and preprocess url

  query_url = APIHelper.clean_url query_builder

  # prepare headers

  headers = {
    "user-agent" => "batchly/1.0.1",
    "content-type" => "application/json; charset=utf-8"
  }

  # append custom auth authorization

  CustomAuthUtility.append_custom_auth_params query_url, "POST", headers

  # invoke the API call request to fetch the response

  response = Unirest.post query_url, headers:headers, parameters:model.to_json

  # Error handling using HTTP status codes

  if response.code == 400
    raise APIException.new "BadRequest", 400, response.raw_body
  elsif !(response.code.between?(200,206)) # [200,206] = HTTP OK

    raise APIException.new "HTTP Response Not OK", response.code, response.raw_body
  end

  response.body
end