Module: DomoscioAdmin

Defined in:
lib/domoscio_admin.rb,
lib/domoscio_admin/json.rb,
lib/domoscio_admin/errors.rb,
lib/domoscio_admin/version.rb,
lib/domoscio_admin/http_calls.rb,
lib/domoscio_admin/resources/resource.rb,
lib/domoscio_admin/authorization_token.rb,
lib/domoscio_admin/resources/users/user.rb,
lib/domoscio_admin/resources/resource_user.rb,
lib/domoscio_admin/resources/resource_instance.rb,
lib/domoscio_admin/resources/instances/instance_user.rb,
lib/domoscio_admin/resources/instances/student_group.rb,
lib/domoscio_admin/resources/instances/learning_program.rb,
lib/domoscio_admin/resources/instances/instance_parameter.rb

Defined Under Namespace

Modules: AuthorizationToken, HTTPCalls, JSON Classes: Configuration, Error, InstanceParameter, InstanceUser, LearningProgram, ProcessingError, Resource, ResourceInstance, ResourceUser, ResponseError, StudentGroup, User

Constant Summary collapse

VERSION =
'0.3.8'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



30
31
32
# File 'lib/domoscio_admin.rb', line 30

def configuration
  @configuration
end

Class Method Details

.api_uri(url = '') ⇒ Object



38
39
40
# File 'lib/domoscio_admin.rb', line 38

def self.api_uri(url = '')
  URI(configuration.root_url + url)
end

.configure {|configuration| ... } ⇒ Object

Yields:



33
34
35
36
# File 'lib/domoscio_admin.rb', line 33

def self.configure
  self.configuration ||= Configuration.new
  yield configuration
end

.perform_call(uri, method, params, headers) ⇒ Object

Actual HTTP call is performed here



129
130
131
132
133
134
135
# File 'lib/domoscio_admin.rb', line 129

def self.perform_call(uri, method, params, headers)
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
    req.body = DomoscioAdmin::JSON.dump(params)
    http.request req
  end
end

.raise_http_failure(uri, response, params) ⇒ Object

This helper will check the response status and build the correcponding DomoscioAdmin::ResponseError

Raises:



116
117
118
119
120
121
122
123
124
125
# File 'lib/domoscio_admin.rb', line 116

def self.raise_http_failure(uri, response, params)
  return if response.is_a? Net::HTTPSuccess

  raise ResponseError.new(
    uri,
    response.code.to_i,
    DomoscioAdmin::JSON.load((response.body.nil? ? '' : response.body), symbolize_keys: true),
    response.body, params
  )
end

.request(method, url, params = {}) ⇒ Object

  • method: HTTP method; lowercase symbol, e.g. :get, :post etc.

  • url: the part after Configuration#root_url

  • params: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body

Performs HTTP requests to Adaptive Engine On token issues, will try once to get a new token then will output a DomoscioAdmin::ReponseError with details

Raises DomoscioAdmin::ResponseError on Adaptive Error Status Raises DomoscioAdmin::ProcessingError on Internal Error



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/domoscio_admin.rb', line 53

def self.request(method, url, params = {})
  params ||= {}
  store_tokens, headers = request_headers
  params.merge!({ 'per_page': 2000 }) unless params[:per_page]
  uri = api_uri(url)
  response = DomoscioAdmin.send_request(uri, method, params, headers)
  return response if response.is_a? DomoscioAdmin::ProcessingError

  begin
    raise_http_failure(uri, response, params)

    if response['Content-Type'] == 'application/zip'
      data = response
    else
      data = DomoscioAdmin::JSON.load(response.body.nil? ? '' : response.body)
    end

    if store_tokens
      DomoscioAdmin::AuthorizationToken::Manager.storage.store({
                                                                 access_token: response['Accesstoken'],
                                                                 refresh_token: response['Refreshtoken']
                                                               })
    end
  rescue MultiJson::LoadError => e
    data = ProcessingError.new(uri, 500, e, response.body, params)
  rescue ResponseError => e
    data = e
  end

  if response['Total']
    pagetotal = (response['Total'].to_i / response['Per-Page'].to_f).ceil
    (2..pagetotal).each do |j|
      response = DomoscioAdmin.send_request(uri, method, params.merge({ page: j }), headers)
      return response if response.is_a? DomoscioAdmin::ProcessingError

      begin
        raise_http_failure(uri, response, params)
        body = DomoscioAdmin::JSON.load(response.body.nil? ? '' : response.body)
        data += body
        data.flatten!
      rescue MultiJson::LoadError => e
        return ProcessingError.new(uri, 500, e, response.body, params)
      rescue ResponseError => e
        return e
      end
    end
  end
  data
