Commit 9658bdbe authored by abuddenberg's avatar abuddenberg
Browse files

Add support for articles and webpages. Improve support for reports.

Fix various bugs including one where a trailing slash in GCIS urls would
cause all api calls to return the contents of /.json
parent c7927abf
...@@ -235,6 +235,7 @@ class Dataset(GcisObject): ...@@ -235,6 +235,7 @@ class Dataset(GcisObject):
'spatial_ref_sys', 'spatial_res', 'spatial_extent', 'doi', 'name', 'url', 'uri', 'spatial_ref_sys', 'spatial_res', 'spatial_extent', 'doi', 'name', 'url', 'uri',
'identifier', 'release_dt', 'attributes'] 'identifier', 'release_dt', 'attributes']
#TODO: This hack has got to go.
self._identifiers = known_ids self._identifiers = known_ids
#Private attributes for handling date parsing #Private attributes for handling date parsing
...@@ -244,7 +245,7 @@ class Dataset(GcisObject): ...@@ -244,7 +245,7 @@ class Dataset(GcisObject):
super(Dataset, self).__init__(data, fields=self.gcis_fields, trans=trans) super(Dataset, self).__init__(data, fields=self.gcis_fields, trans=trans)
self.identifier = self._identifiers[self.name] if self._identifiers and self.name in self._identifiers else None self.identifier = self._identifiers[self.name] if self._identifiers and self.name in self._identifiers else self.identifier
def __repr__(self): def __repr__(self):
return '<Dataset: id:{id} name:{name}>'.format(id=self.identifier, name=self.name) return '<Dataset: id:{id} name:{name}>'.format(id=self.identifier, name=self.name)
...@@ -432,3 +433,38 @@ class Parent(Gcisbase): ...@@ -432,3 +433,38 @@ class Parent(Gcisbase):
def __str__(self): def __str__(self):
return self.__repr__() return self.__repr__()
class Article(Gcisbase):
def __init__(self, data, trans=()):
self.gcis_fields = ['files', 'doi', 'contributors', 'title', 'url', 'notes', 'uri',
'journal_identifier', 'journal_pages', 'cited_by', 'href', 'parents', 'year',
'journal_vol', 'references', 'identifier']
super(Article, self).__init__(data, fields=self.gcis_fields, trans=trans)
def as_json(self, indent=0, omit_fields=('files', 'parents', 'contributors', 'references', 'cited_by')):
return super(Article, self).as_json(omit_fields=omit_fields)
def __repr__(self):
return '<Article: id:{id} title:{t}>'.format(id=self.identifier, t=self.title)
def __str__(self):
return self.__repr__()
class Webpage(Gcisbase):
def __init__(self, data, trans=()):
self.gcis_fields = ['files', 'contributors', 'title', 'url', 'uri', 'cited_by', 'href', 'references',
'parents', 'access_date', 'identifier']
super(Webpage, self).__init__(data, fields=self.gcis_fields, trans=trans)
def as_json(self, indent=0, omit_fields=('files', 'parents', 'contributors', 'references', 'cited_by')):
return super(Webpage, self).as_json(omit_fields=omit_fields)
def __repr__(self):
return '<Webpage id:{id} title:{t}>'.format(id=self.identifier, t=self.title)
def __str__(self):
return self.__repr__()
...@@ -6,7 +6,7 @@ import requests ...@@ -6,7 +6,7 @@ import requests
import yaml import yaml
import getpass import getpass
from domain import Figure, Image, Dataset, Activity, Person, Organization from domain import Figure, Image, Dataset, Activity, Person, Organization, Article, Webpage, Report
def check_image(fn): def check_image(fn):
...@@ -99,6 +99,10 @@ class GcisClient(object): ...@@ -99,6 +99,10 @@ class GcisClient(object):
if username is None or api_key is None: if username is None or api_key is None:
username, api_key = get_credentials(self.base_url) username, api_key = get_credentials(self.base_url)
#Squash trailing slash in base_url, if given
if self.base_url.endswith('/'):
self.base_url = self.base_url[:-1]
self.s = requests.Session() self.s = requests.Session()
self.s.auth = (username, api_key) self.s.auth = (username, api_key)
self.s.headers.update({'Accept': 'application/json'}) self.s.headers.update({'Accept': 'application/json'})
...@@ -188,7 +192,7 @@ class GcisClient(object): ...@@ -188,7 +192,7 @@ class GcisClient(object):
def create_image(self, image, report_id=None, figure_id=None): def create_image(self, image, report_id=None, figure_id=None):
url = '{b}/image/'.format(b=self.base_url) url = '{b}/image/'.format(b=self.base_url)
resp = self.s.post(url, data=image.as_json(), verify=False) resp = self.s.post(url, data=image.as_json(), verify=False)
print image.as_json()
if resp.status_code != 200: if resp.status_code != 200:
return resp return resp
...@@ -316,12 +320,14 @@ class GcisClient(object): ...@@ -316,12 +320,14 @@ class GcisClient(object):
resp = self.s.get(url, verify=False) resp = self.s.get(url, verify=False)
return resp.status_code, resp.text return resp.status_code, resp.text
@http_resp
def get_report(self, report_id): def get_report(self, report_id):
url = '{b}/report/{id}'.format(b=self.base_url, id=report_id) url = '{b}/report/{id}'.format(b=self.base_url, id=report_id)
resp = self.s.get(url, verify=False) resp = self.s.get(url, verify=False)
return resp.json() try:
return Report(resp.json())
except ValueError:
raise Exception(resp.text)
@exists @exists
def report_exists(self, report_id): def report_exists(self, report_id):
...@@ -505,6 +511,62 @@ class GcisClient(object): ...@@ -505,6 +511,62 @@ class GcisClient(object):
url = '{b}/person/{pid}'.format(b=self.base_url, pid=person.identifier) url = '{b}/person/{pid}'.format(b=self.base_url, pid=person.identifier)
return self.s.delete(url, verify=False) return self.s.delete(url, verify=False)
@exists
def article_exists(self, article_id):
url = '{b}/article/{aid}'.format(b=self.base_url, aid=article_id)
return self.s.head(url, verify=False)
def get_article(self, article_id):
url = '{b}/article/{aid}'.format(b=self.base_url, aid=article_id)
resp = self.s.get(url, verify=False)
try:
return Article(resp.json())
except ValueError:
raise Exception(resp.text)
@http_resp
def create_article(self, article):
url = '{b}/article/'.format(b=self.base_url)
return self.s.post(url, data=article.as_json(), verify=False)
@http_resp
def update_article(self, article):
url = '{b}/article/{aid}'.format(b=self.base_url, aid=article.identifier)
return self.s.post(url, data=article.as_json(), verify=False)
@http_resp
def delete_article(self, article):
url = '{b}/article/{aid}'.format(b=self.base_url, aid=article.identifier)
return self.s.delete(url, verify=False)
@exists
def webpage_exists(self, webpage_id):
url = '{b}/webpage/{id}'.format(b=self.base_url, id=webpage_id)
return self.s.head(url, verify=False)
def get_webpage(self, webpage_id):
url = '{b}/webpage/{id}'.format(b=self.base_url, id=webpage_id)
resp = self.s.get(url, verify=False)
try:
return Webpage(resp.json())
except ValueError:
raise Exception(resp.text)
@http_resp
def create_webpage(self, webpage):
url = '{b}/webpage/'.format(b=self.base_url)
return self.s.post(url, data=webpage.as_json(), verify=False)
@http_resp
def update_webpage(self, webpage):
url = '{b}/webpage/{aid}'.format(b=self.base_url, aid=webpage.identifier)
return self.s.post(url, data=webpage.as_json(), verify=False)
@http_resp
def delete_webpage(self, webpage):
url = '{b}/webpage/{aid}'.format(b=self.base_url, aid=webpage.identifier)
return self.s.delete(url, verify=False)
@exists @exists
def organization_exists(self, org_id): def organization_exists(self, org_id):
url = '{b}/organization/{org_id)'.format(b=self.base_url, org_id=org_id) url = '{b}/organization/{org_id)'.format(b=self.base_url, org_id=org_id)
...@@ -681,4 +743,7 @@ class GcisClient(object): ...@@ -681,4 +743,7 @@ class GcisClient(object):
return [re.match(r'\[.+\] \{(.+)\} (.*)', r).groups() for r in resp.json()] return [re.match(r'\[.+\] \{(.+)\} (.*)', r).groups() for r in resp.json()]
# return resp.json() # return resp.json()
else: else:
raise Exception(resp.text) raise Exception('Lookup failed:\nQuery:{q}\nType:{t}\nResponse:\n{r}'.format(q=name, t=pub_type, r=resp.text))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment