stigg-api-client 0.509.0__tar.gz → 0.512.0__tar.gz

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.

Potentially problematic release.


This version of stigg-api-client might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: stigg-api-client
3
- Version: 0.509.0
3
+ Version: 0.512.0
4
4
  Summary:
5
5
  Author: Stigg
6
6
  Author-email: support@stigg.io
@@ -14,6 +14,7 @@ Classifier: Programming Language :: Python :: 3.11
14
14
  Requires-Dist: graphql-core (>=3.1,<4.0)
15
15
  Requires-Dist: requests (>=2.28,<3.0)
16
16
  Requires-Dist: sgqlc (>=16.0,<17.0)
17
+ Requires-Dist: tenacity (>=8.2.2,<9.0.0)
17
18
  Description-Content-Type: text/markdown
18
19
 
19
20
  # stigg-api-client
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "stigg-api-client"
3
- version = "0.509.0"
3
+ version = "0.512.0"
4
4
  description = ""
5
5
  authors = ["Stigg <support@stigg.io>"]
6
6
  readme = "README.md"
@@ -12,6 +12,7 @@ python = "^3.7"
12
12
  sgqlc = "^16.0"
13
13
  graphql-core = "^3.1"
14
14
  requests = "^2.28"
15
+ tenacity = "^8.2.2"
15
16
 
16
17
  [tool.poetry.group.dev.dependencies]
17
18
  flake8 = "^5.0"
@@ -0,0 +1,105 @@
1
+ import requests
2
+ from typing import Dict, Callable
3
+
4
+ from sgqlc.operation import Operation
5
+ from sgqlc.endpoint.requests import RequestsEndpoint
6
+ from requests.exceptions import HTTPError
7
+ from tenacity import retry, retry_if_exception_type, stop_after_attempt
8
+
9
+ from stigg.generated.operations import Operations
10
+
11
+ PRODUCTION_API_URL = "https://api.stigg.io/graphql"
12
+ PRODUCTION_EDGE_API_URL = "https://edge.api.stigg.io"
13
+
14
+ STIGG_REQUESTS_RETRY_COUNT = 3
15
+
16
+ RETRY_KWARGS = {
17
+ "retry": retry_if_exception_type(HTTPError),
18
+ "stop": stop_after_attempt(STIGG_REQUESTS_RETRY_COUNT),
19
+ "reraise": True,
20
+ }
21
+
22
+
23
+ class StiggClient:
24
+ def __init__(self, endpoint: RequestsEndpoint, enable_edge: bool, edge_api_url: str):
25
+ self._endpoint: RequestsEndpoint = endpoint
26
+
27
+ self._enable_edge = enable_edge
28
+ self._edge_url = edge_api_url
29
+
30
+ self._EDGE_QUERIES: Dict[Operation, Callable[[Dict], Dict]] = {
31
+ Operations.query.get_entitlements: self.get_entitlements,
32
+ Operations.query.get_paywall: self.get_paywall,
33
+ }
34
+
35
+ @retry(**RETRY_KWARGS)
36
+ def _get_entitlements(self, variables: Dict[str, Dict]) -> Dict:
37
+ params = variables.get("query")
38
+ if params is None:
39
+ raise ValueError('Variables do not include required key "query".')
40
+
41
+ customer_id = params.pop("customerId")
42
+
43
+ url = f"{self._edge_url}/v1/c/{customer_id}/entitlements.json"
44
+
45
+ data = requests.get(url, headers=self._endpoint.base_headers, params=params, timeout=self._endpoint.timeout)
46
+ data.raise_for_status()
47
+
48
+ return data.json()
49
+
50
+ def get_entitlements(self, variables: Dict[str, Dict]) -> Dict:
51
+ if self._enable_edge is False:
52
+ return self.request(Operations.query.get_entitlements, variables, raw_response=True)
53
+
54
+ return self._get_entitlements(variables)
55
+
56
+ @retry(**RETRY_KWARGS)
57
+ def _get_paywall(self,variables: Dict[str, Dict]) -> Dict:
58
+ params = variables.get("input")
59
+ if params is None:
60
+ raise ValueError('Variables do not include required key "input".')
61
+
62
+ if params.get("productId") is not None:
63
+ url = f"{self._edge_url}/v1/p/{params['productId']}/paywall.json"
64
+ params.pop("productId")
65
+ else:
66
+ url = f"{self._edge_url}/v1/paywall.json"
67
+
68
+ data = requests.get(url, headers=self._endpoint.base_headers, params=params, timeout=self._endpoint.timeout)
69
+ data.raise_for_status()
70
+
71
+ return data.json()
72
+
73
+ def get_paywall(self, variables: Dict[str, Dict]) -> Dict:
74
+ if self._enable_edge is False:
75
+ return self.request(Operations.query.get_paywall, variables, raw_response=True)
76
+
77
+ return self._get_paywall(variables)
78
+
79
+ @retry(**RETRY_KWARGS)
80
+ def request(self, operation: Operation, variables: Dict, raw_response: bool = False):
81
+ data = self._endpoint(operation, variables)
82
+ if raw_response:
83
+ return data
84
+
85
+ # clean up data to so an exception will be raised
86
+ errors = data.get('errors')
87
+ if errors and data.get('data') is not None:
88
+ data.pop('data')
89
+
90
+ # interpret results into native Python objects
91
+ return operation + data
92
+
93
+
94
+ class Stigg(Operations):
95
+ @staticmethod
96
+ def create_client(
97
+ api_key: str,
98
+ api_url: str = PRODUCTION_API_URL,
99
+ enable_edge: bool = True,
100
+ edge_api_url: str = PRODUCTION_EDGE_API_URL,
101
+ request_timeout: int = 30,
102
+ ) -> StiggClient:
103
+ headers = {'X-API-KEY': f'{api_key}'}
104
+ endpoint = RequestsEndpoint(url=api_url, base_headers=headers, timeout=request_timeout)
105
+ return StiggClient(endpoint, enable_edge, edge_api_url)
@@ -1,34 +0,0 @@
1
- from sgqlc.endpoint.requests import RequestsEndpoint
2
- from sgqlc.operation import Operation
3
-
4
- from stigg.generated.operations import Operations
5
-
6
-
7
- class StiggClient:
8
- def __init__(self, endpoint: RequestsEndpoint):
9
- self._endpoint: RequestsEndpoint = endpoint
10
-
11
- def request(self, operation: Operation, variables: dict, raw_response=False):
12
- data = self._endpoint(operation, variables)
13
- if raw_response:
14
- return data
15
-
16
- # clean up data to so an exepction will be raised
17
- errors = data.get('errors')
18
- if errors:
19
- data.pop('data')
20
-
21
- # interpret results into native Python objects
22
- return operation + data
23
-
24
-
25
- class Stigg(Operations):
26
- @staticmethod
27
- def create_client(api_key: str,
28
- api_url: str = 'https://api.stigg.io/graphql',
29
- request_timeout=30) -> StiggClient:
30
- headers = {'X-API-KEY': f'{api_key}'}
31
- endpoint = RequestsEndpoint(url=api_url,
32
- base_headers=headers,
33
- timeout=request_timeout)
34
- return StiggClient(endpoint)