|
1 | 1 | ## JCAPI-Ruby
|
2 | 2 |
|
3 |
| -### Description ### |
| 3 | +### Description |
4 | 4 |
|
5 |
| -This repository contains the Ruby client code for the JumpCloud API v1 and v2. |
6 |
| -It also provides the tools to generate the client code from the API yaml files, using swagger-codegen. |
7 |
| -For detailed instructions on how to generate the code, see the [Contributing](CONTRIBUTING.md) section. |
| 5 | +This repository contains the Ruby client code for the JumpCloud API v1 and |
| 6 | +v2. It also provides the tools to generate the client code from the API YAML |
| 7 | +files, using Swagger Codegen. For detailed instructions on how to generate the |
| 8 | +code, see the [Contributing](CONTRIBUTING.md) section. |
8 | 9 |
|
9 | 10 | ### Installing the Ruby Client
|
10 | 11 |
|
11 |
| -#### Installing from github with Bundler |
| 12 | +#### Installing from GitHub with Bundler |
12 | 13 |
|
13 | 14 | Add the following line(s) to your Gemfile:
|
14 | 15 |
|
15 |
| -`gem 'jcapiv1', :git => 'https://github.com/TheJumpCloud/jcapi-ruby.git'` for the jcapiv1 gem |
| 16 | +`gem 'jcapiv1', :git => 'https://github.com/TheJumpCloud/jcapi-ruby.git'` for the jcapiv1 gem. |
16 | 17 |
|
17 |
| -`gem 'jcapiv2', :git => 'https://github.com/TheJumpCloud/jcapi-ruby.git'` for the jcapiv2 gem |
| 18 | +`gem 'jcapiv2', :git => 'https://github.com/TheJumpCloud/jcapi-ruby.git'` for the jcapiv2 gem. |
18 | 19 |
|
19 |
| -And run: `bundle install` |
| 20 | +And run: `bundle install`. |
20 | 21 |
|
21 |
| -#### Manual install |
| 22 | +#### Manual Install |
22 | 23 |
|
23 | 24 | Change to the appropriate directory (jcapiv1 or jcapiv2) and then run the following
|
24 |
| -commands to build/install the Ruby Client API package: |
| 25 | +commands to build/install the Ruby client API package: |
25 | 26 |
|
26 | 27 | To build the Ruby code into a gem:
|
| 28 | + |
27 | 29 | ```shell
|
28 | 30 | gem build jcapiv1.gemspec
|
29 | 31 | ```
|
| 32 | + |
30 | 33 | Then either install the gem locally:
|
| 34 | + |
31 | 35 | ```shell
|
32 | 36 | gem install --user-install ./jcapiv1-1.0.0.gem
|
33 | 37 | ```
|
| 38 | + |
34 | 39 | Or for all users:
|
| 40 | + |
35 | 41 | ```shell
|
36 |
| -$ sudo gem install ./jcapiv1-1.0.0.gem |
| 42 | +sudo gem install ./jcapiv1-1.0.0.gem |
37 | 43 | ```
|
38 | 44 |
|
39 | 45 | ### Authentication and Authorization
|
40 | 46 |
|
41 |
| -All endpoints support authentication via API key: see the [Authentication and Authorization](https://docs.jumpcloud.com/2.0/authentication-and-authorization/authentication-and-authorization-overview) |
42 |
| -section in our API docs. |
43 |
| - |
44 |
| -Some Systems endpoints (in both API v1 and v2) also support the [System Context authorization](https://docs.jumpcloud.com/2.0/authentication-and-authorization/system-context) |
45 |
| -which allows an individual system to manage its information and resource associations. |
| 47 | +All endpoints support authentication via API key: see the |
| 48 | +[Authentication & Authorization](https://docs.jumpcloud.com/2.0/authentication-and-authorization/authentication-and-authorization-overview) |
| 49 | +section in our API documentation. |
46 | 50 |
|
| 51 | +Some systems endpoints (in both API v1 and v2) also support |
| 52 | +[System Context Authorization](https://docs.jumpcloud.com/2.0/authentication-and-authorization/system-context) |
| 53 | +which allows an individual system to manage its information and resource |
| 54 | +associations. |
47 | 55 |
|
48 | 56 | ### Usage Examples
|
49 | 57 |
|
50 |
| -For more detailed instructions, refer to each API's respective README file |
51 |
| -([README for API v1](jcapiv1/README.md) and [README for API v2](jcapiv2/README.md)) |
52 |
| -and the generated docs under each folder. |
| 58 | +For more detailed instructions, refer to each API version's respective README |
| 59 | +file ([README for API v1](jcapiv1/README.md) and |
| 60 | +[README for API v2](jcapiv2/README.md)) and the generated documentation under |
| 61 | +each folder. |
| 62 | + |
| 63 | +#### API v1 Example |
53 | 64 |
|
54 |
| -#### API v1 example: |
55 | 65 | ```ruby
|
56 | 66 | #!/usr/bin/env ruby
|
57 | 67 |
|
58 |
| -# Load the gem |
59 | 68 | require 'jcapiv1'
|
60 | 69 |
|
61 |
| -# Setup API key: |
62 |
| -JCAPIv1.configure do |config| |
63 |
| - # Configure API key authorization: x-api-key |
64 |
| - config.api_key['x-api-key'] = '<YOUR_JUMPCLOUD_API_KEY>' |
65 |
| -end |
66 |
| - |
67 |
| -# instantiate the Systemusers API object: |
68 |
| -api_instance = JCAPIv1::SystemusersApi.new |
| 70 | +api_key = 'YOUR_API_KEY' |
| 71 | +system_user_id = 'YOUR_SYSTEM_USER_ID' |
69 | 72 |
|
70 | 73 | content_type = 'application/json'
|
71 | 74 | accept = 'application/json'
|
72 | 75 |
|
| 76 | +# Set up the configuration object with your API key for authorization. |
| 77 | +JCAPIv1.configure do |config| |
| 78 | + config.api_key['x-api-key'] = api_key |
| 79 | +end |
| 80 | + |
| 81 | +# Instantiate the API object for the group of endpoints you need to use, |
| 82 | +# for instance the system users API. |
| 83 | +system_users_api = JCAPIv1::SystemusersApi.new |
73 | 84 |
|
74 |
| -# Example 1: print all users: |
| 85 | +# Example 1: Make an API call to retrieve system users. |
75 | 86 |
|
76 | 87 | opts = {
|
77 | 88 | limit: 100, # The number of records to return at once.
|
78 |
| - sort: 'username' # sort by username |
| 89 | + sort: 'username' |
79 | 90 | }
|
80 | 91 |
|
81 | 92 | begin
|
82 |
| - res = api_instance.systemusers_list(content_type, accept, opts) |
83 |
| - puts res.total_count |
84 |
| - puts res.results |
| 93 | + response = system_users_api.systemusers_list(content_type, accept, opts) |
| 94 | + puts response |
85 | 95 | rescue JCAPIv1::ApiError => e
|
86 |
| - puts 'Exception when calling SystemusersApi->systemusers_list: #{e}' |
| 96 | + puts "Exception when calling SystemusersApi->systemusers_list: #{e}" |
87 | 97 | end
|
88 | 98 |
|
| 99 | +# Example 2: Make an API call to update a system user. |
89 | 100 |
|
90 |
| -# Example 2: modify the lastname of a specific user: |
91 |
| - |
92 |
| -# create a put request with the updated last name: |
93 |
| -put_request = JCAPIv1::Systemuserputpost.new |
94 |
| -put_request.lastname = 'updated last name' |
95 |
| -# pass the request in the optional parameters: |
96 |
| -opts = { |
97 |
| - body: put_request |
98 |
| -} |
| 101 | +put_request = JCAPIv1::Systemuserput.new |
| 102 | +put_request.lastname = 'Updated Last Name' |
| 103 | +opts = { body: put_request } |
99 | 104 |
|
100 | 105 | begin
|
101 |
| - res = api_instance.systemusers_put('<YOUR_USER_ID>', content_type, accept, opts) |
102 |
| - puts res |
| 106 | + response = system_users_api.systemusers_put( |
| 107 | + system_user_id, content_type, accept, opts |
| 108 | + ) |
| 109 | + puts response |
103 | 110 | rescue JCAPIv1::ApiError => e
|
104 |
| - puts 'Exception when calling SystemusersApi->systemusers_get: #{e}' |
| 111 | + puts "Exception when calling SystemusersApi->systemusers_put: #{e}" |
105 | 112 | end
|
106 | 113 |
|
107 | 114 | ```
|
108 | 115 |
|
109 | 116 |
|
110 |
| -#### API v2 example: |
| 117 | +#### API v2 Example |
| 118 | + |
111 | 119 | ```ruby
|
112 | 120 | #!/usr/bin/env ruby
|
113 | 121 |
|
114 |
| -# Load the gem |
115 | 122 | require 'jcapiv2'
|
116 | 123 |
|
117 |
| -# Setup API key: |
| 124 | +api_key = 'YOUR_API_KEY' |
| 125 | + |
| 126 | +content_type = 'application/json' |
| 127 | +accept = 'application/json' |
| 128 | + |
| 129 | +# Set up the configuration object with your API key for authorization |
118 | 130 | JCAPIv2.configure do |config|
|
119 |
| - # Configure API key authorization: x-api-key |
120 |
| - config.api_key['x-api-key'] = '<YOUR_JUMPCLOUD_API_KEY>' |
| 131 | + config.api_key['x-api-key'] = api_key |
121 | 132 | end
|
122 | 133 |
|
123 |
| -# instantiate the UserGroups API object: |
124 |
| -api_instance = JCAPIv2::UserGroupsApi.new |
| 134 | +# Instantiate the API object for the group of endpoints you need to use, |
| 135 | +# for instance the user groups API. |
| 136 | +user_groups_api = JCAPIv2::UserGroupsApi.new |
125 | 137 |
|
126 |
| -content_type = 'application/json' |
127 |
| -accept = 'application/json' |
| 138 | +# Make an API call to retrieve user groups. |
128 | 139 |
|
129 |
| -# print all user groups: |
130 | 140 | begin
|
131 |
| - res = api_instance.groups_user_list(content_type, accept) |
132 |
| - puts res |
| 141 | + response = user_groups_api.groups_user_list(content_type, accept) |
| 142 | + puts response |
133 | 143 | rescue JCAPIv2::ApiError => e
|
134 |
| - puts 'Exception when calling UserGroupsApi->groups_user_list: #{e}' |
| 144 | + puts "Exception when calling UserGroupsApi->groups_user_list: #{e}" |
135 | 145 | end
|
| 146 | + |
136 | 147 | ```
|
137 | 148 |
|
138 | 149 |
|
139 |
| -#### System Context API example: |
| 150 | +#### System Context Authorization Example |
| 151 | + |
140 | 152 | ```ruby
|
141 | 153 | #!/usr/bin/env ruby
|
142 | 154 |
|
143 |
| -# Load the gem |
144 | 155 | require 'jcapiv2'
|
145 | 156 |
|
146 |
| -# instantiate the Systems API object: |
147 |
| -api_instance = JCAPIv2::SystemsApi.new |
| 157 | +# Set headers for System Context Authorization. For detailed instructions on |
| 158 | +# how to generate these headers, refer to: |
| 159 | +# https://docs.jumpcloud.com/2.0/authentication-and-authorization/system-context |
| 160 | +system_id = 'YOUR_SYSTEM_ID' |
| 161 | +# The current date on the system, e.g. 'Fri, 16 Jan 1998 12:13:05 GMT' |
| 162 | +system_date = 'YOUR_SYSTEM_DATE' |
| 163 | +system_signature = 'YOUR_SYSTEM_SIGNATURE' |
| 164 | +system_context_auth = \ |
| 165 | + "Signature keyId=\"system/#{system_id}\","\ |
| 166 | + 'headers="request-line date",'\ |
| 167 | + 'algorithm="rsa-sha256",'\ |
| 168 | + "signature=\"#{system_signature}\"" |
148 | 169 |
|
149 | 170 | content_type = 'application/json'
|
150 | 171 | accept = 'application/json'
|
151 |
| -system_id = '<YOUR_SYSTEM_ID>' |
152 |
| -# set headers for the System Context Authorization: |
153 |
| -# for detailed instructions on how to generate these headers, |
154 |
| -# refer to: https://docs.jumpcloud.com/2.0/authentication-and-authorization/system-context |
155 |
| -opts = { |
156 |
| - authorization: 'Signature keyId="system/<YOUR_SYSTEM_ID>",headers="request-line date",algorithm="rsa-sha256",signature="<YOUR_SYSTEM_SIGNATURE>"', |
157 |
| - date: 'Thu, 19 Oct 2017 17:27:57 GMT' # the current date on the system |
158 |
| -} |
| 172 | + |
| 173 | +# Instantiate the API object for the group of endpoints you need to use, |
| 174 | +# for instance the user groups API. |
| 175 | +systems_api = JCAPIv2::SystemsApi.new |
| 176 | + |
| 177 | +# Make an API call to retrieve all system groups this system is a member of. |
| 178 | + |
| 179 | +opts = { authorization: system_context_auth, date: system_date } |
159 | 180 |
|
160 | 181 | begin
|
161 |
| - # list the system groups this system is a member of: |
162 |
| - res = api_instance.graph_system_member_of(system_id, content_type, accept, opts) |
163 |
| - puts res |
| 182 | + response = systems_api.graph_system_member_of( |
| 183 | + system_id, content_type, accept, opts |
| 184 | + ) |
| 185 | + puts response |
164 | 186 | rescue JCAPIv2::ApiError => e
|
165 |
| - puts 'Exception when calling systemsAPI->graph_system_member_of: #{e}' |
| 187 | + puts "Exception when calling systemsAPI->graph_system_member_of: #{e}" |
166 | 188 | end
|
| 189 | + |
167 | 190 | ```
|
0 commit comments