import sys
import json
import requests

try:
  from urllib.parse import urljoin
except ImportError:
  from urlparse import urljoin

from messagebird.base         import Base
from messagebird.balance      import Balance
from messagebird.error        import Error
from messagebird.hlr          import HLR
from messagebird.message      import Message
from messagebird.voicemessage import VoiceMessage
from messagebird.lookup       import Lookup
from messagebird.verify       import Verify

ENDPOINT       = 'https://rest.messagebird.com'
CLIENT_VERSION = '1.2.0'
PYTHON_VERSION = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])


class ErrorException(Exception):
  def __init__(self, errors):
    self.errors = errors
    message = ' '.join([str(e) for e in self.errors])
    super(ErrorException, self).__init__(message)


class Client(object):
  def __init__(self, access_key):
    self.access_key = access_key
    self._supported_status_codes = [200, 201, 204, 401, 404, 405, 422]

  def request(self, path, method='GET', params={}):
    url = urljoin(ENDPOINT, path)

    headers = {
      'Accept'        : 'application/json',
      'Authorization' : 'AccessKey ' + self.access_key,
      'User-Agent'    : 'MessageBird/ApiClient/%s Python/%s' % (CLIENT_VERSION, PYTHON_VERSION),
      'Content-Type'  : 'application/json'
    }

    if method == 'GET':
      response = requests.get(url, verify=True, headers=headers, params=params)
    else:
      response = requests.post(url, verify=True, headers=headers, data=json.dumps(params))

    if response.status_code in self._supported_status_codes:
      json_response = response.json()
    else:
      response.raise_for_status()

    if 'errors' in json_response:
      raise(ErrorException([Error().load(e) for e in json_response['errors']]))

    return json_response

  def balance(self):
    """Retrieve your balance."""
    return Balance().load(self.request('balance'))

  def hlr(self, id):
    """Retrieve the information of a specific HLR lookup."""
    return HLR().load(self.request('hlr/' + str(id)))

  def hlr_create(self, msisdn, reference):
    """Perform a new HLR lookup."""
    return HLR().load(self.request('hlr', 'POST', { 'msisdn' : msisdn, 'reference' : reference }))

  def message(self, id):
    """Retrieve the information of a specific message."""
    return Message().load(self.request('messages/' + str(id)))

  def message_create(self, originator, recipients, body, params={}):
    """Create a new message."""
    if type(recipients) == list:
      recipients = ','.join(recipients)

    params.update({ 'originator' : originator, 'body' : body, 'recipients' : recipients })
    return Message().load(self.request('messages', 'POST', params))

  def voice_message(self, id):
    "Retrieve the information of a specific voice message."
    return VoiceMessage().load(self.request('voicemessages/' + str(id)))

  def voice_message_create(self, recipients, body, params={}):
    """Create a new voice message."""
    if type(recipients) == list:
      recipients = ','.join(recipients)

    params.update({ 'recipients' : recipients, 'body' : body })
    return VoiceMessage().load(self.request('voicemessages', 'POST', params))

  def lookup(self, phonenumber, params={}):
    """Do a new lookup."""
    return Lookup().load(self.request('lookup/' + str(phonenumber), 'GET', params))

  def lookup_hlr(self, phonenumber, params={}):
    """Retrieve the information of a specific HLR lookup."""
    return HLR().load(self.request('lookup/' + str(phonenumber) + '/hlr', 'GET', params))

  def lookup_hlr_create(self, phonenumber, params={}):
    """Perform a new HLR lookup."""
    return HLR().load(self.request('lookup/' + str(phonenumber) + '/hlr', 'POST', params))

  def verify(self, id):
    """Retrieve the information of a specific verification."""
    return Verify().load(self.request('verify/' + str(id)))

  def verify_create(self, recipient, params={}):
    """Create a new verification."""
    params.update({ 'recipient' : recipient })
    return Verify().load(self.request('verify', 'POST', params))

  def verify_verify(self, id, token):
    """Verify the token of a specific verification."""
    return Verify().load(self.request('verify/' + str(id), params={'token': token}))

@@ -1047,28 +1047,65 @@
 ET', params=
+None):%0A    if params is None: params = 
 %7B%7D
-):
 %0A    url = u
@@ -2507,34 +2507,36 @@
 s, body, params=
-%7B%7D
+None
 ):%0A    %22%22%22Create
@@ -2546,32 +2546,67 @@
 new message.%22%22%22%0A
+    if params is None: params = %7B%7D%0A
     if type(reci
@@ -3038,34 +3038,36 @@
 s, body, params=
-%7B%7D
+None
 ):%0A    %22%22%22Create
@@ -3087,24 +3087,59 @@
 message.%22%22%22%0A
+    if params is None: params = %7B%7D%0A
     if type(
@@ -3374,34 +3374,36 @@
 enumber, params=
-%7B%7D
+None
 ):%0A    %22%22%22Do a n
@@ -3408,32 +3408,67 @@
  new lookup.%22%22%22%0A
+    if params is None: params = %7B%7D%0A
     return Looku
@@ -3571,34 +3571,36 @@
 enumber, params=
-%7B%7D
+None
 ):%0A    %22%22%22Retrie
@@ -3639,32 +3639,67 @@
  HLR lookup.%22%22%22%0A
+    if params is None: params = %7B%7D%0A
     return HLR()
@@ -3815,34 +3815,36 @@
 enumber, params=
-%7B%7D
+None
 ):%0A    %22%22%22Perfor
@@ -3858,32 +3858,67 @@
  HLR lookup.%22%22%22%0A
+    if params is None: params = %7B%7D%0A
     return HLR()
@@ -4185,18 +4185,20 @@
  params=
-%7B%7D
+None
 ):%0A    %22
@@ -4225,24 +4225,59 @@
 ication.%22%22%22%0A
+    if params is None: params = %7B%7D%0A
     params.u

fix mutable default value with sentinel