Better naming for api wrappers

This commit is contained in:
Remy Moll
2021-10-30 15:42:49 +02:00
parent 23aff0ddf0
commit fe015d75e7
5 changed files with 47 additions and 33 deletions

View File

@@ -2,40 +2,27 @@ import tortilla
import requests
def get_auth():
token_query = {
'grant_type': 'client_credentials',
'client_id': 'af929f08',
'client_secret': 'ae61e214f679558c28678d5c638c5725'
}
response = requests.post('https://sso-int.sbb.ch/auth/realms/SBB_Public/protocol/openid-connect/token', data=token_query).json()
token = response["access_token"]
auth = {
'Authorization': f"Bearer {token}",
'X-Contract-Id': 'PLY223P',
# 'X-Conversation-Id': str(conv_id),
}
return auth
class SBBWrapper:
def __init__(self) -> None:
self.wrapper = tortilla.wrap('https://b2p-int.api.sbb.ch/api')
self.wrapper.config.headers = self.get_auth()
def get_auth(self):
token_query = {
'grant_type': 'client_credentials',
'client_id': 'af929f08',
'client_secret': 'ae61e214f679558c28678d5c638c5725'
}
SBB = tortilla.wrap('https://b2p-int.api.sbb.ch/api')
SBB.config.headers = get_auth()
# SBB.config.headers = {
# "grant_type" : "client_credentials",
# "client_secret" : "ae61e214f679558c28678d5c638c5725",
# "client_id" : "af929f08pied!",
# }
response = requests.post('https://sso-int.sbb.ch/auth/realms/SBB_Public/protocol/openid-connect/token', data=token_query).json()
# Authorization: Bearer $accessToken' \
# -H 'Cache-Control: no-cache' \
# -H 'Accept: application/json' \
# -H 'X-Contract-Id: ABC1234' \
# -H 'X-Conversation-Id: e5eeb775-1e0e-4f89-923d-afa780ef844b
loc = SBB.locations
print(loc.get(params = {"name":"Bern"}))
token = response["access_token"]
auth = {
'Authorization': f"Bearer {token}",
'X-Contract-Id': 'PLY223P',
# 'X-Conversation-Id': str(conv_id),
}
return auth