end

.request_headersObject

Process the token loading and analyze will return the processed headers and a token store flag



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/domoscio_admin.rb', line 169

def self.request_headers
  begin
    auth_token = DomoscioAdmin::AuthorizationToken::Manager.get_token
    if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
      [false, send_current_tokens(auth_token)]
    else
      [true, request_new_tokens]
    end
  rescue SyntaxError, StandardError
    [true, request_new_tokens]
  end
end

.request_new_tokensObject

If we cant find tokens of they are corrupted / expired, then we set headers to request new ones



195
196
197
198
199
200
201
202
# File 'lib/domoscio_admin.rb', line 195

def self.request_new_tokens
  {
    'user_agent' => DomoscioAdmin.user_agent,
    'ClientId' => DomoscioAdmin.configuration.client_id,
    'Authorization' => "Token token=#{DomoscioAdmin.configuration.client_passphrase}",
    'Content-Type' => 'application/json'
  }
end

.retry_call_and_store_tokens(uri, method, params) ⇒ Object

This method is called when AdaptiveEngine returns tokens errors Action on those errors is to retry and request new tokens, those new token are then stored



139
140
141
142
143
144
145
146
147
# File 'lib/domoscio_admin.rb', line 139

def self.retry_call_and_store_tokens(uri, method, params)
  headers = request_new_tokens
  response = perform_call(uri, method, params, headers)
  DomoscioAdmin::AuthorizationToken::Manager.storage.store({
                                                             access_token: response['Accesstoken'],
                                                             refresh_token: response['Refreshtoken']
                                                           })
  response
end

.send_current_tokens(auth_token) ⇒ Object

If stored token successfully loaded we build the header with them



184
185
186
187
188
189
190
191
192
# File 'lib/domoscio_admin.rb', line 184

def self.send_current_tokens(auth_token)
  {
    'user_agent' => DomoscioAdmin.user_agent,
    'ClientId' => DomoscioAdmin.configuration.client_id,
    'AccessToken' => auth_token[:access_token],
    'RefreshToken' => auth_token[:refresh_token],
    'Content-Type' => 'application/json'
  }
end

.send_request(uri, method, params, headers) ⇒ Object

This function catches usual Http errors during calls



105
106
107
108
109
110
111
112
# File 'lib/domoscio_admin.rb', line 105

def self.send_request(uri, method, params, headers)
  response = perform_call(uri, method, params, headers)
  response = retry_call_and_store_tokens(uri, method, params) if %w[401 403].include? response.code
  response
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNREFUSED, Errno::ECONNRESET,
       EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  ProcessingError.new(uri, 500, e, response, params)
end

.unameObject



160
161
162
163
164
# File 'lib/domoscio_admin.rb', line 160

def self.uname
  `uname -a 2>/dev/null` if RUBY_PLATFORM =~ /linux|darwin/i
rescue Errno::ENOMEM
  'uname lookup failed'
end

.user_agentObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/domoscio_admin.rb', line 149

def self.user_agent
  @uname ||= uname
  {
    bindings_version: DomoscioAdmin::VERSION,
    lang: 'ruby',
    lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
    platform: RUBY_PLATFORM,
    uname: @uname
  }.to_s
end