""" A utility class to help make API calls. """

from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
# noinspection PyCompatibility
from builtins import str

from requests import Session
from gobble.config import OS_HOST

# TO DO: Simplify the request module


def optional(method):
    def new_method(self):
        private_method = '_' + method.__name__
        if getattr(self, private_method):
            return method(self)
        else:
            return ''
    return new_method


class APIRequest(object):
    def __init__(self,
                 host,
                 session=Session(),
                 verb='get',
                 schema='https',
                 path=None,
                 query=None,
                 port=None):

        self.host = host
        self.session = session
        self.verb = verb

        self._schema = schema
        self._port = port
        self._path = path
        self._query = query or {}

    @property
    def schema(self):
        return self._schema + '://'

    @property
    def url(self):
        return self.schema + self.host + self.port + self.path + self.query

    @property
    @optional
    def port(self):
        return ':' + str(self._port)

    @property
    @optional
    def query(self):
        parameters = ['%s=%s' % (k, v) for k, v in self._query.items()]
        return '?' + '&'.join(parameters)

    @property
    @optional
    def path(self):
        if self._path:
            return '/' + '/'.join(self._path)

    def __call__(self, **parameters):
        if parameters:
            self._query.update(parameters)
        return getattr(self.session, self.verb)(self.url)


class APISession(object):
    session = Session()

    check_user = APIRequest(
        OS_HOST,
        session=session,
        path=['oauth', 'check'],
        schema='http'
    )
    check_permission = APIRequest(
        OS_HOST,
        session=session,
        path=['permit', 'check'],
        schema='http'
    )
    oauth_callback = APIRequest(
        OS_HOST,
        session=session,
        path=['oauth', 'callback'],
        schema='http'
    )
    search_users = APIRequest(
        OS_HOST,
        session=session,
        path=['search', 'user'],
        schema='http'
    )

    search_packages = APIRequest(
        OS_HOST,
        session=session,
        path=['search', 'package'],
        schema='http'
    )

@@ -988,16 +988,24 @@
 b = verb
+.lower()
 %0A%0A      
@@ -1889,18 +1889,20 @@
     
-check_user
+authenticate
  = A
@@ -1965,29 +1965,28 @@
      path=%5B'
-oauth
+user
 ', 'check'%5D,
@@ -2022,24 +2022,17 @@
     
-check_permission
+authorize
  = A
@@ -2103,23 +2103,25 @@
 h=%5B'
-permit', 'check
+user', 'authorize
 '%5D,%0A
@@ -2421,17 +2421,16 @@
 '%0A    )%0A
-%0A
     sear
@@ -2541,28 +2541,252 @@
         schema='http'%0A    )%0A
+    update_user = APIRequest(%0A        OS_HOST,%0A        session=session,%0A        path=%5B'user', 'update'%5D,%0A        schema='http',%0A        verb='POST'%0A    )%0A%0A    def __str__(self):%0A        return 'hello'%0A%0A    __repr__ = __str__

Implement the new API endpoints in the APISession class.