Class: KeycloakAdmin::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/keycloak-admin/client/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



4
5
6
# File 'lib/keycloak-admin/client/client.rb', line 4

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#create_payload(value) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/keycloak-admin/client/client.rb', line 40

def create_payload(value)
  if value.nil?
    ""
  elsif value.kind_of?(Array)
    "[#{value.map(&:to_json) * ","}]"
  else
    value.to_json
  end
end

#created_id(response) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/keycloak-admin/client/client.rb', line 32

def created_id(response)
  unless response.net_http_res.is_a? Net::HTTPCreated
    raise "Create method returned status #{response.net_http_res.message} (Code: #{response.net_http_res.code}); expected status: Created (201)"
  end
  (_head, _separator, id) = response.headers[:location].rpartition("/")
  id
end

#current_tokenObject



12
13
14
# File 'lib/keycloak-admin/client/client.rb', line 12

def current_token
  @current_token ||= KeycloakAdmin.create_client(@configuration, @configuration.client_realm_name).token.get
end

#execute_httpObject



24
25
26
27
28
29
30
# File 'lib/keycloak-admin/client/client.rb', line 24

def execute_http
  yield
rescue RestClient::Exceptions::Timeout => e
  raise
rescue RestClient::ExceptionWithResponse => e
  http_error(e.response)
end

#headersObject



16
17
18
19
20
21
22
# File 'lib/keycloak-admin/client/client.rb', line 16

def headers
  {
    Authorization: "Bearer #{current_token.access_token}",
    content_type: :json,
    accept:       :json
  }
end

#server_urlObject



8
9
10
# File 'lib/keycloak-admin/client/client.rb', line 8

def server_url
  @configuration.server_url
end