Skip to content

Commit

Permalink
Update API usage examples so that they work again
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto-herrmann committed Jan 30, 2025
1 parent 38ed800 commit 647eb36
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
13 changes: 9 additions & 4 deletions docs/examples/create_user_example.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import httpx
from get_token_example import get_token
from get_token_example import USER_AGENT, get_token

URL = "http://localhost:5057/user"

url = "http://localhost:5057/user"

def create_user(admin_user, new_user):
headers = {
"accept": "application/json",
"Authorization": f'Bearer {get_token(admin_user)}',
"Authorization": f"Bearer {get_token(admin_user)}",
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
}
response = httpx.put(f'{url}/{new_user["email"]}', headers=headers, json=new_user)
response = httpx.put(f'{URL}/{new_user["email"]}', headers=headers, json=new_user)
print(f"Status Code: {response.status_code}")
response.raise_for_status()

return response.text


if __name__ == "__main__":
admin_user = {
"username": "johndoe@oi.com",
Expand All @@ -25,6 +28,8 @@ def create_user(admin_user, new_user):
"password": "secret",
# "is_admin": False, # defaults to False
# "disabled": False, # defaults to False
"origem_unidade": "SIAPE",
"cod_unidade_autorizadora": 667,
"sistema_gerador": "PGD Petrvs MGI – Ambiente próprio 2.3.2",
}
print("user:", create_user(admin_user, new_user))
11 changes: 7 additions & 4 deletions docs/examples/get_all_users_example.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import httpx
from get_token_example import get_token
from get_token_example import USER_AGENT, get_token

URL = "http://localhost:5057/users"

url = 'http://localhost:5057/users'

def get_all_users(admin_user):
headers = {
"accept": "application/json",
"Authorization": f'Bearer {get_token(admin_user)}',
"Authorization": f"Bearer {get_token(admin_user)}",
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
}
response = httpx.get(url, headers=headers)
response = httpx.get(URL, headers=headers)
print(f"Status Code: {response.status_code}")
response.raise_for_status()

return response.text


if __name__ == "__main__":
admin_user = {
"username": "johndoe@oi.com",
Expand Down
13 changes: 8 additions & 5 deletions docs/examples/get_token_example.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import httpx

url = "http://localhost:5057/token"
URL = "http://localhost:5057/token"
USER_AGENT = "Example API PGD client/1.0 (https://github.com/gestaogovbr/api-pgd/tree/main/docs/examples)"


def get_token(data):
headers = {
"accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": USER_AGENT,
}
with httpx.Client() as client:
response = client.post(url, headers=headers, data=data)
response = client.post(URL, headers=headers, data=data)
print(f"Status Code: {response.status_code}")
response.raise_for_status()
access_token = response.json()["access_token"]

return access_token


if __name__ == "__main__":
# em ../docker-compose.yml
data = {
"username": "johndoe@oi.com", # API_PGD_ADMIN_USER
"password": "secret", # API_PGD_ADMIN_PASSWORD
"username": "johndoe@oi.com", # API_PGD_ADMIN_USER
"password": "secret", # API_PGD_ADMIN_PASSWORD
}
print("token:", get_token(data))

12 changes: 7 additions & 5 deletions docs/examples/get_user_example.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import httpx
from get_token_example import get_token
from get_token_example import USER_AGENT, get_token

URL = "http://localhost:5057/user"

url = 'http://localhost:5057/user'

def get_user(admin_user, user):
headers = {
"accept": "application/json",
"Authorization": f'Bearer {get_token(admin_user)}',
"Authorization": f"Bearer {get_token(admin_user)}",
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
}
response = httpx.get(f'{url}/{user}', headers=headers)
response = httpx.get(f"{URL}/{user}", headers=headers)
print(f"Status Code: {response.status_code}")
response.raise_for_status()

return response.text


if __name__ == "__main__":
admin_user = {
"username": "johndoe@oi.com",
"password": "secret",
}
user = "johndoe@oi.com"
print("user:", get_user(admin_user, user))

0 comments on commit 647eb36

Please sign in to comment.