This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
101 lines (91 loc) · 2.78 KB
/
client.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
# -*- coding:utf-8 -*-
'''
File: client.py
File Created: Saturday, 7th July 2018
Author: Hongzoeng Ng ([email protected])
-----
Last Modified: Saturday, 14th July 2018
Modified By: Hongzoeng Ng ([email protected]>)
-----
Copyright @ 2018 KenEcho
'''
import requests
from params import ADDRESS, DEFAULT_PORT
class Client(object):
def __init__(self, address=ADDRESS, port=DEFAULT_PORT):
self._base_url = "http://{}:{}".format(
address, port
)
def get_full_chain(self):
url = self._base_url + "/full_chain"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
raise Exception(
"Http service error.\nURL: {}".format(url)
)
def mine(self):
url = self._base_url + "/mine"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
raise Exception(
"Http service error.\nURL: {}".format(url)
)
def add_transaction(self, sender, recipient, amount):
url = self._base_url + "/transactions/new"
trans = {
"sender": sender,
"recipient": recipient,
"amount": amount
}
response = requests.post(url, json=trans)
if response.status_code == 201:
return response.json()
else:
raise Exception(
"Error code: {}\n".format(response.status_code)
)
def register_node(self, address):
"""
:params:
address, list of string
"""
url = self._base_url + "/nodes_register"
params = {
"nodes": address
}
response = requests.post(url, json=params)
if response.status_code == 201:
return response.json()
else:
raise Exception(
"Error code: {}\n".format(response.status_code)
)
def update_chain(self):
url = self._base_url + "/nodes/resolve"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
raise Exception(
"Http service error.\nURL: {}".format(url)
)
def registerNode_and_updateChain(self, address):
url = self._base_url + "/nodes_register"
params = {
"nodes": address
}
response = requests.post(url, json=params)
if response.status_code == 201:
print(response.json()['message'])
print("Total nodes:\n")
for node in response.json()['total_nodes']:
print(node)
self.update_chain()
else:
raise Exception(
response
)