from helium.bfs10110.bfs10112 import bfs10145
from base64 import b64encode
from logging import getLogger
from socket import timeout
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.error import URLError
from helium.bfs10063.bfs10147 import bfs10152
class bfs10154(object):
  def bfs10156(self, bfs10155, bfs10157):
    raise NotImplementedError()


class bfs10160(bfs10154):
  def __init__(self, config):
    self.config = config

  def bfs10156(self, bfs10161, bfs10163):
    key = bfs10161.code
    if bfs10161.bfs10162:
      key += bfs10163.bfs10164()

    bfs10165 = {'email': bfs10161.bfs10166, 'key': key, 'p1': b64encode(str(bfs10163.bfs10170())), 'p2': b64encode(str(bfs10163.bfs10167())), 'p3': b64encode(str(bfs10163.bfs10171())), 'p4': b64encode(str(bfs10163.bfs10172())), 'p5': b64encode(str(bfs10163.bfs10174())), 'p6': b64encode(str(bfs10163.bfs10173())), 'p7': b64encode(str(bfs10163.bfs10175()))}
    return self.get(self.config.bfs10176(bfs10165))

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


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

  def bfs10176(self, bfs10201):
    bfs10202 = self.bfs10177.copy()
    bfs10202.update(bfs10201)
    return (self.url + urlencode(bfs10202))


class bfs10204(bfs10160):
  def get(self, url):
    try:
      bfs10203 = urlopen(url, timeout=10)
    except URLError:
      getLogger(__name__).debug('Got URLError while trying to validate license online.', exc_info=True)
      raise bfs10145()
    except timeout:
      getLogger(__name__).warning('Connection to licensing server timed out.', exc_info=True)
      raise bfs10145()

    return bfs10152(bfs10203.read())


class bfs10205(bfs10160):
  def __init__(self, config, bfs10206):
    super(bfs10205, self).__init__(config)
    self.bfs10206 = bfs10206

  def get(self, url):
    self.bfs10206.get(url)
    return bfs10152(self.bfs10206.page_source)


