In [1]:
import sys
PY3 = sys.version > '3'
if PY3:
import urllib.request as urllib2
else:
import urllib2
SERVER_URL = "https://www.ebi.ac.uk/pdbe/api"
def make_request(url, data):
request = urllib2.Request(url)
try:
url_file = urllib2.urlopen(request, data)
except urllib2.HTTPError as e:
if e.code == 404:
print("[NOTFOUND %d] %s" % (e.code, url))
else:
print("[ERROR %d] %s" % (e.code, url))
return None
return url_file.read().decode()
def get_request(url, arg, pretty=False):
full_url = "%s/%s/%s?pretty=%s" % (SERVER_URL, url, arg, str(pretty).lower())
return make_request(full_url, None)
def post_request(url, data, pretty=False):
full_url = "%s/%s/?pretty=%s" % (SERVER_URL, url, str(pretty).lower())
if isinstance(data, (list, tuple)):
data = ",".join(data)
return make_request(full_url, data.encode())
In [2]:
summary = "/pdb/entry/summary"
experiment = "/pdb/entry/experiment"
sifts = "/mappings"
We can make GET resquests simply by doing:
In [11]:
print(get_request(experiment, "1cbs", True))
POST requests passing a string:
In [4]:
print(post_request(summary, "1cbs, 1otz, 2ktn", True))
A list:
In [ ]:
print(post_request(summary, ["1cbs", "1otz", "2ktn"], True))
Or a tuple:
In [6]:
response = post_request(summary, ("1cbs", "1otz", "2ktn"))
print(response)
Please note that we have not used the pretty=True argument in the last request. This is the best way to access the REST API programatically as, by eliminating all the whitespaces, you will save bandwidth and, therefore, time.
The response is a string that can be parsed into a Python dictionary using the json module:
In [7]:
import json
entries = json.loads(response)
And now we can access the elements individually:
In [ ]:
print(entries["1cbs"][0]["experimental_method"])
print(entries["1cbs"][0]["entry_authors"])
In [12]:
entries["1cbs"]
Out[12]:
In [10]:
for k in entries.keys():
print entries[k]
In [ ]: