Class: Apologist::Users::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/Apologist/users/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void



9
10
11
# File 'lib/Apologist/users/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#get_user(request_options: {}, **params) ⇒ Apologist::Users::Types::GetUserResponse

Returns a single user by external id or internal id, with expanded tags and the persisted responder for the agent.

Examples:

client.users.get_user(user_id: "user_id")

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :user_id (String)

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/Apologist/users/client.rb', line 127

def get_user(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "users/#{URI.encode_uri_component(params[:user_id].to_s)}",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Apologist::Users::Types::GetUserResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#list_user_flags(request_options: {}, **params) ⇒ Apologist::Users::Types::ListUserFlagsResponse

Returns a paginated list of user flag definitions for the agent's team (all columns from user_flags), ordered by id ascending.

Examples:

client.users.list_user_flags

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :page (Integer, nil)
  • :per_page (Integer, nil)

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/Apologist/users/client.rb', line 84

def list_user_flags(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["page"] = params[:page] if params.key?(:page)
  query_params["per_page"] = params[:per_page] if params.key?(:per_page)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "users/flags",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Apologist::Users::Types::ListUserFlagsResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#list_users(request_options: {}, **params) ⇒ Apologist::Users::Types::ListUsersResponse

Returns a paginated list of users for the agent's team, with applied tags expanded as { id, name } and the persisted responder id.

Examples:

client.users.list_users

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :page (Integer, nil)
  • :per_page (Integer, nil)
  • :external_id (String, nil)
  • :tags (String, nil)
  • :responder_id (String, nil)
  • :min_timestamp (String, nil)
  • :max_timestamp (String, nil)

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/Apologist/users/client.rb', line 35

def list_users(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["page"] = params[:page] if params.key?(:page)
  query_params["per_page"] = params[:per_page] if params.key?(:per_page)
  query_params["external_id"] = params[:external_id] if params.key?(:external_id)
  query_params["tags"] = params[:tags] if params.key?(:tags)
  query_params["responder_id"] = params[:responder_id] if params.key?(:responder_id)
  query_params["min_timestamp"] = params[:min_timestamp] if params.key?(:min_timestamp)
  query_params["max_timestamp"] = params[:max_timestamp] if params.key?(:max_timestamp)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "users",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Apologist::Users::Types::ListUsersResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#update_user(request_options: {}, **params) ⇒ Apologist::Users::Types::UpdateUserResponse

Updates a user's external_id and/or tags and upserts the persisted responder for the agent. Only provided fields are changed.

Examples:

client.users.update_user(user_id: "user_id")

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :user_id (String)

Returns:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/Apologist/users/client.rb', line 165

def update_user(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request_data = Apologist::Users::Types::UserUpdateRequest.new(params).to_h
  non_body_param_names = %w[user_id]
  body = request_data.except(*non_body_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "PATCH",
    path: "users/#{URI.encode_uri_component(params[:user_id].to_s)}",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Apologist::Users::Types::UpdateUserResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end