Store URL paths in the class

so that there's no need to build the whole URL every time.
Also:
- stop converting back and forth dictionaries and json strings
- rename response to r
- return tuples when there's an error to know the error
- use inline if
master
juga 2 years ago
parent 48204d83be
commit 0ca33a1dec

@ -7,6 +7,14 @@ class GNUnetRest:
def __init__(self, host="localhost", port="7776"):
self.host = host
self.port = port
self.base_url = "http://{}:{}/".format(host, port)
self.identity = self.base_url + "identity/"
self.identity_name = self.identity + "name/"
self.identity_hash = self.identity + "pubkey/"
self.identity_subsystem = self.identity + "subsystem/"
self.identity_namestore = self.identity_subsystem + "namestore/"
self.namestore = self.base_url + "namestore/"
self.gns = self.base_url + "gns/"
def is_port_open(self):
# check if port is open
@ -24,65 +32,28 @@ class GNUnetRest:
# Identity Section
def identity_list(self):
"""Returns a list of all identities."""
requeststring = "http://" + self.host + ":" + self.port + "/identity/"
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val
r = requests.get(self.identity)
return r.json() if r.ok else (False, r.reason)
def identity_hash(self, thename):
def fetch_identity_name(self, thename):
"""Returns the public key of an identity specified by name."""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/name/"
+ thename
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val["pubkey"]
r = requests.get(self.identity_hash + thename)
return r.json()['name'] if r.ok else (False, r.reason)
def identity_name(self, thepubkey):
def fetch_identity_hash(self, thepubkey):
"""Returns the name of an identity specified by public key."""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/pubkey/"
+ thepubkey
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val["name"]
r = requests.get(self.identity_name + thepubkey)
return r.json()['pubkey'] if r.ok else (False, r.reason)
def namestore_identity(self):
def fetch_identity_namestore(self):
"""Returns the default identity of subsystem 'namestore'."""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/subsystem/namestore"
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val["name"]
r = requests.get(self.identity_namestore)
return r.json()['name'] if r.ok else (False, r.reason)
def create_identity(self, thename):
"""Creates Identy, returns True if successful, False if not."""
requeststring = "http://" + self.host + ":" + self.port + "/identity"
datadict = {"name": thename}
dataparams = json.dumps(datadict)
respdata = requests.post(requeststring, data=dataparams)
if "201" in str(respdata):
# successful
return True
else:
# not successful
return False
return ()
r = requests.post(self.identity, {"name": thename})
return r.ok if r.ok else (False, r.reason)
def change_identity_name(self, thename, newname):
"""Changes name of identity.
@ -90,39 +61,16 @@ class GNUnetRest:
Returns True if successful, False if not.
"""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/name/"
+ thename
r = requests.put(
self.identity_name + thename,
{"newname": newname}
)
datadict = {"newname": newname}
dataparams = json.dumps(datadict)
respdata = requests.put(requeststring, data=dataparams)
if "204" in str(respdata):
# successful
return True
else:
return False
return True if r.ok else (False, r.reason)
def delete_identity(self, thename):
"""Deletes Identity, returns True if successful, false if not."""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/name/"
+ thename
)
respdata = requests.delete(requeststring)
if "204" in str(respdata):
# successful
return True
else:
return False
r = requests.delete(self.identity_name + thename)
return True if r.ok else (False, r.reason)
# Namestore Section
@ -132,71 +80,40 @@ class GNUnetRest:
Returns True if successful False if not.
"""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/identity/subsystem/"
+ thename
)
datadict = {"subsystem": "namestore"}
dataparams = json.dumps(datadict)
respdata = requests.put(requeststring, data=dataparams)
if "204" in str(respdata):
return True
else:
return False
r = requests.put(self.identity_subsystem, {"subsystem": "namestore"})
# r =
return True if r.ok else (False, r.reason)
def list_namestore_entries(self, thezone):
"""Returns a list of all namestore entries."""
requeststring = (
"http://" + self.host + ":" + self.port + "/namestore/" + thezone
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val
# return(respdata)
r = requests.get(self.namestore + thezone)
return r.json() if r.ok else (False, r.reason)
def add_namestore_entry(self, thezone, thename, thevalue, thetype):
"""Adds a namestore entry, returns true if successful, false if not."""
requeststring = (
"http://" + self.host + ":" + self.port + "/namestore/" + thezone
)
dataparams = (
'{"data": [{"value": "'
+ thevalue
+ '", "record_type": "'
+ thetype
+ '", "expiration_time":"1d", "private": false, "relative_expiration": false, "supplemental": false, "shadow": false}], "record_name": "'
+ thename
+ '"}'
)
respdata = requests.post(requeststring, data=dataparams)
if "204" in str(respdata):
return True
else:
return False
data = {
'value': thevalue,
'record_type': thetype,
'record_name': thename,
"expiration_time": "1d",
"private": False,
"relative_expiration": False,
"supplemental": False,
"shadow": False,
}
r = requests.post(self.namestore + thezone, data)
return True if r.ok else (False, r.reason)
def change_namestore_entry(self, thezone, thename, thevalue, thetype):
# changes a namestore entry, returns true if successful, false if not
requeststring = (
"http://" + self.host + ":" + self.port + "/namestore/" + thezone
)
dataparams = (
'{"data": [{"value": "'
+ thevalue
+ '", "record_type": "'
+ thetype
+ '", "expiration_time":"1d", "private": false, "relative_expiration": false, "supplemental": false, "shadow": false}], "record_name": "'
+ thename
+ '"}'
)
respdata = requests.put(requeststring, data=dataparams)
if "204" in str(respdata):
return True
else:
return False
data = {
'value': thevalue,
'record_type': thetype,
'record_name': thename,
}
r = requests.put(self.namestore + thezone, data)
return True if r.ok else (False, r.reason)
def delete_namestore_entry(self, thezone, thename):
"""Deletes a namestore entry.
@ -204,53 +121,18 @@ class GNUnetRest:
Returns true if successful, false if not.
"""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/namestore/"
+ thezone
+ "/"
+ thename
)
respdata = requests.delete(requeststring)
if "204" in str(respdata):
return True
else:
return False
r = requests.delete(self.namestore + thezone + "/" + thename)
return True if r.ok else (False, r.reason)
# GNS section
def list_gns_record(self, thezone, thename):
"""Returns a list of values and record types with the same record name."""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/gns/"
+ thename
+ "."
+ thezone
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val
r = requests.get(self.gns + thename + "." + thezone)
return r.json() if r.ok else (False, r.reason)
def list_gns_record_type(self, thezone, thename, thetype):
"""Returns a list of values of a certain name and record type."""
requeststring = (
"http://"
+ self.host
+ ":"
+ self.port
+ "/gns/"
+ thename
+ "."
+ thezone
+ "?record_type="
+ thetype
r = requests.get(
self.gns + thename + "." + thezone + "?record_type=" + thetype
)
respdata = requests.get(requeststring).json()
val = json.loads(json.dumps(respdata))
return val
return r.json() if r.ok else (False, r.reason)

Loading…
Cancel
Save