Last active
April 24, 2026 20:14
-
-
Save ilosamart/1fe392151251224e660cff4ec2ef524b to your computer and use it in GitHub Desktop.
SAJ get locações
This file contains hidden or 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
| import asyncio | |
| import httpx | |
| from config import settings | |
| async def get_saj_token( | |
| keycloak_token_url: str = settings.saj.KEYCLOAK_TOKEN_URL, # type: ignore | |
| keycloak_user: str = settings.saj.KEYCLOAK_USER, # type: ignore | |
| keycloak_password: str = settings.saj.KEYCLOAK_PASSWORD, # type: ignore | |
| keycloak_client_id: str = settings.saj.KEYCLOAK_CLIENT_ID, # type: ignore | |
| ) -> str: | |
| data = { | |
| "grant_type": "password", | |
| "client_id": keycloak_client_id, | |
| "username": keycloak_user, | |
| "password": keycloak_password, | |
| } | |
| headers = {"Content-Type": "application/x-www-form-urlencoded"} | |
| async with httpx.AsyncClient() as client: | |
| response = await client.post( | |
| keycloak_token_url, | |
| data=data, | |
| headers=headers, | |
| ) | |
| response.raise_for_status() | |
| return response.json().get("access_token") | |
| async def get_saj_lotacoes_usuario(username: str) -> list: | |
| async with httpx.AsyncClient() as client: | |
| token = await get_saj_token() | |
| query = """ | |
| query Usuario($filter: USR_usuario_get_by_id_filter) { | |
| usuario(filter: $filter) { | |
| id | |
| nome | |
| lotacoes { | |
| principal | |
| id_foro | |
| descricao | |
| id_local | |
| sequencial | |
| } | |
| } | |
| } | |
| """ | |
| variables = {"filter": {"id": username}} | |
| # Headers para o GraphQL | |
| # O campo Authorization deve conter o prefixo 'Bearer ' [5, 8] | |
| headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"} | |
| # Execução da Query | |
| gql_payload = {"query": query, "variables": variables} | |
| response = await client.post( | |
| settings.saj.GRAPHQL_BASE_URL, # type: ignore | |
| json=gql_payload, | |
| headers=headers, | |
| ) | |
| response.raise_for_status() | |
| resultado = response.json() | |
| return resultado.get("data", {}).get("usuario", {}) | |
| if __name__ == "__main__": | |
| # token = asyncio.run(get_saj_token()) | |
| # print(token) | |
| usuarios = ( | |
| "tramasoli", | |
| "clecia", | |
| "william", | |
| ) | |
| for usuario in usuarios: | |
| print(f"Lotacoes do usuario {usuario}:") | |
| lotacoes = asyncio.run(get_saj_lotacoes_usuario(usuario)) | |
| print(lotacoes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment