Skip to content

Updates API endpoint #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 134 additions & 135 deletions lymbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,139 +2,138 @@
import urllib2
import json


class Lymbix:

API_BASE = 'https://gyrus.lymbix.com/';
TONALIZE_MULTIPLE = 'tonalize_multiple';
TONALIZE_DETAILED = 'tonalize_detailed';
TONALIZE = 'tonalize';
FLAG_RESPONSE = 'flag_response';

def __init__(self, authentication_key):
'''
Args:
-authentication_key: your Lymbix authentication key
'''
if authentication_key == None or len(authentication_key) == 0:
raise Exception('You must include your authentication key')

self.authentication_key = authentication_key

''' utility functions '''

def _get_headers(self):
headers = {
'Authentication': self.authentication_key,
'Accept': 'application/json',
'Version': '2.2'}
return headers

''' api methods '''

def tonalize_multiple(self, articles, options = None):
'''
tonalize multiple articles

Args:
-articles: articles to tonalize
-options: additional parameters (reference_ids and return_fields)

Returns:
-see the api documentation for the format of this object
'''
if articles == None or len(articles) == 0:
raise Exception('You must include articles to tonalize')

url = self.API_BASE + self.TONALIZE_MULTIPLE
data = {'articles': articles}
if options != None: data.update(options)
for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)

headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return json.loads(response.read())

def tonalize_detailed(self, article, options = None):
'''
tonalize an article

Args:
-article: article to tonalize
-options: additional parameters (reference_id and return_fields)

Returns:
-see the api documentation for the format of this object
'''
if article == None or len(article) == 0:
raise Exception('You must include an article to tonalize')

url = self.API_BASE + self.TONALIZE_DETAILED
data = {'article': article}
if options != None: data.update(options)
for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)

headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return json.loads(response.read())

def tonalize(self, article, options = None):
'''
tonalize an article

Args:
-article: article to tonalize
-options: additional parameters (reference_id and return_fields)

Returns:
-see the api documentation for the format of this object
'''
if article == None or len(article) == 0:
raise Exception('You must include an article to tonalize')

url = self.API_BASE + self.TONALIZE
data = {'article': article}
if options != None: data.update(options)
for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)

headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return json.loads(response.read())

def flag_response(self, phrase, api_method = None, api_version = '2.2', callback_url = None, options = None):
'''
flag a response as inaccurate

Args:
-phrase: the phrase that returns an inaccurate response
-api_method: the method that returns an inaccurate response
-api_version: the version that returns an inaccurate response
-callback_url: a url to call when the phrase has been re-rated
-options: additional parameters (reference_id)

Returns:
-see the api documentation for the format of this object
'''
if phrase == None or len(phrase) == 0:
raise Exception('You must include a phrase to flag')

url = self.API_BASE + self.FLAG_RESPONSE
data = {'phrase': phrase}

if (api_method != None): data['apiMethod'] = api_method
if (api_version != None): data['apiVersion'] = api_version
if (callback_url != None): data['callbackUrl'] = callback_url
if (options != None): data.update(options)

for key, value in data.iteritems(): data[key] = json.dumps(value)
data = urllib.urlencode(data)

headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
return response.read()

API_BASE = 'http://api.lymbix.com/'
TONALIZE_MULTIPLE = 'tonalize_multiple'
TONALIZE_DETAILED = 'tonalize_detailed'
TONALIZE = 'tonalize'
FLAG_RESPONSE = 'flag_response'

def __init__(self, authentication_key):
'''
Args:
-authentication_key: your Lymbix authentication key
'''
if not authentication_key:
raise Exception('You must include your authentication key')

self.authentication_key = authentication_key

''' utility functions '''

def _get_headers(self):
headers = {
'Authentication': self.authentication_key,
'Accept': 'application/json',
'Version': '2.2'}
return headers

def _prep_data(self, data, options):
if options:
data.update(options)
for key, value in data.iteritems():
data[key] = json.dumps(value)
return urllib.urlencode(data)

def _call(self, url, data, returns_json=False):
headers = self._get_headers()
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
if returns_json:
return json.loads(response.read())
return response.read()

''' api methods '''

def tonalize_multiple(self, articles, options=None):
'''
tonalize multiple articles

Args:
-articles: articles to tonalize
-options: additional parameters (reference_ids and return_fields)

Returns:
-see the api documentation for the format of this object
'''
if not articles:
raise Exception('You must include articles to tonalize')

url = self.API_BASE + self.TONALIZE_MULTIPLE
data = {'articles': articles}
data = self._prep_data(data, options)

return self._call(url, data, returns_json=True)

def tonalize_detailed(self, article, options=None):
'''
tonalize an article

Args:
-article: article to tonalize
-options: additional parameters (reference_id and return_fields)

Returns:
-see the api documentation for the format of this object
'''
if not article:
raise Exception('You must include an article to tonalize')

url = self.API_BASE + self.TONALIZE_DETAILED

data = {'article': article}
data = self._prep_data(data, options)

return self._call(url, data, returns_json=True)

def tonalize(self, article, options=None):
'''
tonalize an article

Args:
-article: article to tonalize
-options: additional parameters (reference_id and return_fields)

Returns:
-see the api documentation for the format of this object
'''
if not article:
raise Exception('You must include an article to tonalize')

url = self.API_BASE + self.TONALIZE
data = {'article': article}
data = self._prep_data(data, options)

return self._call(url, data, returns_json=True)

def flag_response(self, phrase, api_method=None, api_version='2.2', callback_url=None, options=None):
'''
flag a response as inaccurate

Args:
-phrase: the phrase that returns an inaccurate response
-api_method: the method that returns an inaccurate response
-api_version: the version that returns an inaccurate response
-callback_url: a url to call when the phrase has been re-rated
-options: additional parameters (reference_id)

Returns:
-see the api documentation for the format of this object
'''
if not phrase:
raise Exception('You must include a phrase to flag')

url = self.API_BASE + self.FLAG_RESPONSE

data = {'phrase': phrase}
if (api_method is not None):
data['apiMethod'] = api_method
if (api_version is not None):
data['apiVersion'] = api_version
if (callback_url is not None):
data['callbackUrl'] = callback_url
data = self._prep_data(data, options)

return self._call(url, data)