Coverage for webapp/api/sso.py : 88%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import os
2from webapp import api
3from webapp.api.exceptions import ApiResponseError, ApiResponseDecodeError
5api_session = api.requests.Session()
7SNAPSTORE_DASHBOARD_API_URL = os.getenv(
8 "SNAPSTORE_DASHBOARD_API_URL", "https://dashboard.snapcraft.io/"
9)
11LOGIN_URL = os.getenv("LOGIN_URL", "https://login.ubuntu.com")
14HEADERS = {
15 "Accept": "application/json, application/hal+json",
16 "Content-Type": "application/json",
17 "Cache-Control": "no-cache",
18}
21def process_response(response):
22 if not response.ok:
23 raise ApiResponseError("Unknown error from api", response.status_code)
25 try:
26 body = response.json()
27 except ValueError as decode_error:
28 api_error_exception = ApiResponseDecodeError(
29 "JSON decoding failed: {}".format(decode_error)
30 )
31 raise api_error_exception
33 return body
36def post_macaroon(json):
37 url = "".join([SNAPSTORE_DASHBOARD_API_URL, "dev/api/acl/"])
38 response = api_session.post(url=url, headers=HEADERS, json=json)
40 return process_response(response)
43def get_refreshed_discharge(json):
44 url = "".join([LOGIN_URL, "/api/v2/tokens/refresh"])
45 response = api_session.post(url=url, headers=HEADERS, json=json)
47 return process_response(response)