|
1 | | -## FusionAuth Python Client  |
2 | | -If you're integrating FusionAuth with a Python 3 application, this library will speed up your development time. |
| 1 | +# FusionAuth Python Client  |
| 2 | + |
| 3 | +## Intro |
| 4 | + |
| 5 | +<!-- |
| 6 | +tag::forDocSite[] |
| 7 | +--> |
| 8 | + |
| 9 | +If you're integrating FusionAuth with a Python 3 application, this library will speed up your development time. Please also make sure to check our [SDK Usage Suggestions page](https://fusionauth.io/docs/sdks/#usage-suggestions). |
3 | 10 |
|
4 | 11 | For additional information and documentation on FusionAuth refer to [https://fusionauth.io](https://fusionauth.io). |
5 | 12 |
|
6 | | -### Install |
| 13 | +## Install |
7 | 14 | To install the FusionAuth Python Client package run: |
8 | 15 |
|
9 | 16 | ```bash |
10 | 17 | pip install fusionauth-client |
11 | 18 | ``` |
12 | 19 |
|
13 | | -This library can be found on PyPI |
14 | | -* https://pypi.org/project/fusionauth-client/ |
| 20 | +This library can be found on PyPI at https://pypi.org/project/fusionauth-client/. |
| 21 | + |
| 22 | +## Examples |
| 23 | + |
| 24 | +### Set Up |
| 25 | + |
| 26 | +First, you have to make sure you have a running FusionAuth instance. If you don't have one already, the easiest way to install FusionAuth is [via Docker](https://fusionauth.io/docs/get-started/download-and-install/docker), but there are [other ways](https://fusionauth.io/docs/get-started/download-and-install). By default, it'll be running on `localhost:9011`. |
| 27 | + |
| 28 | +Then, you have to [create an API Key](https://fusionauth.io/docs/apis/authentication#managing-api-keys) in the admin UI to allow calling API endpoints. |
15 | 29 |
|
16 | | -### Coding |
17 | | -And then include the package in your code by using the following statement. |
| 30 | +You are now ready to use this library. |
| 31 | + |
| 32 | +### Create the Client |
| 33 | + |
| 34 | +Include the package in your code by using the following statement. |
18 | 35 |
|
19 | 36 | ```python |
20 | 37 | from fusionauth.fusionauth_client import FusionAuthClient |
21 | 38 | ``` |
22 | 39 |
|
23 | | -Now you're ready to begin making requests to FusionAuth. You will need to supply an API key you created in FusionAuth, the folowing example assumes an API key of `6b87a398-39f2-4692-927b-13188a81a9a3`. |
| 40 | +Now you're ready to begin making requests to FusionAuth. You will need to supply an API key you created in FusionAuth, the following example assumes an API key of `6b87a398-39f2-4692-927b-13188a81a9a3`. |
24 | 41 |
|
25 | 42 | ```python |
26 | 43 | client = FusionAuthClient('6b87a398-39f2-4692-927b-13188a81a9a3', 'http://localhost:9011') |
27 | 44 | ``` |
28 | 45 |
|
29 | | -Here's an example which logs a user in: |
| 46 | +### Error Handling |
| 47 | + |
| 48 | +After every request is made, you need to check for any errors and handle them. To avoid cluttering things up, we'll omit the error handling in the next examples, but you should do something like the following. |
| 49 | + |
| 50 | + |
| 51 | +### Create an Application |
| 52 | + |
| 53 | +To create an [Application](https://fusionauth.io/docs/get-started/core-concepts/applications), use the `create_application()` method. |
30 | 54 |
|
31 | 55 | ```python |
32 | | -from fusionauth.fusionauth_client import FusionAuthClient |
33 | | -client = FusionAuthClient(API_KEY, 'http://localhost:9011') |
| 56 | +data = { |
| 57 | + 'application': { |
| 58 | + 'name': 'ChangeBank' |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +result = client.create_application(data) |
| 63 | + |
| 64 | +# Handle errors as shown in the beginning of the Examples section |
| 65 | + |
| 66 | +# Otherwise parse the successful response |
| 67 | +print(result.success_response) |
| 68 | +``` |
| 69 | + |
| 70 | +[Check the API docs for this endpoint](https://fusionauth.io/docs/apis/applications#create-an-application) |
34 | 71 |
|
| 72 | +### Adding Roles to an Existing Application |
| 73 | + |
| 74 | +To add [roles to an Application](https://fusionauth.io/docs/get-started/core-concepts/applications#roles), use `create_application_role()`. |
| 75 | + |
| 76 | +```python |
35 | 77 | data = { |
36 | | - 'loginId': loginId, |
37 | | - 'password': password, |
38 | | - 'applicationId': My_App_ID |
| 78 | + 'role': { |
| 79 | + 'name': 'customer', |
| 80 | + 'description': 'Default role for regular customers', |
| 81 | + 'isDefault': 1 |
| 82 | + } |
39 | 83 | } |
40 | 84 |
|
41 | | -print(client.login(data).success_response) |
| 85 | +result = client.create_application_role( |
| 86 | + application_id='5a89377e-a250-4b15-b766-377ecc9b9fc9', |
| 87 | + request=data |
| 88 | +) |
| 89 | + |
| 90 | +# Handle errors as shown in the beginning of the Examples section |
| 91 | + |
| 92 | +# Otherwise parse the successful response |
| 93 | +print(result.success_response) |
| 94 | +``` |
| 95 | + |
| 96 | +[Check the API docs for this endpoint](https://fusionauth.io/docs/apis/applications#create-an-application-role) |
| 97 | + |
| 98 | +### Retrieve Application Details |
| 99 | + |
| 100 | +To fetch details about an [Application](https://fusionauth.io/docs/get-started/core-concepts/applications), use `retrieve_application()`. |
| 101 | + |
| 102 | +```python |
| 103 | +result = client.retrieve_application( |
| 104 | + application_id='5a89377e-a250-4b15-b766-377ecc9b9fc9' |
| 105 | +) |
| 106 | +print(result.success_response) |
| 107 | +``` |
| 108 | + |
| 109 | +[Check the API docs for this endpoint](https://fusionauth.io/docs/apis/applications#retrieve-an-application) |
| 110 | + |
| 111 | +### Delete an Application |
| 112 | + |
| 113 | +To delete an [Application](https://fusionauth.io/docs/get-started/core-concepts/applications), use `delete_application()`. |
| 114 | + |
| 115 | +```python |
| 116 | +client.delete_application( |
| 117 | + application_id='5a89377e-a250-4b15-b766-377ecc9b9fc9' |
| 118 | +) |
| 119 | +``` |
| 120 | + |
| 121 | +[Check the API docs for this endpoint](https://fusionauth.io/docs/apis/applications#delete-an-application) |
| 122 | + |
| 123 | +### Lock a User |
| 124 | + |
| 125 | +To [prevent a User from logging in](https://fusionauth.io/docs/get-started/core-concepts/users), use `deactivate_user()`. |
| 126 | + |
| 127 | +```python |
| 128 | +client.deactivate_user( |
| 129 | + user_id='231b982c-9304-4642-9bac-492d6917f5aa' |
| 130 | +) |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +[Check the API docs for this endpoint](https://fusionauth.io/docs/apis/users#delete-a-user) |
| 135 | + |
| 136 | +### Registering a User |
| 137 | + |
| 138 | +To [register a User in an Application](https://fusionauth.io/docs/get-started/core-concepts/users#registrations), use `register()`. |
| 139 | + |
| 140 | +The code below also adds a `customer` role and a custom `appBackgroundColor` property to the User Registration. |
| 141 | + |
| 142 | +```python |
| 143 | +result = client.register( |
| 144 | + user_id='231b982c-9304-4642-9bac-492d6917f5aa', |
| 145 | + request={ |
| 146 | + 'registration': { |
| 147 | + 'applicationId': '5a89377e-a250-4b15-b766-377ecc9b9fc9', |
| 148 | + 'roles': [ |
| 149 | + 'customer' |
| 150 | + ], |
| 151 | + 'data': { |
| 152 | + 'appBackgroundColor': '#096324' |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +) |
| 157 | +print(result.success_response) |
42 | 158 | ``` |
43 | 159 |
|
44 | | -Each method in the client library includes documentation to describe the use and parameters. In addition to this resource, review the API documentation. https://fusionauth.io/docs/v1/tech/apis/ |
| 160 | +[Check the API docs for this endpoint](https://fusionauth.io/docs/apis/registrations#create-a-user-registration-for-an-existing-user) |
| 161 | + |
| 162 | +<!-- |
| 163 | +end::forDocSite[] |
| 164 | +--> |
| 165 | + |
45 | 166 |
|
46 | | -If you encounter a bug with this library, please open an issue. |
47 | 167 |
|
48 | 168 | ## Questions and support |
49 | 169 |
|
50 | | -If you have a question or support issue regarding this client library, we'd love to hear from you. |
| 170 | +If you find any bugs in this library, [please open an issue](https://github.com/FusionAuth/fusionauth-python-client/issues). Note that changes to the `FusionAuthClient` class have to be done on the [FusionAuth Client Builder repository](https://github.com/FusionAuth/fusionauth-client-builder/blob/master/src/main/client/python.client.ftl), which is responsible for generating that file. |
| 171 | + |
| 172 | +But if you have a question or support issue, we'd love to hear from you. |
51 | 173 |
|
52 | | -If you have a paid edition with support included, please [open a ticket in your account portal](https://account.fusionauth.io/account/support/). Learn more about [paid editions here](https://fusionauth.io/pricing). |
| 174 | +If you have a paid plan with support included, please [open a ticket in your account portal](https://account.fusionauth.io/account/support/). Learn more about [paid plan here](https://fusionauth.io/pricing). |
53 | 175 |
|
54 | 176 | Otherwise, please [post your question in the community forum](https://fusionauth.io/community/forum/). |
55 | 177 |
|
56 | 178 | ## Contributing |
57 | 179 |
|
58 | 180 | Bug reports and pull requests are welcome on GitHub at https://github.com/FusionAuth/fusionauth-python-client. |
59 | 181 |
|
| 182 | +Note: if you want to change the `FusionAuthClient` class, you have to do it on the [FusionAuth Client Builder repository](https://github.com/FusionAuth/fusionauth-client-builder/blob/master/src/main/client/python.client.ftl), which is responsible for generating all client libraries we support. |
| 183 | + |
60 | 184 | ## License |
61 | 185 |
|
62 | | -This code is available as open source under the terms of the [Apache v2.0 License](https://opensource.org/licenses/Apache-2.0). |
| 186 | +This code is available as open source under the terms of the [Apache v2.0 License](https://opensource.org/license/apache-2-0). |
63 | 187 |
|
64 | 188 | ## Upgrade Policy |
65 | 189 |
|
|
0 commit comments