import json
import requests
from . import exceptions
from requests.exceptions import RequestException


class RequestMakerException(Exception):
    pass


class RequestMaker(object):

    def __init__(self, api_path, host, token):
        self.api_path = api_path
        self.host = host
        self.token = token

    def is_bad_response(self, response):
        return 400 <= response.status_code <= 500

    def headers(self):
        headers = {
            'Content-type': 'application/json',
            'Authorization': 'Bearer {0}'.format(self.token)
        }
        return headers

    def get(self, uri, query={}, **parameters):
        try:
            full_url = self.host + self.api_path + uri.format(**parameters)
            result = requests.get(
                full_url,
                headers=self.headers(),
                params=query
            )
        except RequestException:
            raise exceptions.TaigaRestException(
                full_url, 400,
                'Network error!', 'GET'
            )
        if not self.is_bad_response(result):
            print result.text
            return result
        else:
            raise exceptions.TaigaRestException(
                full_url, result.status_code,
                result.text, 'GET'
            )

    def post(self, uri, payload=None, query={}, files={}, **parameters):
        if files:
            headers = {
                'Authorization': 'Bearer {0}'.format(self.token)
            }
            data = payload
        else:
            headers = self.headers()
            data = json.dumps(payload)
        try:
            full_url = self.host + self.api_path + uri.format(**parameters)
            result = requests.post(
                full_url,
                headers=headers,
                data=data,
                params=query,
                files=files
            )
        except RequestException:
            raise exceptions.TaigaRestException(
                full_url, 400,
                'Network error!', 'POST'
            )
        if not self.is_bad_response(result):
            return result
        else:
            raise exceptions.TaigaRestException(
                full_url, result.status_code,
                result.text, 'POST'
            )

    def delete(self, uri, query={}, **parameters):
        try:
            full_url = self.host + self.api_path + uri.format(**parameters)
            result = requests.delete(
                full_url,
                headers=self.headers(),
                params=query
            )
        except RequestException:
            raise exceptions.TaigaRestException(
                full_url, 400,
                'Network error!', 'DELETE'
            )
        if not self.is_bad_response(result):
            return result
        else:
            raise exceptions.TaigaRestException(
                full_url, result.status_code,
                result.text, 'DELETE'
            )

    def put(self, uri, payload=None, query={}, **parameters):
        try:
            full_url = self.host + self.api_path + uri.format(**parameters)
            result = requests.put(
                full_url,
                headers=self.headers(),
                data=json.dumps(payload),
                params=query
            )
        except RequestException:
            raise exceptions.TaigaRestException(
                full_url, 400,
                'Network error!', 'PUT'
            )
        if not self.is_bad_response(result):
            return result
        else:
            raise exceptions.TaigaRestException(
                full_url, result.status_code,
                result.text, 'PUT'
            )

@@ -96,17 +96,16 @@
 eption%0A%0A
-%0A
 class Re
@@ -587,16 +587,107 @@
 eaders%0A%0A
+    def urljoin(self, *parts):%0A        return '/'.join(part.strip('/') for part in parts)%0A%0A
     def 
@@ -754,32 +754,45 @@
      full_url = 
+self.urljoin(
 self.host + self
@@ -776,34 +776,33 @@
 rljoin(self.host
- +
+,
  self.api_path +
@@ -791,34 +791,33 @@
 t, self.api_path
- +
+,
  uri.format(**pa
@@ -817,32 +817,33 @@
 at(**parameters)
+)
 %0A            res
@@ -1195,16 +1195,17 @@
         
+#
 print re
@@ -1738,32 +1738,45 @@
      full_url = 
+self.urljoin(
 self.host + self
@@ -1760,34 +1760,33 @@
 rljoin(self.host
- +
+,
  self.api_path +
@@ -1775,34 +1775,33 @@
 t, self.api_path
- +
+,
  uri.format(**pa
@@ -1801,32 +1801,33 @@
 at(**parameters)
+)
 %0A            res
@@ -2483,32 +2483,45 @@
      full_url = 
+self.urljoin(
 self.host + self
@@ -2505,34 +2505,33 @@
 rljoin(self.host
- +
+,
  self.api_path +
@@ -2520,34 +2520,33 @@
 t, self.api_path
- +
+,
  uri.format(**pa
@@ -2546,32 +2546,33 @@
 at(**parameters)
+)
 %0A            res
@@ -3200,24 +3200,37 @@
  full_url = 
+self.urljoin(
 self.host + 
@@ -3226,18 +3226,17 @@
 elf.host
- +
+,
  self.ap
@@ -3245,10 +3245,9 @@
 path
- +
+,
  uri
@@ -3267,16 +3267,17 @@
 ameters)
+)
 %0A       

Add urljoin method