Hide keyboard shortcuts

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 

4 

5api_session = api.requests.Session() 

6 

7SNAPSTORE_DASHBOARD_API_URL = os.getenv( 

8 "SNAPSTORE_DASHBOARD_API_URL", "https://dashboard.snapcraft.io/" 

9) 

10 

11LOGIN_URL = os.getenv("LOGIN_URL", "https://login.ubuntu.com") 

12 

13 

14HEADERS = { 

15 "Accept": "application/json, application/hal+json", 

16 "Content-Type": "application/json", 

17 "Cache-Control": "no-cache", 

18} 

19 

20 

21def process_response(response): 

22 if not response.ok: 

23 raise ApiResponseError("Unknown error from api", response.status_code) 

24 

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 

32 

33 return body 

34 

35 

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) 

39 

40 return process_response(response) 

41 

42 

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) 

46 

47 return process_response(response)