from stacktester import exceptions

import httplib2
import os
import time


class Client(object):

    USER_AGENT = 'python-nova_test_client'

    def __init__(self, host='localhost', port=80, base_url=''):
        #TODO: join these more robustly
        self.base_url = "http://%s:%s/%s" % (host, port, base_url)

    def poll_request(self, method, url, check_response, **kwargs):

        timeout = kwargs.pop('timeout', 180)
        # Start timestamp
        start_ts = int(time.time())

        resp, body = self.request(method, url, **kwargs)
        while (not check_response(resp, body)):
            if (int(time.time()) - start_ts >= (timeout * 1000)):
                raise exceptions.TimeoutException
            time.sleep(2)
            resp, body = self.request(method, url, **kwargs)

    def request(self, method, url, **kwargs):
        self.http_obj = httplib2.Http()

        params = {}
        params['headers'] = {'User-Agent': self.USER_AGENT}
        params['headers'].update(kwargs.get('headers', {}))
        if 'Content-Type' not in kwargs.get('headers',{}):
            params['headers']['Content-Type'] = 'application/json'

        if 'body' in kwargs:
            params['body'] = kwargs.get('body')

        req_url = "%s/%s" % (self.base_url, url)
        resp, body = self.http_obj.request(req_url, method, **params)
        return resp, body

from stacktester import exceptions

import httplib2
import os
import time


class Client(object):

    USER_AGENT = 'python-nova_test_client'

    def __init__(self, host='localhost', port=80, base_url=''):
        #TODO: join these more robustly
        self.base_url = "http://%s:%s/%s" % (host, port, base_url)

    def poll_request(self, method, url, check_response, **kwargs):

        timeout = kwargs.pop('timeout', 180)
        interval = kwargs.pop('interval', 2)
        # Start timestamp
        start_ts = int(time.time())

        while True:
            resp, body = self.request(method, url, **kwargs)
            if (check_response(resp, body)):
                break
            if (int(time.time()) - start_ts >= (timeout * 1000)):
                raise exceptions.TimeoutException
            time.sleep(interval)

    def request(self, method, url, **kwargs):
        self.http_obj = httplib2.Http()

        params = {}
        params['headers'] = {'User-Agent': self.USER_AGENT}
        params['headers'].update(kwargs.get('headers', {}))
        if 'Content-Type' not in params.get('headers',{}):
            params['headers']['Content-Type'] = 'application/json'

        if 'body' in kwargs:
            params['body'] = kwargs.get('body')

        req_url = "%s/%s" % (self.base_url, url)
        resp, body = self.http_obj.request(req_url, method, **params)
        return resp, body

make interval adjustable in poll_request, and also remove check_response call duplication