canonicalwebteam.store-api 5.0.0__py3-none-any.whl → 6.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ from canonicalwebteam.exceptions import (
2
+ StoreApiConnectionError,
3
+ StoreApiResourceNotFound,
4
+ StoreApiResponseDecodeError,
5
+ StoreApiResponseError,
6
+ StoreApiResponseErrorList,
7
+ PublisherAgreementNotSigned,
8
+ PublisherMacaroonRefreshRequired,
9
+ PublisherMissingUsername,
10
+ )
11
+
12
+
13
+ class Base:
14
+ def __init__(self, session):
15
+ self.session = session
16
+
17
+ def process_response(self, response):
18
+ # 5xx responses are not in JSON format
19
+ if response.status_code >= 500:
20
+ raise StoreApiConnectionError("Service Unavailable")
21
+
22
+ try:
23
+ body = response.json()
24
+ except ValueError as decode_error:
25
+ api_error_exception = StoreApiResponseDecodeError(
26
+ "JSON decoding failed: {}".format(decode_error)
27
+ )
28
+ raise api_error_exception
29
+
30
+ if self._is_macaroon_expired(response.headers):
31
+ raise PublisherMacaroonRefreshRequired
32
+
33
+ if not response.ok:
34
+ error_list = (
35
+ body["error_list"]
36
+ if "error_list" in body
37
+ else body.get("error-list")
38
+ )
39
+ if "error_list" in body or "error-list" in body:
40
+ for error in error_list:
41
+ if error["code"] == "user-missing-latest-tos":
42
+ raise PublisherAgreementNotSigned
43
+ if error["code"] == "user-not-ready":
44
+ if "has not signed agreement" in error["message"]:
45
+ raise PublisherAgreementNotSigned
46
+ elif "username" in error["message"]:
47
+ raise PublisherMissingUsername
48
+ if error["code"] == "resource-not-found":
49
+ raise StoreApiResourceNotFound
50
+
51
+ raise StoreApiResponseErrorList(
52
+ "The api returned a list of errors",
53
+ response.status_code,
54
+ error_list,
55
+ )
56
+ elif not body:
57
+ raise StoreApiResponseError(
58
+ "Unknown error from api", response.status_code
59
+ )
60
+
61
+ return body
62
+
63
+ def _is_macaroon_expired(self, headers):
64
+ """
65
+ Returns True if the macaroon needs to be refreshed from
66
+ the header response.
67
+ """
68
+ return headers.get("WWW-Authenticate") == ("Macaroon needs_refresh=1")