Skip to content

Instantly share code, notes, and snippets.

@fabiobatalha
Last active April 26, 2018 14:42
Show Gist options
  • Save fabiobatalha/65280e00ef3681d9f6f5894b4416b1d4 to your computer and use it in GitHub Desktop.
Save fabiobatalha/65280e00ef3681d9f6f5894b4416b1d4 to your computer and use it in GitHub Desktop.
OTS rudmentar interface
import os
from json import JSONDecodeError
import requests
OTS_API_HOST = os.environ.get('OTS_API_HOST', 'http://pkp-udev.lib.sfu.ca/')
OTS_CITATION_STYLE_HASH = os.environ.get('OTS_CITATION_STYLE_HASH', '3f0f7fede090f24cc71b7281073996be')
OTS_CONVERSION_STAGE = os.environ.get('OTS_CONVERSION_STAGE', '5')
class OTSError(Exception):
pass
class InternalServerError(OTSError):
pass
class AuthenticationError(OTSError):
pass
class InvalidJSONReturn(OTSError):
pass
class Restful:
def __init__(self, user, access_token, api_host=OTS_API_HOST):
self.api_host = api_host
self.user = user
self.access_token = access_token
def submit(self, input_filename, content, citation_style_hash=OTS_CITATION_STYLE_HASH):
"""Example request:
http://example.com/api/job/submit
POST parameters:
'fileName' : 'document.docx'
'citationStyleHash' : 'c6de5efe3filenamefilename294b26391ea343053c19a84',
'fileContent' : '...'
Example response:
{"status":"success","id":123}
"""
# parameters
URL = self.api_host + 'api/job/submit'
params = {
'email': self.user,
'access_token': self.access_token,
'fileName': input_filename,
'fileContent': content,
'citationStyleHash': citation_style_hash
}
# request
response = requests.post(URL, params)
# response content
if response.status_code == 500:
raise InternalServerError(response.text)
if response.status_code in [403, 401]:
# PKP is using 403 for unauthorized access, we asked them to fix it to raise 401.
# This should be reviwed if they fix it in the OTS API
raise AuthenticationError('user name or user token invalid')
try:
return response.json()
except JSONDecodeError as e:
raise InvalidJSONReturn()
def status(self, job_id):
"""Example request:
http://example.com/api/job/status?email=user@example.com&access_token=token&id=123
Example response:
{"status":"success","jobStatus":0,"jobStatusDescription":"Pending"}
"""
# parameters
URL = self.api_host + 'api/job/status'
params = {
'email': self.user,
'access_token': self.access_token,
'id': job_id,
}
# request
response = requests.get(URL, params)
# response content
if response.status_code != 500:
return response.json()
def retrieve(self, job_id, conversion_stage=OTS_CONVERSION_STAGE):
"""Example request:
http://example.com/api/job/retrieve?email=user@example.com&access_token=token&id=123&conversionStage=10
Example response:
The requested document or a JSON string with an error message.
"""
# parameters
URL = self.api_host + 'api/job/retrieve'
params = {
'id': job_id,
'email': self.user,
'access_token': self.access_token,
'conversionStage': conversion_stage
}
# request
response = requests.get(URL, params)
# response content
if response.status_code != 500:
try:
# error status is only returned in json format.
return response.json()
except JSONDecodeError:
# If no raise exception it means the content was retrieved
result = {
'result': response.text,
'status': 'success'
}
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment