from helium.bfs10131.errors import bfs10171
from base64 import b64encode
from socket import timeout
from urllib import urlencode
from urllib2 import urlopen, URLError
import logging
from helium.bfs10076.bfs10172 import bfs10176
bfs10106 = logging.getLogger(__name__)
class bfs10201(object):
  def bfs10203(self, bfs10202, bfs10204):
    raise NotImplementedError()


class bfs10205(bfs10201):
  def __init__(self, config):
    self.config = config

  def bfs10203(self, bfs10206, bfs10207):
    key = bfs10206.code
    if bfs10206.bfs10210:
      key += bfs10207.bfs10212()

    bfs10211 = {'email': bfs10206.bfs10213, 'key': key, 'p1': b64encode(str(bfs10207.bfs10215())), 'p2': b64encode(str(bfs10207.bfs10214())), 'p3': b64encode(str(bfs10207.bfs10216())), 'p4': b64encode(str(bfs10207.bfs10220())), 'p5': b64encode(str(bfs10207.bfs10217())), 'p6': b64encode(str(bfs10207.bfs10221())), 'p7': b64encode(str(bfs10207.bfs10222()))}
    return self.get(self.config.bfs10223(bfs10211))

  def get(self, url):
    raise NotImplementedError()


class bfs10225(object):
  def __init__(self, url, bfs10224=None):
    self.url = url
    self.bfs10224 = (bfs10224 or {})

  def bfs10223(self, bfs10226):
    bfs10230 = self.bfs10224.copy()
    bfs10230.update(bfs10226)
    return (self.url + urlencode(bfs10230))


class bfs10227(bfs10205):
  def get(self, url):
    try:
      bfs10231 = urlopen(url, timeout=10)
    except URLError:
      bfs10106.debug('Got URLError while trying to validate license online.', exc_info=True)
      raise bfs10171()
    except timeout:
      bfs10106.warning('Connection to licensing server timed out.', exc_info=True)
      raise bfs10171()

    return bfs10176(bfs10231.read())


class bfs10232(bfs10205):
  def __init__(self, config, bfs10234):
    super(bfs10232, self).__init__(config)
    self.bfs10234 = bfs10234

  def get(self, url):
    self.bfs10234.get(url)
    return bfs10176(self.bfs10234.page_source)


