PBinCLI/pbincli/api.py

60 lines
1.8 KiB
Python
Raw Normal View History

2017-02-19 10:26:22 +00:00
import requests
2018-05-22 17:44:18 +00:00
class PrivateBin:
2018-05-23 08:14:35 +00:00
def __init__(self, server, proxy=None):
2018-05-22 17:44:18 +00:00
self.server = server
2017-02-19 10:26:22 +00:00
self.headers = {'X-Requested-With': 'JSONHttpRequest'}
2018-05-23 08:14:35 +00:00
if proxy:
2018-05-22 17:44:18 +00:00
self.proxy = {proxy.split('://')[0]: proxy}
2018-02-12 14:28:18 +00:00
else:
2018-05-22 17:44:18 +00:00
self.proxy = {}
2017-02-20 12:13:42 +00:00
2017-02-20 12:13:42 +00:00
def post(self, request):
2019-06-02 14:04:38 +00:00
result = requests.post(
url = self.server,
headers = self.headers,
proxies = self.proxy,
data = request)
try:
return result.json()
except ValueError as e:
print("ERROR: Unable parse response as json. Received (size = {}):\n".format(len(result.text), result.text))
exit(1)
2017-02-20 12:13:42 +00:00
def get(self, request):
2019-06-02 14:04:38 +00:00
return requests.get(
url = self.server + "?" + request,
headers = self.headers,
proxies = self.proxy).json()
def delete(self, request):
result = requests.post(
url = self.server,
headers = self.headers,
proxies = self.proxy,
data = request)
# using try as workaround for versions < 1.3 due to we cant detect
# if server used version 1.2, where auto-deletion is added
try:
return result.json()
except ValueError as e:
# unable parse response as json because it can be empty (1.2), so simulate correct answer
from json import loads as json_loads
return json_loads('{"status":0}')
2017-02-20 12:13:42 +00:00
2019-06-02 14:04:38 +00:00
def getVersion(self):
jsonldSchema = requests.get(
url = self.server + '?jsonld=paste',
proxies = self.proxy).json()
return jsonldSchema['@context']['v']['@value'] \
if ('@context' in jsonldSchema and
'v' in jsonldSchema['@context'] and
'@value' in jsonldSchema['@context']['v']) \
else 1