#
# Copyright (c) 2014 NSONE, Inc.
#
# License under The MIT License (MIT). See LICENSE in project root.
#
from __future__ import absolute_import

from nsone.rest.transport.base import TransportBase
from nsone.rest.errors import ResourceException, RateLimitException, \
    AuthException

try:
    import requests
    have_requests = True
except ImportError:
    have_requests = False


class RequestsTransport(TransportBase):

    def __init__(self, config):
        if not have_requests:
            raise ImportError('requests module required for RequestsTransport')
        TransportBase.__init__(self, config, self.__module__)
        self.REQ_MAP = {
            'GET': requests.get,
            'POST': requests.post,
            'DELETE': requests.delete,
            'PUT': requests.put
        }
        self._timeout = self._config.get('timeout', None)
        if isinstance(self._timeout, list) and len(self._timeout) == 2:
            self._timeout = tuple(self._timeout)

    def send(self, method, url, headers=None, data=None, params=None, files=None,
             callback=None, errback=None):
        self._logHeaders(headers)
        resp = self.REQ_MAP[method](url, headers=headers, verify=self._verify,
                                    data=data, files=files, params=params, timeout=self._timeout)
        print("URL: %s, params: %s" % (url, params))
        if resp.status_code != 200:
            if errback:
                errback(resp)
                return
            else:
                if resp.status_code == 429:
                    raise RateLimitException(
                        'rate limit exceeded', resp, resp.text,
                        by=resp.headers.get('X-RateLimit-By', 'customer'),
                        limit=resp.headers.get('X-RateLimit-Limit', 10),
                        period=resp.headers.get('X-RateLimit-Period', 1),
                        remaining=resp.headers.get('X-RateLimit-Remaining', 100)
                    )
                elif resp.status_code == 401:
                    raise AuthException('unauthorized',
                                        resp,
                                        resp.text)
                else:
                    raise ResourceException('server error',
                                            resp,
                                            resp.text)
        # TODO make sure json is valid
        try:
            jsonOut = resp.json()
        except ValueError:
            if errback:
                errback(resp)
                return
            else:
                raise ResourceException('invalid json in response',
                                        resp,
                                        resp.text)
        if callback:
            return callback(jsonOut)
        else:
            return jsonOut

TransportBase.REGISTRY['requests'] = RequestsTransport

@@ -1319,61 +1319,8 @@
 ut)%0A
-        print(%22URL: %25s, params: %25s%22 %25 (url, params))%0A
     

remove extraneous print