-
Notifications
You must be signed in to change notification settings - Fork 8
/
store.py
170 lines (147 loc) · 4.97 KB
/
store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from canonicalwebteam.store_api.exceptions import (
StoreApiResponseDecodeError,
StoreApiResponseError,
StoreApiResponseErrorList,
)
class StoreApi:
def __init__(self, session, store=None):
self.config = {1: {"base_url": "", "headers": {}}}
self.session = session
def process_response(self, response):
try:
body = response.json()
except ValueError as decode_error:
api_error_exception = StoreApiResponseDecodeError(
"JSON decoding failed: {}".format(decode_error)
)
raise api_error_exception
if not response.ok:
if "error_list" in body or "error-list" in body:
# V1 and V2 error handling
error_body = (
body["error_list"]
if "error_list" in body
else body["error-list"]
)
api_error_exception = StoreApiResponseErrorList(
"The API returned a list of errors",
response.status_code,
error_body,
)
raise api_error_exception
else:
raise StoreApiResponseError(
"Unknown error from API", response.status_code
)
return body
def get_endpoint_url(self, endpoint, api_version=1):
base_url = self.config[api_version]["base_url"]
return f"{base_url}{endpoint}"
def search(self, search, size, page, category=None, api_version=1):
url = self.get_endpoint_url("search", api_version)
params = {
"q": search,
"size": size,
"page": page,
"scope": "wide",
"arch": "wide",
"confinement": "strict,classic",
"fields": ",".join(
[
"package_name",
"title",
"summary",
"icon_url",
"media",
"publisher",
"developer_validation",
"origin",
"apps",
]
),
}
if category:
params["section"] = category
return self.process_response(
self.session.get(
url,
params=params,
headers=self.config[api_version].get("headers"),
)
)
def get_all_items(self, size, api_version=1):
url = self.get_endpoint_url("search", api_version)
return self.process_response(
self.session.get(
url,
params={"scope": "wide", "size": size},
headers=self.config[api_version].get("headers"),
)
)
def get_category_items(self, category, size=10, page=1, api_version=1):
return self.search(
search="",
category=category,
size=size,
page=page,
api_version=api_version,
)
def get_featured_items(self, size=10, page=1, api_version=1):
return self.search(
search="",
category="featured",
size=size,
page=page,
api_version=api_version,
)
def get_publisher_items(self, publisher, size=500, page=1, api_version=1):
return self.search(
search="publisher:" + publisher,
size=size,
page=page,
api_version=api_version,
)
def get_item_details(self, name, api_version=1):
url = self.get_endpoint_url("info/" + name, api_version)
params = {
"fields": ",".join(
[
"title",
"summary",
"description",
"license",
"contact",
"website",
"publisher",
"prices",
"media",
"download",
"version",
"created-at",
"confinement",
"categories",
"trending",
]
)
}
return self.process_response(
self.session.get(
url,
params=params,
headers=self.config[api_version].get("headers"),
)
)
def get_public_metrics(self, json, api_version=1):
url = self.get_endpoint_url("metrics")
headers = self.config[api_version].get("headers", {})
headers.update({"Content-Type": "application/json"})
return self.process_response(
self.session.post(url, headers=headers, json=json)
)
def get_categories(self, api_version=1):
url = self.get_endpoint_url("sections")
return self.process_response(
self.session.get(
url, headers=self.config[api_version].get("headers")
)
)