|
2 | 2 |
|
3 | 3 | ### Description |
4 | 4 |
|
5 | | -This repository contains the Python 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 Python 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 Python Client |
10 | 11 |
|
11 | | -You can run the following pip commands in order to install the packages directly from Github: |
| 12 | +You can run the following `pip` commands in order to install the packages |
| 13 | +directly from GitHub: |
| 14 | + |
12 | 15 | ``` |
13 | 16 | pip install git+https://github.com/TheJumpCloud/jcapi-python.git#subdirectory=jcapiv1 |
14 | 17 | pip install git+https://github.com/TheJumpCloud/jcapi-python.git#subdirectory=jcapiv2 |
15 | 18 | ``` |
16 | 19 |
|
17 | | -Alternatively you can also pull this repository and install manually: |
18 | | -Change to the appropriate directory (jcapiv1 or jcapiv2) and then run the following command: |
| 20 | +Alternatively you can also pull this repository and install manually. Change to |
| 21 | +the appropriate directory (jcapiv1 or jcapiv2) and then run one of the |
| 22 | +following commands. |
19 | 23 |
|
20 | 24 | To install the package for the local user: |
| 25 | + |
21 | 26 | ``` |
22 | 27 | $ python setup.py install --user |
23 | 28 | ``` |
| 29 | + |
24 | 30 | To install the package for all users: |
| 31 | + |
25 | 32 | ``` |
26 | 33 | $ sudo python setup.py install |
27 | 34 | ``` |
28 | 35 |
|
29 | 36 | ### Authentication and Authorization |
30 | 37 |
|
31 | | -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) section in our API docs. |
| 38 | +All endpoints support authentication via API key: see the |
| 39 | +[Authentication & Authorization](https://docs.jumpcloud.com/2.0/authentication-and-authorization/authentication-and-authorization-overview) |
| 40 | +section in our API documentation. |
32 | 41 |
|
33 | | -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) which allows an individual system to manage its information and resource associations. |
| 42 | +Some systems endpoints (in both API v1 and v2) also support |
| 43 | +[System Context Authorization](https://docs.jumpcloud.com/2.0/authentication-and-authorization/system-context) |
| 44 | +which allows an individual system to manage its information and resource |
| 45 | +associations. |
34 | 46 |
|
35 | 47 | ### Usage Examples |
36 | 48 |
|
37 | | -For more detailed instructions, refer to each API's respective README file ([README for API v1](jcapiv1/README.md) and [README for API v2](jcapiv2/README.md)) and the generated docs under each folder. |
| 49 | +For more detailed instructions, refer to each API version's respective README |
| 50 | +file ([README for API v1](jcapiv1/README.md) and |
| 51 | +[README for API v2](jcapiv2/README.md)) and the generated documentation under |
| 52 | +each folder. |
| 53 | + |
| 54 | +#### API v1 Example |
38 | 55 |
|
39 | | -#### API v1 example: |
40 | 56 | ```python |
| 57 | +"""JumpCloud API v1 example.""" |
| 58 | + |
41 | 59 | import jcapiv1 |
42 | 60 | from jcapiv1.rest import ApiException |
43 | 61 |
|
44 | | -... |
45 | | -content_type = 'application/json' |
46 | | -accept = 'application/json' |
47 | | - |
48 | | -# set up the configuration object with your API key for authorization: |
49 | | -jcapiv1.configuration.api_key['x-api-key'] = '<YOUR_API_KEY>' |
50 | | - |
51 | | -# instantiate the API object for the group of endpoints you need to use |
52 | | -# for instance for Systemusers API: |
53 | | -systemusersAPI = jcapiv1.SystemusersApi() |
54 | | - |
55 | | -try: |
56 | | - # make an API call to retrieve all systemusers: |
57 | | - users = systemusersAPI.systemusers_list(content_type, accept) |
58 | | - print(users) |
59 | | -except ApiException as e: |
60 | | - print("Exception when calling SystemusersApi->systemusers_list: %s\n" % e) |
61 | | - |
62 | | -try: |
63 | | - # make an API call to modify a specific user's last name: |
64 | | - put_request = jcapiv1.Systemuserputpost() |
65 | | - put_request.lastname = 'Updated Last Name' |
66 | | - systemusersAPI.systemusers_put('<YOUR_SYSTEMUSER_ID>', content_type, accept, body=put_request) |
67 | | -except ApiException as e: |
68 | | - print("Exception when calling SystemusersApi->systemusers_put: %s\n" % e) |
| 62 | +API_KEY = "YOUR_API_KEY" |
| 63 | +SYSTEM_USER_ID = "YOUR_SYSTEM_USER_ID" |
| 64 | +SYSTEM_USER_EMAIL = "YOUR_SYSTEM_USER_EMAIL" |
| 65 | +SYSTEM_USER_USERNAME = "YOUR_SYSTEM_USER_USERNAME" |
| 66 | + |
| 67 | +CONTENT_TYPE = "application/json" |
| 68 | +ACCEPT = "application/json" |
| 69 | + |
| 70 | +# Set up the configuration object with your API key for authorization |
| 71 | +CONFIGURATION = jcapiv1.Configuration() |
| 72 | +CONFIGURATION.api_key['x-api-key'] = API_KEY |
| 73 | + |
| 74 | +# Instantiate the API object for the group of endpoints you need to use, |
| 75 | +# for instance the system users API |
| 76 | +API_INSTANCE = jcapiv1.SystemusersApi(jcapiv1.ApiClient(CONFIGURATION)) |
| 77 | + |
| 78 | +def list_users(): |
| 79 | + """Make an API call to retrieve all system users.""" |
| 80 | + try: |
| 81 | + users = API_INSTANCE.systemusers_list(CONTENT_TYPE, ACCEPT) |
| 82 | + print(users) |
| 83 | + except ApiException as err: |
| 84 | + print("Exception when calling SystemusersApi->systemusers_list: %s\n" % err) |
| 85 | + |
| 86 | +def update_user(): |
| 87 | + """Make an API call to update a system user.""" |
| 88 | + put_request = jcapiv1.Systemuserputpost(email=SYSTEM_USER_EMAIL, username=SYSTEM_USER_USERNAME) |
| 89 | + put_request.lastname = "Updated Last Name" |
| 90 | + |
| 91 | + try: |
| 92 | + user = API_INSTANCE.systemusers_put(SYSTEM_USER_ID, CONTENT_TYPE, ACCEPT, body=put_request) |
| 93 | + print(user) |
| 94 | + except ApiException as err: |
| 95 | + print("Exception when calling SystemusersApi->systemusers_put: %s\n" % err) |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + list_users() |
| 99 | + update_user() |
| 100 | + |
69 | 101 | ``` |
70 | 102 |
|
71 | | -#### API v2 example: |
| 103 | +#### API v2 Example |
| 104 | + |
72 | 105 | ```python |
| 106 | +"""JumpCloud API v2 example.""" |
| 107 | + |
73 | 108 | import jcapiv2 |
74 | 109 | from jcapiv2.rest import ApiException |
75 | 110 |
|
76 | | -... |
77 | | -content_type = 'application/json' |
78 | | -accept = 'application/json' |
| 111 | +API_KEY = "YOUR_API_KEY" |
| 112 | + |
| 113 | +CONTENT_TYPE = "application/json" |
| 114 | +ACCEPT = "application/json" |
| 115 | + |
| 116 | +# Set up the configuration object with your API key for authorization |
| 117 | +CONFIGURATION = jcapiv2.Configuration() |
| 118 | +CONFIGURATION.api_key['x-api-key'] = API_KEY |
79 | 119 |
|
80 | | -# set up the configuration object with your API key: |
81 | | -jcapiv2.configuration.api_key['x-api-key'] = '<YOUR_API_KEY>' |
| 120 | +# Instantiate the API object for the group of endpoints you need to use, |
| 121 | +# for instance the user groups API |
| 122 | +API_INSTANCE = jcapiv2.UserGroupsApi(jcapiv2.ApiClient(CONFIGURATION)) |
82 | 123 |
|
83 | | -# instantiate the API object for the group of endpoints you need to use |
84 | | -# for instance for User Groups API: |
85 | | -userGroupsAPI = jcapiv2.UserGroupsApi() |
| 124 | +def get_user_groups(): |
| 125 | + """Make an API call to retrieve all user groups.""" |
| 126 | + try: |
| 127 | + user_groups = API_INSTANCE.groups_user_list(CONTENT_TYPE, ACCEPT) |
| 128 | + print(user_groups) |
| 129 | + except ApiException as err: |
| 130 | + print("Exception when calling UserGroupsApi->groups_user_list: %s\n" % err) |
86 | 131 |
|
87 | | -try: |
88 | | - # make an API call to retrieve all user groups: |
89 | | - userGroups = userGroupsAPI.groups_user_list(content_type, accept) |
90 | | - print(userGroups) |
91 | | -except ApiException as e: |
92 | | - print("Exception when calling UserGroupsApi->groups_user_list: %s\n" % e) |
| 132 | +if __name__ == "__main__": |
| 133 | + get_user_groups() |
93 | 134 |
|
94 | 135 | ``` |
95 | 136 |
|
96 | | -#### System Context Authorization example: |
| 137 | +#### System Context Authorization Example |
| 138 | + |
97 | 139 | ```python |
| 140 | +"""JumpCloud system context authorization example.""" |
| 141 | + |
98 | 142 | import jcapiv2 |
99 | 143 | from jcapiv2.rest import ApiException |
100 | 144 |
|
101 | | -content_type = 'application/json' |
102 | | -accept = 'application/json' |
103 | | -system_id = '<YOUR_SYSTEM_ID>' |
104 | | - |
105 | | -# set headers for the System Context Authorization: |
106 | | -# for detailed instructions on how to generate these headers, |
107 | | -# refer to: https://docs.jumpcloud.com/2.0/authentication-and-authorization/system-context |
108 | | -sys_context_auth = 'Signature keyId="system/<YOUR_SYSTEM_ID>",headers="request-line date",algorithm="rsa-sha256",signature="<YOUR_SYSTEM_SIGNATURE>"' |
109 | | -sys_context_date = 'Thu, 19 Oct 2017 17:27:57 GMT' # the current date on the system |
110 | | - |
111 | | -# instantiate the API object for the group of endpoints you need to use |
112 | | -# for instance for User Groups API: |
113 | | -systemsAPI = jcapiv2.SystemsApi() |
114 | | - |
115 | | -try: |
116 | | - # list the system groups this system is a member of: |
117 | | - # Note that we pass the System Context Authorization headers as the 'date' and 'authorization' parameters |
118 | | - groups = systemsAPI.graph_system_member_of(system_id, content_type, accept, date=sys_context_date, authorization=sys_context_auth) |
119 | | - print groups |
120 | | -except ApiException as e: |
121 | | - print("Exception when calling systemsAPI->graph_system_member_of: %s\n" % e) |
| 145 | +# Set headers for System Context Authorization. For detailed instructions on |
| 146 | +# how to generate these headers, refer to: |
| 147 | +# https://docs.jumpcloud.com/2.0/authentication-and-authorization/system-context |
| 148 | +SYSTEM_ID = "YOUR_SYSTEM_ID" |
| 149 | +# The current date on the system, e.g. "Thu, 09 Aug 1990 14:25:15 GMT" |
| 150 | +SYSTEM_DATE = "YOUR_SYSTEM_DATE" |
| 151 | +SYSTEM_SIGNATURE = "YOUR_SYSTEM_SIGNATURE" |
| 152 | +SYSTEM_CONTEXT_AUTH = ( |
| 153 | + 'Signature keyId="system/{}",' |
| 154 | + 'headers="request-line date",' |
| 155 | + 'algorithm="rsa-sha256",' |
| 156 | + 'signature="{}"' |
| 157 | +).format(SYSTEM_ID, SYSTEM_SIGNATURE) |
| 158 | + |
| 159 | +CONTENT_TYPE = "application/json" |
| 160 | +ACCEPT = "application/json" |
| 161 | + |
| 162 | +# Set up the configuration object |
| 163 | +CONFIGURATION = jcapiv2.Configuration() |
| 164 | + |
| 165 | +# Instantiate the API object for the group of endpoints you need to use, |
| 166 | +# for instance the systems API |
| 167 | +API_INSTANCE = jcapiv2.SystemsApi(jcapiv2.ApiClient(CONFIGURATION)) |
| 168 | + |
| 169 | +def get_system_groups(): |
| 170 | + """Make an API call to retrieve all system groups this system is a member of.""" |
| 171 | + try: |
| 172 | + # Note the System Context Authorization headers are passed as arguments |
| 173 | + system_groups = API_INSTANCE.graph_system_member_of( |
| 174 | + SYSTEM_ID, CONTENT_TYPE, ACCEPT, |
| 175 | + date=SYSTEM_DATE, authorization=SYSTEM_CONTEXT_AUTH, |
| 176 | + ) |
| 177 | + print(system_groups) |
| 178 | + except ApiException as err: |
| 179 | + print("Exception when calling systemsAPI->graph_system_member_of: %s\n" % err) |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + get_system_groups() |
| 183 | + |
122 | 184 | ``` |
0 commit comments