Class: EasyPost::Services::ApiKey

Inherits:
Service
  • Object
show all
Defined in:
lib/easypost/services/api_key.rb

Constant Summary collapse

MODEL_CLASS =

:nodoc:

EasyPost::Models::ApiKey

Instance Method Summary collapse

Methods inherited from Service

#initialize

Constructor Details

This class inherits a constructor from EasyPost::Services::Service

Instance Method Details

#allObject

Retrieve a list of all ApiKey objects.



7
8
9
10
11
# File 'lib/easypost/services/api_key.rb', line 7

def all
  response = @client.make_request(:get, 'api_keys')

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

#create(mode) ⇒ Object

Create an API key for a child or referral customer user



33
34
35
36
37
# File 'lib/easypost/services/api_key.rb', line 33

def create(mode)
  response = @client.make_request(:post, 'api_keys', { mode: mode })

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

#delete(id) ⇒ Object

Delete an API key for a child or referral customer user



40
41
42
43
44
45
# File 'lib/easypost/services/api_key.rb', line 40

def delete(id)
  @client.make_request(:delete, "api_keys/#{id}")

  # Return true if succeeds, an error will be thrown if it fails
  true
end

#disable(id) ⇒ Object

Disable an API key for a child or referral customer user



55
56
57
58
59
# File 'lib/easypost/services/api_key.rb', line 55

def disable(id)
  response = @client.make_request(:post, "api_keys/#{id}/disable")

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

#enable(id) ⇒ Object

Enable an API key for a child or referral customer user



48
49
50
51
52
# File 'lib/easypost/services/api_key.rb', line 48

def enable(id)
  response = @client.make_request(:post, "api_keys/#{id}/enable")

  EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

#retrieve_api_keys_for_user(id) ⇒ Object

Retrieve a list of ApiKey objects (works for the authenticated user or a child user).



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/easypost/services/api_key.rb', line 14

def retrieve_api_keys_for_user(id)
  api_keys = all

  if api_keys.id == id
    # This function was called on the authenticated user
    return api_keys.keys
  end

  # This function was called on a child user (authenticated as parent, only return this child user's details).
  api_keys.children.each do |child|
    if child.id == id
      return child.keys
    end
  end

  raise EasyPost::Errors::FilteringError.new(EasyPost::Constants::NO_USER_FOUND)
end