Class: GLPI::SDK::Session

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/glpi/sdk/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, app_token, username, password) ⇒ Session

Returns a new instance of Session.



8
9
10
11
12
13
14
# File 'lib/glpi/sdk/session.rb', line 8

def initialize(url, app_token, username, password)
  @url = url
  @app_token = app_token
  @auth = { username: username, password: password }

  init
end

Instance Attribute Details

#app_tokenObject

Returns the value of attribute app_token.



6
7
8
# File 'lib/glpi/sdk/session.rb', line 6

def app_token
  @app_token
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/glpi/sdk/session.rb', line 6

def token
  @token
end

Instance Method Details

#active_entitiesObject

Return active entities of current logged user



34
35
36
# File 'lib/glpi/sdk/session.rb', line 34

def active_entities
  request :get, '/getActiveEntities'
end

#active_profileObject

Return the current active profile



29
30
31
# File 'lib/glpi/sdk/session.rb', line 29

def active_profile
  request :get, '/getActiveProfile'
end

#full_sessionObject

Return the current php $_SESSION



39
40
41
# File 'lib/glpi/sdk/session.rb', line 39

def full_session
  request :get, '/getFullSession'
end

#glpi_configObject

Return the current $CFG_GLPI



44
45
46
# File 'lib/glpi/sdk/session.rb', line 44

def glpi_config
  request :get, '/getGlpiConfig'
end

#initObject

Request a session token to uses other api endpoints



17
18
19
20
# File 'lib/glpi/sdk/session.rb', line 17

def init
  response = request :get, '/initSession', basic_auth: @auth
  self.token = response['session_token']
end

#killObject

Destroy a session identified by a session token.



23
24
25
26
# File 'lib/glpi/sdk/session.rb', line 23

def kill
  request :get, '/killSession'
  self.token = nil
end

#request(method, endpoint, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/glpi/sdk/session.rb', line 48

def request(method, endpoint, options = {})
  options[:base_uri] = @url

  options[:headers] = options.fetch(:headers, {})
  options[:headers]['Content-Type'] = 'application/json'
  options[:headers]['App-Token'] = @app_token
  options[:headers]['Session-Token'] = @token if @token

  response = self.class.send(method, endpoint, options)

  unless response.success?
    type, message = response.parsed_response
    Error.dispatch(type, message)
  end

  response.parsed_response
end