-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update API usage examples so that they work again
- Loading branch information
1 parent
38ed800
commit 647eb36
Showing
4 changed files
with
31 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|