import requests
from requests import HTTPError


class Bioportal(object):

    '''A Python binding of the BioPortal REST API
    (http://data.bioontology.org/documentation)'''

    BASE_URL = 'http://data.bioontology.org'

    def __init__(self, api_key):
        self.apikey = api_key

    def classes(self, search_query, **kwargs):

        # http://data.bioontology.org/documentation#nav_search

        endpoint = '/search'
        full_url = Bioportal.BASE_URL + endpoint

        payload = kwargs
        payload['q'] = search_query
        payload['apikey'] = self.apikey

        return self._bioportal_api_request(full_url, payload)

    def annotator(self, text, **kwargs):

        # http://data.bioontology.org/documentation#nav_annotator

        endpoint = '/annotator'
        full_url = Bioportal.BASE_URL + endpoint

        payload = kwargs
        payload['text'] = text
        payload['apikey'] = self.apikey

        complete_annotations = self._bioportal_api_request(full_url, payload)

        return complete_annotations

    def recommender(self, text_or_keywords, **kwargs):

        # http://data.bioontology.org/documentation#nav_recommender

        endpoint = '/recommender'
        full_url = Bioportal.BASE_URL + endpoint

        payload = kwargs
        payload['input'] = text_or_keywords
        payload['apikey'] = self.apikey

        return self._bioportal_api_request(full_url, payload)

    def _bioportal_api_request(self, url, payload):

        r = requests.get(url, params=payload)
        json_response = r.json()

        try:
            # This will raise an HTTPError if the HTTP request returned an
            # unsuccessful status code.
            r.raise_for_status()
        except HTTPError:
            if 'errors' in json_response.keys():
                error_messages = json_response['errors']
                error_message = '\n'.join(error_messages)
            elif 'error' in json_response.keys():
                error_message = json_response['error']

            raise HTTPError(error_message)

        return json_response

@@ -9,16 +9,30 @@
 equests%0A
+import urllib%0A
 from req
@@ -56,16 +56,17 @@
 Error%0A%0A%0A
+%0A
 class Bi
@@ -1429,32 +1429,690 @@
 _url, payload)%0A%0A
+    def ontology_class(self, ontology, cls_id):%0A        '''%0A        Just supports the /ontologies/:ontology/classes/:cls endpoint, which%0A        returns information about one class%0A%0A        ontology: name of the ontology%0A        cls_id: @id of the class. Ex: http://www.radlex.org/RID/#RID43314%0A        '''%0A%0A        # http://data.bioontology.org/documentation#Class%0A%0A        escaped_cls_id = urllib.quote(cls_id, safe='')%0A        endpoint = '/ontologies/%7B%7D/classes/%7B%7D'.format(ontology, escaped_cls_id)%0A        full_url = Bioportal.BASE_URL + endpoint%0A%0A        payload = %7B'apikey': self.apikey%7D%0A%0A        return self._bioportal_api_request(full_url, payload)%0A%0A
     def _bioport

add support for a new endpoint