<commit_before>#!/usr/bin/env python

import md5
import os
import os.path
import sys
import time
import urllib

# define exceptions
class Error403(Exception): pass
class GoGridException(Exception): pass

class GoGridClient:
  """gogrid api client"""
  default_params = {'format':'csv', 'v':'1.0'}
  server = 'https://api.gogrid.com/api'
  api_key = None
  secret = None
  
  def __init__(self, key='', secret='', server=None):
    if key != "":
        self.api_key = key
    if secret != "":
        self.secret = secret
    if server is not None:
        self.server = server

    # if the options weren't given via constructor, try
    # to read them from file
    if self.api_key is None or self.secret is None:
        lines = open(os.path.expanduser("~/.ggrc"), 'r').readlines()
        self.api_key, self.secret = lines[0].split(":")

    gg_server = os.getenv('GG_SERVER')
    if gg_server is not None:
        self.server = gg_server

    self.default_params['api_key'] = self.api_key.strip()
    self.default_params['secret'] = self.secret.strip()
  
  def getRequestURL(self, method, params={}):
    """ constructs a call url from a given method with params """
    requestURL = self.server + '/' + method + '?'
    call_params = self.default_params.copy()
    call_params.update(params)
    call_params['sig'] = self.getSignature(call_params['api_key'],call_params['secret'])
    del call_params['secret']
    requestURL += urllib.urlencode(call_params)
    return requestURL
  
  def getSignature(self,key,secret):
    """ create sig from md5 of key + secret + time """
    m = md5.new(key + secret + str(int(time.time())))

    return m.hexdigest()
        
  def sendAPIRequest(self,method,params={}):
    """ send a request and return response """
    url = self.getRequestURL(method,params)
    f = urllib.urlopen(url)
    result = f.read()

    if "403 Not Authorized" in result:
        raise Error403, "Authorization error, check credentials and your time settings."

    if "FAILURE" in result:
        raise GoGridException(result.splitlines()[2].replace(",", ": "))

    return result

if __name__ == "__main__":
    client = GoGridClient()

    # ma   ke some calls
    print client.sendAPIRequest("grid/server/list")
<commit_msg>Better expection handling when cannot connect to server.<commit_after>#!/usr/bin/env python

import md5
import os
import os.path
import sys
import time
import urllib

# define exceptions
class Error403(Exception): pass
class GoGridException(Exception): pass

class GoGridClient:
  """gogrid api client"""
  default_params = {'format':'csv', 'v':'1.0'}
  server = 'https://api.gogrid.com/api'
  api_key = None
  secret = None
  
  def __init__(self, key='', secret='', server=None):
    if key != "":
        self.api_key = key
    if secret != "":
        self.secret = secret
    if server is not None:
        self.server = server

    # if the options weren't given via constructor, try
    # to read them from file
    if self.api_key is None or self.secret is None:
        lines = open(os.path.expanduser("~/.ggrc"), 'r').readlines()
        self.api_key, self.secret = lines[0].split(":")

    gg_server = os.getenv('GG_SERVER')
    if gg_server is not None:
        self.server = gg_server

    self.default_params['api_key'] = self.api_key.strip()
    self.default_params['secret'] = self.secret.strip()
  
  def getRequestURL(self, method, params={}):
    """ constructs a call url from a given method with params """
    requestURL = self.server + '/' + method + '?'
    call_params = self.default_params.copy()
    call_params.update(params)
    call_params['sig'] = self.getSignature(call_params['api_key'],call_params['secret'])
    del call_params['secret']
    requestURL += urllib.urlencode(call_params)
    return requestURL
  
  def getSignature(self, key, secret):
    """ create sig from md5 of key + secret + time """
    m = md5.new(key + secret + str(int(time.time())))

    return m.hexdigest()
        
  def sendAPIRequest(self, method, params={}):
    """ send a request and return response """
    url = self.getRequestURL(method, params)
    try:
        f = urllib.urlopen(url)
    except IOError, err:
        print "%s: %s" % (self.server, err.strerror[1])
        sys.exit(1)

    result = f.read()

    if "403 Not Authorized" in result:
        raise Error403, "Authorization error, check credentials and your time settings."

    if "FAILURE" in result:
        raise GoGridException(result.splitlines()[2].replace(",", ": "))

    return result

if __name__ == "__main__":
    client = GoGridClient()

    # ma   ke some calls
    print client.sendAPIRequest("grid/server/list")

{'fraction_non_alphanumeric': 0.13444658531363735, 'fraction_numerical': 0.007278955255833868, 'mean_word_length': 3.2204155374887082, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '56914', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1462, 'n_words': 401}