Skip to content

Commit 9dccfc8

Browse files
authored
Upgrading dependencies, rewriting README and running tests (#24)
* 🚧 Starting to revamp the library * 💚 refs #24 Fixing tests
1 parent 670fb13 commit 9dccfc8

7 files changed

Lines changed: 211 additions & 60 deletions

File tree

.github/workflows/test.yaml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ name: run_tests
33

44
# Controls when the action will run.
55
on:
6-
#push:
7-
8-
# Allows you to run this workflow manually from the Actions tab
6+
push:
7+
branches: [ "master" ]
8+
pull_request:
9+
branches: [ "master" ]
910
workflow_dispatch:
1011

1112
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
@@ -21,18 +22,16 @@ jobs:
2122
# Steps represent a sequence of tasks that will be executed as part of the job
2223
steps:
2324
# Setup the system with the repository code, Java, and Ruby
24-
- uses: actions/checkout@v2
25-
- uses: isbang/compose-action@v1.0.0
25+
- uses: actions/checkout@v4
26+
- uses: isbang/compose-action@v1.5.1
2627
with:
2728
compose-file: './src/test/docker/docker-compose.yml'
2829
down-flags: '--volumes'
29-
- uses: actions/setup-python@v3
30+
- uses: actions/setup-python@v5
3031
with:
31-
python-version: '3.9.x'
32+
python-version: '3.12.2'
3233
- name: Install fusionauth library
33-
run: |
34-
pip3 install .
35-
pip3 install unittest2
34+
run: pip3 install .
3635
shell: bash
3736
- name: Check to see if FusionAuth is loaded
3837
run: |

README.md

Lines changed: 144 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,189 @@
1-
## FusionAuth Python Client ![semver 2.0.0 compliant](http://img.shields.io/badge/semver-2.0.0-brightgreen.svg?style=flat-square)
2-
If you're integrating FusionAuth with a Python 3 application, this library will speed up your development time.
1+
# FusionAuth Python Client ![semver 2.0.0 compliant](http://img.shields.io/badge/semver-2.0.0-brightgreen.svg?style=flat-square)
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).
310

411
For additional information and documentation on FusionAuth refer to [https://fusionauth.io](https://fusionauth.io).
512

6-
### Install
13+
## Install
714
To install the FusionAuth Python Client package run:
815

916
```bash
1017
pip install fusionauth-client
1118
```
1219

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.
1529

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.
1835

1936
```python
2037
from fusionauth.fusionauth_client import FusionAuthClient
2138
```
2239

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`.
2441

2542
```python
2643
client = FusionAuthClient('6b87a398-39f2-4692-927b-13188a81a9a3', 'http://localhost:9011')
2744
```
2845

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.
3054

3155
```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)
3471

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
3577
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+
}
3983
}
4084

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)
42158
```
43159

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+
45166

46-
If you encounter a bug with this library, please open an issue.
47167

48168
## Questions and support
49169

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.
51173

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).
53175

54176
Otherwise, please [post your question in the community forum](https://fusionauth.io/community/forum/).
55177

56178
## Contributing
57179

58180
Bug reports and pull requests are welcome on GitHub at https://github.com/FusionAuth/fusionauth-python-client.
59181

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+
60184
## License
61185

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).
63187

64188
## Upgrade Policy
65189

build.savant

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2023, FusionAuth, All Rights Reserved
2+
* Copyright (c) 2018-2024, FusionAuth, All Rights Reserved
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,7 +80,7 @@ target(name: "setup-python", description: "Gets the python dependencies") {
8080
proc1.consumeProcessOutput(System.out, System.err)
8181
proc1.waitFor()
8282

83-
def proc2 = "python3 -m pip install --user --upgrade wheel twine unittest2 requests deprecated".execute()
83+
def proc2 = "python3 -m pip install --user --upgrade wheel twine requests deprecated".execute()
8484
proc2.consumeProcessOutput(System.out, System.err)
8585
proc2.waitFor()
8686
}

src/test/docker/docker-compose.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@ version: '3'
22

33
services:
44
db:
5-
image: postgres:12.9
5+
image: postgres:16.0-alpine
66
environment:
77
PGDATA: /var/lib/postgresql/data/pgdata
88
POSTGRES_USER: postgres
99
POSTGRES_PASSWORD: postgres
10+
healthcheck:
11+
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
12+
interval: 5s
13+
timeout: 5s
14+
retries: 5
1015
networks:
11-
- db
16+
- db_net
1217
restart: unless-stopped
1318
volumes:
1419
- db_data:/var/lib/postgresql/data
1520

1621
fusionauth:
1722
image: fusionauth/fusionauth-app:latest
1823
depends_on:
19-
- db
24+
db:
25+
condition: service_healthy
2026
environment:
2127
DATABASE_URL: jdbc:postgresql://db:5432/fusionauth
2228
DATABASE_ROOT_USERNAME: postgres
@@ -25,23 +31,28 @@ services:
2531
DATABASE_PASSWORD: fusionauth
2632
FUSIONAUTH_APP_MEMORY: 512M
2733
FUSIONAUTH_APP_RUNTIME_MODE: development
34+
FUSIONAUTH_APP_SILENT_MODE: true
2835
FUSIONAUTH_APP_URL: http://fusionauth:9011
29-
SEARCH_TYPE: database
3036
FUSIONAUTH_APP_KICKSTART_FILE: /usr/local/fusionauth/kickstart/kickstart.json
31-
37+
SEARCH_TYPE: database
3238
networks:
33-
- db
39+
- db_net
3440
restart: unless-stopped
3541
ports:
3642
- 9011:9011
3743
volumes:
38-
- fa_config:/usr/local/fusionauth/config
44+
- fusionauth_config:/usr/local/fusionauth/config
3945
- ./kickstart:/usr/local/fusionauth/kickstart
46+
healthcheck:
47+
test: curl --silent --fail http://localhost:9011/api/status -o /dev/null -w "%{http_code}"
48+
interval: 5s
49+
timeout: 5s
50+
retries: 5
4051

4152
networks:
42-
db:
53+
db_net:
4354
driver: bridge
4455

4556
volumes:
4657
db_data:
47-
fa_config:
58+
fusionauth_config:
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2022, FusionAuth, All Rights Reserved
2+
# Copyright (c) 2024, FusionAuth, All Rights Reserved
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -13,13 +13,26 @@
1313
# either express or implied. See the License for the specific
1414
# language governing permissions and limitations under the License.
1515
#
16-
imgid=`docker container ls|grep fusiona|awk '{print $1}'`
1716

18-
cnt=`docker logs $imgid 2>&1|grep 'Starting FusionAuth HTTP server' |wc -l`
17+
cd $(dirname "$0")
1918

20-
while [ $cnt != "1" ]; do
21-
echo "waiting $cnt $imgid";
22-
cnt=`docker logs $imgid 2>&1|grep 'Starting FusionAuth HTTP server' |wc -l`;
23-
sleep 5;
19+
isFusionAuthReady () {
20+
docker compose logs fusionauth 2>&1 | grep -Fq 'Completed [POST] request to [/api/user/registration/00000000-0000-0000-0000-000000000008]'
21+
}
22+
23+
max_retries=20
24+
i=1
25+
while [ "$i" -le "$max_retries" ]; do
26+
echo -n "[$i/$max_retries] Waiting for FusionAuth server to start... "
27+
28+
if isFusionAuthReady; then
29+
echo "READY"
30+
exit 0
31+
fi
32+
33+
echo "NOT READY"
34+
sleep 5
35+
i=$((i + 1))
2436
done
2537

38+
exit 1

0 commit comments

Comments
 (0)