This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
/
net.py
410 lines (355 loc) · 18 KB
/
net.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import logging
import treq
import traceback
from twisted.internet.defer import inlineCallbacks
from cpchain.crypto import ECCipher
from cpchain.utils import config, Encoder
from cpchain.wallet.fs import publish_file_update
from cpchain.wallet import utils
import time
logger = logging.getLogger(__name__) # pylint: disable=locally-disabled, invalid-name
class MarketClient:
def __init__(self, wallet, account=None):
self.wallet = wallet
self.account = account or self.wallet.accounts.default_account
self.public_key = ECCipher.serialize_public_key(self.account.public_key) if self.account else None
self.url = config.market.market_url + '/'
self.token = ''
self.nonce = ''
@inlineCallbacks
def get(self, url, raw_content=False):
url = self.url + url
headers = {
"MARKET-KEY": self.public_key,
"MARKET-TOKEN": self.token,
"Content-Type": "application/json"
}
resp = yield treq.get(url, headers=headers)
if raw_content:
content = yield treq.content(resp)
return content
data = yield treq.json_content(resp)
return data
@staticmethod
def str_to_timestamp(s):
return s
@inlineCallbacks
def isRegistered(self, public_key):
header = {'Content-Type': 'application/json'}
data = {'public_key': public_key}
resp = yield treq.post(url=self.url + 'account/v1/isregistered/', headers=header, json=data,
persistent=False)
result = yield treq.json_content(resp)
return result['status']
@inlineCallbacks
def login(self, username=None):
header = {'Content-Type': 'application/json'}
data = {'public_key': self.public_key, 'username': username}
resp = yield treq.post(url=self.url + 'account/v1/login/', headers=header, json=data,
persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug("login response: %s", confirm_info)
self.nonce = confirm_info['message']
logger.debug('nonce: %s', self.nonce)
signature = ECCipher.create_signature(self.account.private_key, self.nonce)
header_confirm = {'Content-Type': 'application/json'}
data_confirm = {'public_key': self.public_key, 'code': Encoder.bytes_to_hex(signature)}
resp = yield treq.post(self.url + 'account/v1/confirm/', headers=header_confirm,
json=data_confirm,
persistent=False)
confirm_info = yield treq.json_content(resp)
self.token = confirm_info['message']
return confirm_info['status']
@inlineCallbacks
def publish_product(self, selected_id, title, ptype, description, price,
tags, start_date, end_date, category=None, cover_image=None):
logger.debug("start publish product")
header = {'Content-Type': 'multipart/form-data'} # 'application/json'}
header['MARKET-KEY'] = self.public_key
header['MARKET-TOKEN'] = self.token
logger.debug('header token: %s', self.token)
data = {
'owner_address': self.public_key,
'title': title,
'ptype': ptype,
'description': description,
'price': price,
'tags': tags,
'start_date': start_date,
'end_date': end_date,
'category': category
}
signature_source = str(self.public_key) + str(title) + str(ptype) + str(description) + str(
price) + MarketClient.str_to_timestamp(start_date) + MarketClient.str_to_timestamp(end_date)
signature = ECCipher.create_signature(self.account.private_key, signature_source)
data['signature'] = Encoder.bytes_to_hex(signature)
logger.debug("signature: %s", data['signature'])
try:
url = self.url + 'product/v1/allproducts/'
resp = yield treq.post(url,
files=dict(cover_image=open(cover_image, 'rb')),
headers=header, data=data, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug('market_hash: %s', confirm_info['data']['market_hash'])
market_hash = confirm_info['data']['market_hash']
except Exception as e:
logger.exception(e)
if ptype == 'file' or ptype == 'stream':
publish_file_update(market_hash, selected_id)
return market_hash
@inlineCallbacks
def query_product(self, keyword):
logger.debug('keywords: %s', keyword)
header = {'Content-Type': 'application/json'}
url = self.url + 'product/v1/es_product/search/?search=' + keyword
logger.debug('query url: %s', url)
resp = yield treq.get(url=url, headers=header, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug("query product confirm info: %s", confirm_info)
return confirm_info['results']
@inlineCallbacks
def query_by_tag(self, tag):
url = self.url + 'product/v1/es_product/search/?status=0&tag=' + str(tag)
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
resp = yield treq.get(url, headers=header)
confirm_info = yield treq.json_content(resp)
logger.debug('query by tag confirm info: %s', confirm_info)
return confirm_info['results']
@inlineCallbacks
def query_carousel(self):
logger.debug('status: in query carousel')
url = self.url + 'main/v1/carousel/list/'
logger.debug("query carousel url: %s", url)
header = {'Content-Type': 'application/json', 'MARKET-KEY': self.public_key,
'MARKET-TOKEN': self.token}
resp = yield treq.get(url=url, headers=header)
confirm_info = yield treq.json_content(resp)
logger.debug("carousel response: %s", confirm_info)
return confirm_info['data']
@inlineCallbacks
def query_hot_tag(self):
url = self.url + 'main/v1/hot_tag/list/'
header = {'Content-Type': 'application/json', 'MARKET-KEY': self.public_key,
'MARKET-TOKEN': self.token}
resp = yield treq.get(url=url, headers=header)
confirm_info = yield treq.json_content(resp)
logger.debug("hot tag: %s", confirm_info)
return confirm_info['data']
@inlineCallbacks
def query_promotion(self):
url = self.url + 'product/v1/recommend_product/list/'
header = {'Content-Type': 'application/json', 'MARKET-KEY': self.public_key,
'MARKET-TOKEN': self.token}
resp = yield treq.get(url=url, headers=header)
confirm_info = yield treq.json_content(resp)
logger.debug("promotion: %s", confirm_info)
return confirm_info['data']
@inlineCallbacks
def query_recommend_product(self):
url = self.url + 'product/v1/recommend_product/list/'
header = {'Content-Type': 'application/json', 'MARKET-KEY': self.public_key,
'MARKET-TOKEN': self.token}
resp = yield treq.get(url=url, headers=header)
confirm_info = yield treq.json_content(resp)
logger.debug("recommend product: %s", confirm_info)
return confirm_info['data']
@inlineCallbacks
def add_product_sales_quantity(self, market_hash):
url = self.url + 'product/v1/product/sales_quantity/add/'
payload = {'market_hash': market_hash}
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
logger.debug("add product sales quantity url: %s" % url)
logger.debug("add product sales quantity header: %s" % header)
logger.debug("add product sales quantity payload: %s" % payload)
resp = yield treq.post(url, headers=header, json=payload)
confirm_info = yield treq.json_content(resp)
return confirm_info
@inlineCallbacks
def upload_file_info(self, hashcode, path, size, product_id, remote_type, remote_uri, name, encrypted_key):
# fixme: another argument aes_key should be passed and encrypted
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
data = {"public_key": self.public_key, "hashcode": hashcode, "path": path, "size": size,
"client_id": product_id,
"remote_type": remote_type, "remote_uri": remote_uri, "is_published": "False",
"aes_key": encrypted_key, "market_hash": "hash", "name": name}
url = self.url + 'user_data/v1/uploaded_file/add/'
logger.debug('upload file info url: %s', url)
logger.debug('upload file info header: %s', header)
logger.debug('upload file info payload: %s', data)
resp = yield treq.post(url, headers=header, json=data, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug('upload file info response: %s', confirm_info)
return confirm_info['status']
@inlineCallbacks
def update_file_info(self, product_id, market_hash):
logger.debug("update file info in market")
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
data = {"client_id": product_id, "market_hash": market_hash, "is_published": True}
url = self.url + 'user_data/v1/uploaded_file/update/'
logger.debug('update file info payload: %s', data)
logger.debug('update file info url: %s', url)
logger.debug('product id: %s', product_id)
resp = yield treq.post(url, headers=header, json=data, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug('update file info to market response: %s', confirm_info)
return confirm_info['status']
@inlineCallbacks
def query_by_seller(self, public_key):
logger.debug("in query by seller")
logger.debug("seller's public key: %s", self.public_key)
logger.debug("public key used by query: %s", public_key)
url = self.url + 'product/v1/es_product/search/?ordering=-created&offset=0&limit=100&status=0&seller=' + str(public_key)
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token, 'Content-Type': 'application/json'}
resp = yield treq.get(url, headers=header, persistent=False)
confirm_info = yield treq.json_content(resp)
return confirm_info['results']
@inlineCallbacks
def query_comment_by_hash(self, market_hash):
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
url = utils.build_url(self.url + "comment/v1/comment/list/", {'market_hash': market_hash})
logger.debug(url)
resp = yield treq.get(url, headers=header)
comment_info = yield treq.json_content(resp)
logger.debug('query comment by hash response: %s', comment_info)
return comment_info['data']
@inlineCallbacks
def add_comment_by_hash(self, market_hash, comment=""):
comment_address = ECCipher.get_address_from_public_key(self.account.public_key)
logger.debug(comment_address)
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token, 'Content-Type': 'application/json'}
data = {'public_key': self.public_key, 'market_hash': market_hash, 'content': comment}
url = utils.build_url(self.url + "comment/v1/comment/add/", {'market_hash': market_hash})
resp = yield treq.post(url, headers=header, json=data, persistent=False)
comment_info = yield treq.json_content(resp)
logger.debug('add comment by hash response: %s', comment_info)
return comment_info['status']
@inlineCallbacks
def delete_file_info(self, product_id):
logger.debug("delete file info in market")
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
data = {"client_id": product_id}
url = self.url + 'user_data/v1/uploaded_file/delete/'
logger.debug('delete file info payload: %s', data)
logger.debug('delete file info url: %s', url)
logger.debug('product id: %s', product_id)
resp = yield treq.post(url, headers=header, json=data, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug('delete file info to market confirm: %s', confirm_info)
return confirm_info['status']
@inlineCallbacks
def add_follow_seller(self, seller_public_key=""):
logger.debug("add following seller to market")
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
logger.debug(self.public_key)
data = {'public_key': self.public_key, 'seller_public_key': seller_public_key}
url = self.url + 'product/v1/my_seller/subscribe/'
logger.debug('add following seller payload: %s', data)
logger.debug('add following seller url: %s', url)
resp = yield treq.post(url, headers=header, json=data, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug('add following seller to market confirm: %s', confirm_info)
return confirm_info['status']
@inlineCallbacks
def add_follow_tag(self, tag="tag1"):
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
logger.debug(self.public_key)
data = {'public_key': self.public_key, 'tag': tag}
url = self.url + 'product/v1/my_tag/subscribe/'
logger.debug('add following tag payload: %s', data)
logger.debug('add following tag url: %s', url)
resp = yield treq.post(url, headers=header, json=data, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug('add following tag to market confirm: %s', confirm_info)
return confirm_info['status']
@inlineCallbacks
def hide_product(self, market_hash=''):
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
logger.debug(self.public_key)
data = {'market_hash': market_hash}
url = self.url + 'product/v1/product/hide/'
logger.debug('hide product payload: %s', data)
logger.debug('hide product url: %s', url)
resp = yield treq.post(url, headers=header, json=data, persistent=False)
confirm_info = yield treq.json_content(resp)
logger.debug('hide product to market confirm: %s', confirm_info)
return confirm_info['status']
@inlineCallbacks
def query_by_following_tag(self):
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
url = utils.build_url(self.url + "product/v1/my_tag/product/search/")
logger.debug(url)
resp = yield treq.get(url, headers=header)
logger.debug(resp)
comment_info = yield treq.json_content(resp)
logger.debug('query by following tag to market confirm: %s', comment_info)
return comment_info['data']
@inlineCallbacks
def query_by_following_seller(self):
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
url = utils.build_url(self.url + "product/v1/my_seller/product/search/")
logger.debug(url)
resp = yield treq.get(url, headers=header)
logger.debug(resp)
comment_info = yield treq.json_content(resp)
logger.debug('query by following seller confirm: %s', comment_info)
return comment_info['data']
@inlineCallbacks
def products(self, search=None):
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
query = {}
if search:
query['keyword'] = search
url = utils.build_url(self.url + "product/v1/allproducts/", query)
logger.debug(url)
resp = yield treq.get(url, headers=header)
product_info = yield treq.json_content(resp)
return product_info
@inlineCallbacks
def myproducts(self):
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
'Content-Type': 'application/json'}
url = utils.build_url(self.url + "product/v1/allproducts/my_products/", {})
logger.debug(url)
resp = yield treq.get(url, headers=header)
products_info = yield treq.json_content(resp)
return products_info
@inlineCallbacks
def query_data(self, market_hash):
url = self.url + 'user_data/v1/uploaded_file/item/'
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token, 'Content-Type': 'application/json'}
url = utils.build_url(url, {'market_hash': market_hash})
resp = yield treq.get(url, headers=header, persistent=False)
data_info = yield treq.json_content(resp)
return data_info
@inlineCallbacks
def query_records(self, address):
url = self.url + 'records/v1/record/'
header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token, 'Content-Type': 'application/json'}
url = utils.build_url(url, {'address': address})
resp = yield treq.get(url, headers=header, persistent=False)
data_info = yield treq.json_content(resp)
return data_info
@inlineCallbacks
def query_username(self, app=None, public_key=None):
header = {'Content-Type': 'application/json'}
if not public_key:
public_key = self.public_key
data = {'public_key': public_key}
resp = yield treq.post(url=self.url + 'account/v1/username/', headers=header, json=data,
persistent=False)
username = yield treq.json_content(resp)
if app:
app.username = username['username']
return username