-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathapi_key.rb
More file actions
60 lines (45 loc) · 1.81 KB
/
api_key.rb
File metadata and controls
60 lines (45 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# frozen_string_literal: true
class EasyPost::Services::ApiKey < EasyPost::Services::Service
MODEL_CLASS = EasyPost::Models::ApiKey # :nodoc:
# Retrieve a list of all ApiKey objects.
def all
response = @client.make_request(:get, 'api_keys')
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
# Retrieve a list of ApiKey objects (works for the authenticated user or a child user).
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
# Create an API key for a child or referral customer user
def create(mode)
response = @client.make_request(:post, 'api_keys', { mode: mode })
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
# Delete an API key for a child or referral customer user
def delete(id)
@client.make_request(:delete, "api_keys/#{id}")
# Return true if succeeds, an error will be thrown if it fails
true
end
# Enable an API key for a child or referral customer user
def enable(id)
response = @client.make_request(:post, "api_keys/#{id}/enable")
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
# Disable an API key for a child or referral customer user
def disable(id)
response = @client.make_request(:post, "api_keys/#{id}/disable")
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
end