import urllib2
import json

class Api():
  group_name  = "Digital Ocean"
  client_key  = None
  api_key     = None
  servers     = {}

  def __init__(self, **kwargs):
    for key in kwargs:
      setattr(self, key, kwargs[key])

  def grab_servers(self):
    DROPLETS_URL  = 'https%s/droplets/?client_id=%s&api_key=%s' % \
                      ('://api.digitalocean.com',
                        self.client_key,
                        self.api_key)

    droplets      = urllib2.urlopen(DROPLETS_URL)
    try:
      data        = json.loads(droplets.read())
    except:
      raise Exception("Fatal error: No droplets found")

    for droplet in data['droplets']:
      if droplet['status'] == 'active':
        name = droplet['name']
        if name not in self.servers:
          self.servers[name] = []
        self.servers[name].append(droplet['ip_address'])

  def get_servers(self, name):
    return self.servers[name] if name in self.servers else None


import urllib2
import json

class Api():
  group_name  = "Digital Ocean"
  client_key  = None
  api_key     = None
  servers     = {}

  def __init__(self, **kwargs):
    for key in kwargs:
      setattr(self, key, kwargs[key])

  def grab_servers(self):
    DROPLETS_URL  = 'https%s/droplets/?client_id=%s&api_key=%s' % \
                      ('://api.digitalocean.com',
                        self.client_key,
                        self.api_key)

    try:
      droplets      = urllib2.urlopen(DROPLETS_URL)
    except urllib2.URLError:
      raise Exception("Fatal error: Unable to connect to API")

    try:
      data        = json.loads(droplets.read())
    except:
      raise Exception("Fatal error: No droplets found")

    for droplet in data['droplets']:
      if droplet['status'] == 'active':
        name = droplet['name']
        if name not in self.servers:
          self.servers[name] = []
        self.servers[name].append(droplet['ip_address'])

  def get_servers(self, name):
    return self.servers[name] if name in self.servers else None


Return a helpful exception if API is uncontactable
Return a helpful exception if API is uncontactable
  import urllib2
  import json
  
  class Api():
    group_name  = "Digital Ocean"
    client_key  = None
    api_key     = None
    servers     = {}
  
    def __init__(self, **kwargs):
      for key in kwargs:
        setattr(self, key, kwargs[key])
  
    def grab_servers(self):
      DROPLETS_URL  = 'https%s/droplets/?client_id=%s&api_key=%s' % \
                        ('://api.digitalocean.com',
                          self.client_key,
                          self.api_key)
  
+     try:
-     droplets      = urllib2.urlopen(DROPLETS_URL)
+       droplets      = urllib2.urlopen(DROPLETS_URL)
+     except urllib2.URLError:
+       raise Exception("Fatal error: Unable to connect to API")
+ 
      try:
        data        = json.loads(droplets.read())
      except:
        raise Exception("Fatal error: No droplets found")
  
      for droplet in data['droplets']:
        if droplet['status'] == 'active':
          name = droplet['name']
          if name not in self.servers:
            self.servers[name] = []
          self.servers[name].append(droplet['ip_address'])
  
    def get_servers(self, name):
      return self.servers[name] if name in self.servers else None
  
  
Return a helpful exception if API is uncontactable
## Code Before:
import urllib2
import json

class Api():
  group_name  = "Digital Ocean"
  client_key  = None
  api_key     = None
  servers     = {}

  def __init__(self, **kwargs):
    for key in kwargs:
      setattr(self, key, kwargs[key])

  def grab_servers(self):
    DROPLETS_URL  = 'https%s/droplets/?client_id=%s&api_key=%s' % \
                      ('://api.digitalocean.com',
                        self.client_key,
                        self.api_key)

    droplets      = urllib2.urlopen(DROPLETS_URL)
    try:
      data        = json.loads(droplets.read())
    except:
      raise Exception("Fatal error: No droplets found")

    for droplet in data['droplets']:
      if droplet['status'] == 'active':
        name = droplet['name']
        if name not in self.servers:
          self.servers[name] = []
        self.servers[name].append(droplet['ip_address'])

  def get_servers(self, name):
    return self.servers[name] if name in self.servers else None


## Instruction:
Return a helpful exception if API is uncontactable
## Code After:
import urllib2
import json

class Api():
  group_name  = "Digital Ocean"
  client_key  = None
  api_key     = None
  servers     = {}

  def __init__(self, **kwargs):
    for key in kwargs:
      setattr(self, key, kwargs[key])

  def grab_servers(self):
    DROPLETS_URL  = 'https%s/droplets/?client_id=%s&api_key=%s' % \
                      ('://api.digitalocean.com',
                        self.client_key,
                        self.api_key)

    try:
      droplets      = urllib2.urlopen(DROPLETS_URL)
    except urllib2.URLError:
      raise Exception("Fatal error: Unable to connect to API")

    try:
      data        = json.loads(droplets.read())
    except:
      raise Exception("Fatal error: No droplets found")

    for droplet in data['droplets']:
      if droplet['status'] == 'active':
        name = droplet['name']
        if name not in self.servers:
          self.servers[name] = []
        self.servers[name].append(droplet['ip_address'])

  def get_servers(self, name):
    return self.servers[name] if name in self.servers else None

