Exemplo n.º 1
0
 def do_upgrade(cls, odid, oskey, ndid, nsid):
     data = [
         'oldid', odid, 'oldskey', oskey, 'newdid', ndid, 'newsensorid',
         nsid
     ]
     ret = cls._post_data(cls.doupgrade_path, data)
     if ret.status == "ERROR":
         raise RinorError(ret.code, ret.description)
Exemplo n.º 2
0
 def post_list(cls, data):
     data = cls._post_data(cls.create_path, data)
     if data.status == "ERROR":
         raise RinorError(data.code, data.description)
     if cls.index:
         result = data[cls.index]
     else:
         result = data
     return result[0]
Exemplo n.º 3
0
 def get_list(cls):
     data = cls._get_data(cls.list_path)
     if data.status == "ERROR":
         raise RinorError(data.code, data.description)
     if cls.index:
         result = data[cls.index]
     else:
         result = data
     return result
Exemplo n.º 4
0
 def delete_details(cls, id):
     data = cls._delete_data(cls.delete_path, [id])
     if data.status == "ERROR":
         raise RinorError(data.code, data.description)
     if cls.index:
         result = data[cls.index]
     else:
         result = data
     if len(result) > 0:
         return result[0]
     else:
         return None
Exemplo n.º 5
0
def do_rinor_call(method, url, params):
    """ do_rinor_call
        
        param method: methode to use, get, post, put, delete
        param url: the url to call
        param params: a python dict with the http parameters
             => can also be a list (old system) but then the list will be translated to a dict

        returns a python dic with 2 keys
            status = http status code
            data = a python dict with the decoded json
    """
    if type(params) == list:
        # translate the list to a dict
        params = dict(itertools.izip_longest(*[iter(params)] * 2,
                                             fillvalue=""))
    if type(params) is not dict:
        raise RinorError(
            reason=
            "Params should be either a list (old system) or a dict (new system), params is a  {0}"
            .format(type(params)))
    try:
        response = requests.reqiuest(method=method, url=url, params=params)
    except ConnectionError as e:
        raise RinorError(reason="Connection failed for '{0}'".format(url))
    except HTTPError as e:
        raise RinorError(reason="HTTP Error for '{0}'".format(url))
    except Timeout as e:
        raise RinorError(reason="Timeout for '{0}'".format(url))
    except TooManyRedirects as e:
        raise RinorError(reason="Too many redirects for '{0}'".format(url))
    ret = {}
    ret['status'] = response.status_code
    # try to fetch the data
    try:
        ret['data'] = response.json()
    except:
        ret['data'] = response.text
    # return the data (json or txt)
    return ret
Exemplo n.º 6
0
 def add_xplstat_params(self, id, parameters):
     try:
         # Find the db id, based on the json id
         xplstat = XPLStat.objects.get(device_id=self.id, json_id=id)
     except XPLStat.DoesNotExist:
         pass
     else:
         params = ['id', xplstat.id]
         if parameters:
             params.extend(
                 list(reduce(lambda x, y: x + y, parameters.items())))
         _data = Device._put_data(Device.addxplstat_path, params)
         if _data.status == "ERROR":
             raise RinorError(_data.code, _data.description)
Exemplo n.º 7
0
    retries = 0
    attempts = 0
    while True:
        try:
            attempts += 1
            respObj = urllib2.urlopen(uri)
            break
        except urllib2.HTTPError, e:
            raise RinorNotAvailable(code=e.code, resp=e.read())
        except urllib2.URLError, e:
            if attempts <= retries:
                continue
            else:
                raise RinorNotAvailable(reason=e.reason)
        except BadStatusLine:
            raise RinorError(reason="No response for '%s'" % uri)
    resp = respObj.read()
    resp = resp.replace("u'", "'")  # Fix for unicode prefix.
    resp_obj = simplejson.loads(resp)
    return _objectify_json(resp_obj)


def _objectify_json(i):
    if isinstance(i, dict):
        transformed_dict = JSONDict()
        for key, val in i.iteritems():
            transformed_dict[key] = _objectify_json(val)
        return transformed_dict
    elif isinstance(i, list):
        for idx in range(len(i)):
            i[idx] = _objectify_json(i[idx])
Exemplo n.º 8
0
 def list_upgrade(cls):
     data = cls._get_data(cls.listupgrade_path)
     if data.status == "ERROR":
         raise RinorError(data.code, data.description)
     return data[cls.index]
Exemplo n.º 9
0
 def add_global_params(self, parameters):
     params = ['id', self.id]
     params.extend(list(reduce(lambda x, y: x + y, parameters.items())))
     _data = Device._put_data(Device.addglobal_path, params)
     if _data.status == "ERROR":
         raise RinorError(_data.code, _data.description)