import string
import random
from hashlib import md5

import requests


class TempMail(object):
    """
    Wrapper for online service which provides temporary email address.
    """

    def __init__(self, login=None, domain=None, api_domain='api.temp-mail.ru'):
        self.login = login
        self.domain = domain
        self.api_domain = api_domain

    @property
    def available_domains(self):
        """
        Return list of available domains for use in email address.
        """
        if not hasattr(self, '_available_domains'):
            url = 'http://{0}/request/domains/format/json/'.format(
                self.api_domain)
            req = requests.get(url)
            domains = req.json()
            setattr(self, '_available_domains', domains)
        return self._available_domains

    def generate_login(self, min_length=6, max_length=10, digits=True):
        """
        Generate string for email address login with defined length and
        alphabet.
        """
        chars = string.ascii_lowercase
        if digits:
            chars += string.digits
        length = random.randint(min_length, max_length)
        return ''.join(random.choice(chars) for x in range(length))

    def get_email_address(self):
        """
        Use login and domain from class initialization or generate new
        and return full email address.
        """
        if self.login is None:
            self.login = self.generate_login()

        available_domains = self.available_domains
        if self.domain is None:
            self.domain = random.choice(available_domains)
        elif self.domain not in available_domains:
            raise ValueError('Domain not found in available domains!')
        return u'{0}{1}'.format(self.login, self.domain)

    def get_hash(self, email):
        """
        Return md5 hash for given email address.
        """
        return md5(email).hexdigest()

    def get_mailbox(self, email=None, email_hash=None):
        """
        Return list of emails in given email address
        or dict with `error` key if mail box is empty.
        """
        if email is None:
            email = self.get_email_address()
        if email_hash is None:
            email_hash = self.get_hash(email)

        url = 'http://{0}/request/mail/id/{1}/format/json/'.format(
            self.api_domain, email_hash)
        req = requests.get(url)
        return req.json()

@@ -100,16 +100,20 @@
 %22%22%22%0A    
+API 
 Wrapper 
@@ -120,15 +120,8 @@
 for 
-online 
 serv
@@ -159,24 +159,267 @@
 ail address.
+%0A%0A    :param login: (optional) login for email address.%0A    :param domain: (optional) domain (from current available)%0A    for email address.%0A    :param api_domain: (optional) domain for temp-mail api.%0A    Default value is %60%60api.temp-mail.ru%60%60.
 %0A    %22%22%22%0A%0A  
@@ -1220,16 +1220,295 @@
 lphabet.
+%0A%0A        :param min_length: (optional) min login length.%0A        Default value is %60%606%60%60.%0A        :param max_length: (optional) max login length.%0A        Default value is %60%6010%60%60.%0A        :param digits: (optional) use digits in login generation.%0A        Default value is %60%60True%60%60.
 %0A       
@@ -1787,108 +1787,115 @@
     
-Use login and domain from class initialization or generate new%0A        and return full email address
+Return full email address from login and domain from params in class%0A        initialization or generate new
 .%0A  
@@ -2388,32 +2388,92 @@
 n email address.
+%0A%0A        :param email: email address for generate md5 hash.
 %0A        %22%22%22%0A   
@@ -2629,16 +2629,16 @@
 address%0A
-
         
@@ -2683,16 +2683,132 @@
 s empty.
+%0A%0A        :param email: (optional) email address.%0A        :param email_hash: (optional) md5 hash from email address.
 %0A       

improve docstrings for class methods, close #2