This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eugene Yurtaev
committed
Mar 29, 2016
0 parents
commit 104667e
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import requests | ||
from APIResponse import APIResponse | ||
from Errors import * | ||
import json | ||
|
||
class API: | ||
def __init__(self): | ||
self.api_endpoint = "http://localhost:8545/" | ||
|
||
def http_request(self, method, params): | ||
data = {"jsonrpc": "2.0", | ||
"method": method, | ||
"params": params, | ||
"id": 1543} # TODO make nonce | ||
|
||
r = requests.post(self.api_endpoint, json=data) | ||
if r.status_code == requests.codes.ok: | ||
return r.text | ||
else: | ||
raise BadResponseError(r.status_code) | ||
|
||
def api_request(self, method, params): | ||
try: | ||
response = self.http_request(method, params) | ||
json_response = json.loads(response) | ||
if json_response.get('error', False): | ||
return APIResponse({}, json_response['error'].get('code', '-1'), | ||
json_response['error'].get('message', 'Internal error')) | ||
return APIResponse(json_response, 0) | ||
except BadResponseError: | ||
return APIResponse({}, 3, "Internal error") | ||
|
||
|
||
def getGasPrice(self): | ||
response = self.api_request("eth_gasPrice", []) | ||
response.response_dict["result"] = int(response.response_dict["result"], 16) | ||
return str(response) | ||
|
||
def getAccountByAddress(self, address): | ||
response = self.api_request("eth_getBalance", [address, "latest"]) | ||
ans = APIResponse({"address": address, | ||
"balance": int(response.response_dict["result"], 16)}) | ||
return str(ans) | ||
|
||
def getLatestBlock(self): | ||
response = self.api_request("eth_getBlockByNumber", ["latest", False]) | ||
ans = APIResponse({"number": int(response.response_dict["result"]["number"], 16), | ||
"hash": response.response_dict["result"]["hash"]}) | ||
return str(ans) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Flask goes here |