Пример #1
0
 def __parseresponse__(self, response):
     """Parse the response
     """
     statusCodeStr = str(response.status_code)
     if not statusCodeStr.startswith('2'):
         # Not 2xx, parsed as error response
         try:
             jObj = json.loads(response.content)
         except Exception as error:
             raise EtcdResponseError(response.content, error, traceback.format_exc())
         # Load error info
         code = jObj.get('errorCode')
         if not code:
             raise EtcdUnexpectedError('Require error code in etcd error response', response.content)
         if not code in KEY_ERRORS:
             raise EtcdUnexpectedError('Unknown etcd error code [%s]' % code, response.content)
         # Create the error object
         cause = jObj.get('cause')
         index = jObj.get('index')
         message = jObj.get('message')
         raise KEY_ERRORS[code](cause, code, index, message)
     else:
         # 2xx, parsed as KeyResponse
         # The headers
         clusterID = response.headers.get('X-Etcd-Cluster-Id')
         index = response.headers.get('X-Etcd-Index')
         raftIndex = response.headers.get('X-Raft-Index')
         raftTerm = response.headers.get('X-Raft-Term')
         # The content
         jObj = json.loads(response.content)
         action = jObj.get('action')
         node = jObj.get('node')
         prevNode = jObj.get('prevNode')
         # Create the response
         return KeyResponse(clusterID, index, raftIndex, raftTerm, action, node, prevNode)
Пример #2
0
    def members(self, timeout = None):
        """Get the memebers of the etcd cluster
        Parameters:
            timeout                         The timeout in ms
        Returns:
            A list of Member object
        """
        with self.client.pool.use() as node:
            response = self.client.channel.get(self.client.channel.compose(node, '/v2/members'), timeout = timeout or self.client.timeout)
        if response.status_code != requests.codes.ok:
            raise EtcdResponseError(response.status_code)
        try:

            jObj = json.loads(response.content)['members']
            return [ Member.load(x) for x in jObj ]
        except Exception as error:
            # Failed to load response
            raise EtcdResponseError(response.content, error, traceback.format_exc())