from helium.bfs10135.errors import bfs10174
from socket import timeout
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.error import URLError
import logging
from helium.bfs10077.bfs10173 import bfs10177
bfs10107 = logging.getLogger(__name__)
class bfs10204(object):
  def bfs10206(self, bfs10203, bfs10205):
    raise NotImplementedError()


class bfs10207(bfs10204):
  def __init__(self, config):
    self.config = config

  def bfs10206(self, bfs10210, bfs10212):
    key = bfs10210.code
    if bfs10210.bfs10211:
      key += bfs10212.bfs10213()

    bfs10215 = {'email': bfs10210.bfs10214, 'key': key}
    return self.get(self.config.bfs10216(bfs10215))

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


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

  def bfs10216(self, bfs10221):
    bfs10222 = self.bfs10217.copy()
    bfs10222.update(bfs10221)
    return (self.url + urlencode(bfs10222))


class bfs10223(bfs10207):
  def get(self, url):
    try:
      bfs10225 = urlopen(url, timeout=10)
    except URLError:
      bfs10107.debug('Got URLError while trying to validate license online.', exc_info=True)
      raise bfs10174()
    except timeout:
      bfs10107.warning('Connection to licensing server timed out.', exc_info=True)
      raise bfs10174()

    return bfs10177(bfs10225.read())


class bfs10224(bfs10207):
  def __init__(self, config, bfs10226):
    super(bfs10224, self).__init__(config)
    self.bfs10226 = bfs10226

  def get(self, url):
    self.bfs10226.get(url)
    return bfs10177(self.bfs10226.page_source)


