Instantly share code, notes, and snippets.

What would you like to do? API Python Wrapper
import hashlib
import time
import requests
class ReadableAPIClient:
API Client for, written for Python 3
See API documentation:
NOTE: Very limited exception handling, expand and adapt as needed
Example usage:
readable_client = ReadableAPIClient(**{"api_key": "your_api_string"})
text_scoring = readable_client.check_text_readability("You can see your usage of the API in your account administration area.")
url_scoring = readable_client.check_url_readability("")
highlights_scoring = readable_client.retrieve_highlighted_issues("a2345c78f")
text_data = readable_client.get_readability_with_highlights(
source="You can see your usage of the API in your account administration area",
url_data = readable_client.get_readability_with_highlights(
def __init__(self, *args, **kwargs):
self._api_key = kwargs.get('api_key')
def _prepare_headers(self):
timestamp = str(int(time.time()))
combined = '{}{}'.format(self._api_key, timestamp).encode('utf8')
signing = hashlib.md5()
headers = {'API_SIGNATURE': signing.hexdigest().encode('utf8'),
'API_REQUEST_TIME': timestamp}
return headers
def _post(self, endpoint, payload):
request_url = ''.join([self.BASE_API_URL, endpoint])
headers = self._prepare_headers()
response =, data=payload, headers=headers)
return response
def check_text_readability(self, text):
ENDPOINT = 'text/'
payload = {'text': text}
response = self._post(ENDPOINT, payload)
return response
def check_url_readability(self, url, extract=False):
ENDPOINT = 'url/'
payload = {'url': url, 'extract': extract}
response = self._post(ENDPOINT, payload)
return response
def retrieve_highlighted_issues(self, score_id):
ENDPOINT = 'highlight/'
payload = {'score_id': score_id}
response = self._post(ENDPOINT, payload)
return response
def get_readability_with_highlights(self, analysis_type, source, extract=False):
if analysis_type == 'url':
scoring_response = self.check_url_readability(source, extract)
elif analysis_type == 'text':
scoring_response = self.check_text_readability(source)
raise Exception("Set analysis_type parameter to 'url' or 'text'")
scoring_data = scoring_response.json()
highlights_response = self.retrieve_highlighted_issues(scoring_data['score_id'])
return {
'scoring': scoring_data,
'highlights': highlights_response.json(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment