-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathexample.rb
99 lines (89 loc) · 2.72 KB
/
example.rb
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require_relative '../lib/ruby_http_client'
# This example uses the SendGrid API as an example
headers = JSON.parse('
{
"Authorization": "Bearer ' + ENV['SENDGRID_API_KEY'] + '"
}
')
host = 'https://api.sendgrid.com'
client = SendGrid::Client.new(host: host, request_headers: headers)
# You can pass in an http_options hash to set values for NET::HTTP attributes
# https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html
# client = SendGrid::Client.new(host: host,
# request_headers: headers,
# http_options: {open_timeout: 15, read_timeout: 30})
# If you want to make request via proxy, you can set your proxy server in two ways.
#
# (1) Pass proxy_options hash
#
# client = SendGrid::Client.new(host: host,
# request_headers: headers,
# proxy_options: { host: '127.0.0.1', port: 8080 })
#
# (2) Set 'http_proxy' environment variable
#
# ENV['http_proxy'] = 'user:[email protected]:8080'
# client = SendGrid::Client.new(host: host, request_headers: headers)
# GET Collection
query_params = { 'limit' => 100, 'offset' => 0 }
response = client.version('v3').api_keys.get(query_params: query_params)
puts response.status_code
puts response.body
puts response.headers
# POST
request_body = JSON.parse('
{
"name": "My API Key Ruby Test",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}
')
response = client.version('v3').api_keys.post(request_body: request_body)
puts response.status_code
puts response.body
puts response.headers
api_key_id = JSON.parse(response.body)['api_key_id']
# GET Single
response = client.version('v3').api_keys._(api_key_id).get
puts response.status_code
puts response.body
puts response.headers
# PATCH
request_body = JSON.parse('
{
"name": "A New Hope"
}
')
response = client.api_keys._(api_key_id).patch(request_body: request_body)
puts response.status_code
puts response.body
puts response.headers
# PUT
request_body = JSON.parse('
{
"name": "A New Hope",
"scopes": [
"user.profile.read",
"user.profile.update"
]
}
')
response = client.api_keys._(api_key_id).put(request_body: request_body)
puts response.status_code
puts response.body
puts response.headers
# DELETE
response = client.api_keys._(api_key_id).delete
puts response.status_code
puts response.headers
# Rate limit information
response = client.version('v3').api_keys._(api_key_id).get
puts response.ratelimit.limit
puts response.ratelimit.remaining
puts response.ratelimit.reset
puts response.ratelimit.exceeded?
# Sleep the current thread until the reset has happened
response.ratelimit.wait!