def get_page_data(self):
        """
        Calls API to get page data as dictionary, which includes page token.
        "manage_pages" permissions must first be granted.
        """
        url = FB_HOST + "/%s/accounts" % self.id
        params = {
            'access_token': self.access_token,
            'client_id': FB_APP_ID,
            'client_secret': FB_APP_SECRET
        }

        response = requests.get(url, params=params)
        logging.debug("get_page_data response: %s" % response.text)

        try:
            response_dict = response.json()
        except ValueError:
            raise FacebookAPIError(
                "Unexpected response. No JSON object could be decoded.")

        if 'error' in response_dict:
            raise FacebookAPIError("Error in response: %r" %
                                   response_dict['error'])

        page_data = response_dict.get('data')
        if not page_data:
            logging.warn("No data object in response.")

        return page_data
    def _reliable_op(*args, **kwargs):
        argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
        fname = func.func_name
        msg = 'MQ - -> %s(%s)' % (fname, ','.join('%s=%s' % entry \
                                                for entry in zip(argnames[1:],\
                                                args[1:]) + kwargs.items()))
        trans_times = 0
        while True:
            trans_times += 1
            if trans_times > MAX_RETRY:
                logging.error('rabbitmq connection lost!')
                return None
            try:
                ###############################################################
                logging.warn('function:%s' % (func.func_name))
                ###############################################################
                return func(*args, **kwargs)

            except pika.exceptions.AMQPConnectionError, e:
                logging.error("AMQPConnectionError: %s" % e)
                logging.error(traceback.format_exc())
                if not _reconnect(args, msg, e):
                    raise
            except pika.exceptions.AMQPChannelError, e:
                try:
                    if len(args) == 0 or not isinstance(
                            args[0], RabbitMQBlockingClient):
                        raise MQError(
                            "unsupported decorator, it should be applied to RabbitMQBlockingClient methods"
                        )
                    close_func = getattr(args[0], "close")
                    close_func()
                except Exception, e:
                    logging.error(e)
                    logging.error(traceback.format_exc())
    def _get_by_queue(self, queue_name):
        method, properties, body = self._channel.basic_get(queue=queue_name, \
                                                        no_ack=self._auto_ack)
        if method is None or method.NAME == "Basic.GetEmpty":
            return None

        if properties.content_type == "text/json":
            message = loads_jsonx(body)
        else:
            message = {"__body": body}

        message["__delivery_tag"] = method.delivery_tag
        if properties.timestamp is not None:
            message["__timestamp"] = properties.timestamp

        if self._timestamp_expires:
            message_id = generate_message_id(message, self._message_ids)
            if self._aux_store is None:
                expired = False
                logging.warn("aux_store is none in basic_get")
            else:
                expired = \
                self._aux_store.check_message_expires(self._message_type, \
                                                      message_id, \
                                                      properties.timestamp)
            if expired:
                if not self._auto_ack:
                    self.ack(method.delivery_tag)
                return self._get_by_queue(queue_name)

        return message
    def get_access_code(self, redirect_uri=""):
        """
        Calls API using long-term access token to get access code.
        Takes and passes one parameter to API:
            redirect_uri    Defaults to empty string.
        """
        url = FB_HOST + "/oauth/client_code"
        params = {'access_token': self.long_term_access_token,
                  'client_id': FB_APP_ID,
                  'client_secret': FB_APP_SECRET,
                  'redirect_uri': redirect_uri}
        response = requests.get(url, params=params)
        logging.debug("get_access_code response: %s" % response.text)

        try:
            response_dict = response.json()
        except ValueError:
            raise FacebookAPIError("Unexpected response. No JSON object could be decoded.")

        if 'error' in response_dict:
            raise FacebookAPIError("Error in response: %r" % response_dict['error'])

        access_code = self._get_json(response).get('code')

        if not access_code:
            logging.warn("No access_code in response.")
        return access_code
    def get_page_data(self):
        """
        Calls API to get page data as dictionary, which includes page token.
        "manage_pages" permissions must first be granted.
        """
        url = FB_HOST + "/%s/accounts" % self.id
        params = {'access_token': self.access_token,
                  'client_id': FB_APP_ID,
                  'client_secret': FB_APP_SECRET}

        response = requests.get(url, params=params)
        logging.debug("get_page_data response: %s" % response.text)

        try:
            response_dict = response.json()
        except ValueError:
            raise FacebookAPIError("Unexpected response. No JSON object could be decoded.")

        if 'error' in response_dict:
            raise FacebookAPIError("Error in response: %r" % response_dict['error'])

        page_data = response_dict.get('data')
        if not page_data:
            logging.warn("No data object in response.")

        return page_data
    def _get_by_queue(self, queue_name):
        method, properties, body = self._channel.basic_get(queue=queue_name, \
                                                        no_ack=self._auto_ack)
        if method is None or method.NAME == "Basic.GetEmpty":
            return None

        if properties.content_type == "text/json":
            message = loads_jsonx(body)
        else:
            message = {"__body": body}

        message["__delivery_tag"] = method.delivery_tag
        if properties.timestamp is not None:
            message["__timestamp"] = properties.timestamp

        if self._timestamp_expires:
            message_id = generate_message_id(message, self._message_ids)
            if self._aux_store is None:
                expired = False
                logging.warn("aux_store is none in basic_get")
            else:
                expired = \
                self._aux_store.check_message_expires(self._message_type, \
                                                      message_id, \
                                                      properties.timestamp)
            if expired:
                if not self._auto_ack:
                    self.ack(method.delivery_tag)
                return self._get_by_queue(queue_name)

        return message
    def get_access_code(self, redirect_uri=""):
        """
        Calls API using long-term access token to get access code.
        Takes and passes one parameter to API:
            redirect_uri    Defaults to empty string.
        """
        url = FB_HOST + "/oauth/client_code"
        params = {
            'access_token': self.long_term_access_token,
            'client_id': FB_APP_ID,
            'client_secret': FB_APP_SECRET,
            'redirect_uri': redirect_uri
        }
        response = requests.get(url, params=params)
        logging.debug("get_access_code response: %s" % response.text)

        try:
            response_dict = response.json()
        except ValueError:
            raise FacebookAPIError(
                "Unexpected response. No JSON object could be decoded.")

        if 'error' in response_dict:
            raise FacebookAPIError("Error in response: %r" %
                                   response_dict['error'])

        access_code = self._get_json(response).get('code')

        if not access_code:
            logging.warn("No access_code in response.")
        return access_code
    def _reliable_op(*args, **kwargs):
        argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
        fname = func.func_name
        msg = 'MQ - -> %s(%s)' % (fname, ','.join('%s=%s' % entry \
                                                for entry in zip(argnames[1:],\
                                                args[1:]) + kwargs.items()))
        trans_times = 0
        while True:
            trans_times += 1
            if trans_times > MAX_RETRY:
                logging.error('rabbitmq connection lost!')
                return None
            try:
###############################################################
                logging.warn('function:%s' %(func.func_name))
###############################################################
                return func(*args, **kwargs)

            except pika.exceptions.AMQPConnectionError, e:
                logging.error("AMQPConnectionError: %s" % e)
                logging.error(traceback.format_exc())
                if not _reconnect(args, msg, e):
                    raise
            except pika.exceptions.AMQPChannelError, e:
                try:
                    if len(args) == 0 or not isinstance(args[0], RabbitMQBlockingClient):
                        raise MQError("unsupported decorator, it should be applied to RabbitMQBlockingClient methods")
                    close_func = getattr(args[0], "close")
                    close_func()
                except Exception, e:
                    logging.error(e)
                    logging.error(traceback.format_exc())
    def publish(self, message, priority=0):
        if priority < 0:
            raise MQError("priority %d should be >= 0" % priority)

        if priority >= self._priority_level:
            logging.warn("priority has been changed due to exceeding", \
                         message=message, original_priority=priority, \
                         changed_priority=self._priority_level - 1)
            priority = self._priority_level - 1
            if priority < 0:
                priority = 0

        #determine properties
        properties = pika.BasicProperties()
        properties.content_type = self._content_type
        if self._persistent:
            properties.delivery_mode = 2
        if self._with_timestamp:
            properties.timestamp = datetime2timestamp(
                datetime.datetime.utcnow())

        #get group_id
        if self._group_mode:
            if self._content_type != "text/json":
                #Note: here we just support hashable object, e.g., str/unicode etc.
                group_hash = message
            else:
                if not ("__group_hash" in message.keys()):
                    raise MQError(
                        "__group_hash is required if message type is in group_mode"
                    )
                group_hash = message["__group_hash"]
            group_int_hash = hash(group_hash)
            group_id = group_int_hash % self._group_counts[priority]
        else:
            group_id = 0
        #the message is changed here
        if self._content_type == "text/json" and "__group_hash" in message.keys(
        ):
            message.pop("__group_hash")

        #determine body
        if self._content_type == "text/json":
            body = dumps_jsonx(message)
        else:
            body = message

        #determine routing_key
        routing_key = self.generate_routing_name(self._binding_prefix,
                                                 priority, group_id)

        try:
            self._channel.basic_publish(exchange=self._exchange,
                                        routing_key=routing_key,
                                        body=body,
                                        properties=properties)
        except AttributeError, e:
            logging.info('basic publish raise AttributeError: %s' % e)
            raise pika.exceptions.AMQPConnectionError
 def expire_message(self, message):
     if not self._timestamp_expires:
         raise MQError("expire_message should be valid when _timestamp_expires is true")
     if self._aux_store is None:
         logging.warn("aux_store is none in expire_message")
         return
     message_id = generate_message_id(message, self._message_ids)
     timestamp = datetime2timestamp(datetime.datetime.utcnow())
     self._aux_store.add_expired_message(self._message_type, message_id, timestamp)
    def get(self):

        #############################################
        logging.warn('get called!')
        #############################################
        for priority in range(self._priority_level):
            message = self._get_by_priority(priority)
            if message is not None:
                return message
        return None
 def get(self):
     
     #############################################
     logging.warn('get called!')
     #############################################
     for priority in range(self._priority_level):
         message = self._get_by_priority(priority)
         if message is not None:
             return message
     return None
 def expire_message(self, message):
     if not self._timestamp_expires:
         raise MQError(
             "expire_message should be valid when _timestamp_expires is true"
         )
     if self._aux_store is None:
         logging.warn("aux_store is none in expire_message")
         return
     message_id = generate_message_id(message, self._message_ids)
     timestamp = datetime2timestamp(datetime.datetime.utcnow())
     self._aux_store.add_expired_message(self._message_type, message_id,
                                         timestamp)
    def publish(self, message, priority=0):
        if priority < 0:
            raise MQError("priority %d should be >= 0" % priority)

        if priority >= self._priority_level:
            logging.warn("priority has been changed due to exceeding", \
                         message=message, original_priority=priority, \
                         changed_priority=self._priority_level - 1)
            priority = self._priority_level - 1
            if priority < 0:
                priority = 0

        #determine properties
        properties = pika.BasicProperties()
        properties.content_type = self._content_type
        if self._persistent:
            properties.delivery_mode = 2
        if self._with_timestamp:
            properties.timestamp = datetime2timestamp(datetime.datetime.utcnow())

        #get group_id
        if self._group_mode:
            if self._content_type != "text/json":
                #Note: here we just support hashable object, e.g., str/unicode etc.
                group_hash = message
            else:
                if not ("__group_hash" in message.keys()):
                    raise MQError("__group_hash is required if message type is in group_mode")
                group_hash = message["__group_hash"]
            group_int_hash = hash(group_hash)
            group_id = group_int_hash % self._group_counts[priority]
        else:
            group_id = 0
        #the message is changed here
        if self._content_type == "text/json" and "__group_hash" in message.keys():
            message.pop("__group_hash")

        #determine body
        if self._content_type == "text/json":
            body = dumps_jsonx(message)
        else:
            body = message

        #determine routing_key
        routing_key = self.generate_routing_name(self._binding_prefix, priority, group_id)

        try:
            self._channel.basic_publish(exchange=self._exchange, routing_key=routing_key, body=body, properties=properties)
        except AttributeError, e:
            logging.info('basic publish raise AttributeError: %s' % e)
            raise pika.exceptions.AMQPConnectionError
    def __init__(self, **kwargs):
        """
        instantiated with keyword arguments:
            id                        id of the test user. Logs warning if not passed.
            app_access_token          application access token; fetches from API if not passed.
            access_token              short term user access token.
            long_term_access_token    long term user access token.
            access_code               user access code.
        """
        self.id = kwargs.get('id')
        self.app_access_token = kwargs.get('app_access_token', FacebookUserAccess.get_app_access_token())
        self.access_token = kwargs.get('access_token')
        self.long_term_access_token = kwargs.get('long_term_access_token')
        self.access_code = kwargs.get('access_code')

        if not self.id:
            logging.warn("FacebookUserAccess instantiated without id.")
    def __init__(self, **kwargs):
        """
        instantiated with keyword arguments:
            id                        id of the test user. Logs warning if not passed.
            app_access_token          application access token; fetches from API if not passed.
            access_token              short term user access token.
            long_term_access_token    long term user access token.
            access_code               user access code.
        """
        self.id = kwargs.get('id')
        self.app_access_token = kwargs.get(
            'app_access_token', FacebookUserAccess.get_app_access_token())
        self.access_token = kwargs.get('access_token')
        self.long_term_access_token = kwargs.get('long_term_access_token')
        self.access_code = kwargs.get('access_code')

        if not self.id:
            logging.warn("FacebookUserAccess instantiated without id.")
Esempio n. 17
0
    def __init__(self, host, port):

        server.append(self)
        self.host = host
        self.port = port
        confirmflag = False
        for i in range(500):
            try:
                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                self.sock.bind((self.host, self.port + i))
                break
            except socket.error as err:
                confirmflag = True
                logging.warn("port {} already in use, increasing by 1.".format(
                    self.port + i))
                continue
        logging.debug("Connected to localhost:{}".format(self.port + i))
        if confirmflag:
            raw_input("Press Enter.")
    def rpc(self, message, priority=0):
        '''
        caller of rpc will wait until results are returned, timeout supported by default
        '''

        start_time = datetime.datetime.utcnow()
        correlation_id = str(uuid.uuid4())

        def on_response(channel, method, props, body):
            if correlation_id == props.correlation_id:
                self._rpc_cache[correlation_id] = body

        callback_queue = self._get_callback_queue(correlation_id, on_response)
        if callback_queue is None:
            logging.warn("can't get callback_queue",
                         url=message.get("url", ""))
            return None

        message["__reply_to"] = callback_queue
        message["__correlation_id"] = correlation_id

        self.publish(message, priority)
        while not correlation_id in self._rpc_cache.keys():
            curr_time = datetime.datetime.utcnow()
            if curr_time - start_time > datetime.timedelta(
                    seconds=self._timeout):
                logging.warn("timeout for rpc request",
                             url=message.get("url", ""))
                break
            connection = getattr(self._blocking_connection, '_connection')
            connection.process_data_events()

        self._channel.basic_cancel(consumer_tag=correlation_id)
        self._rpc_queue_pool.put(callback_queue)
        response = self._rpc_cache.pop(correlation_id, None)
        if response is not None and self._rpc_reply_content_type == "text/json":
            response = loads_jsonx(response)

        return response
    def rpc(self, message, priority=0):
        '''
        caller of rpc will wait until results are returned, timeout supported by default
        '''

        start_time = datetime.datetime.utcnow()
        correlation_id = str(uuid.uuid4())

        def on_response(channel, method, props, body):
            if correlation_id == props.correlation_id:
                self._rpc_cache[correlation_id] = body

        callback_queue = self._get_callback_queue(correlation_id, on_response)
        if callback_queue is None:
            logging.warn("can't get callback_queue", url=message.get("url", ""))
            return None

        message["__reply_to"] = callback_queue
        message["__correlation_id"] = correlation_id

        self.publish(message, priority)
        while not correlation_id in self._rpc_cache.keys():
            curr_time = datetime.datetime.utcnow()
            if curr_time - start_time > datetime.timedelta(seconds=self._timeout):
                logging.warn("timeout for rpc request", url=message.get("url", ""))
                break
            connection = getattr(self._blocking_connection, '_connection')
            connection.process_data_events()

        self._channel.basic_cancel(consumer_tag=correlation_id)
        self._rpc_queue_pool.put(callback_queue)
        response = self._rpc_cache.pop(correlation_id, None)
        if response is not None and self._rpc_reply_content_type == "text/json":
            response = loads_jsonx(response)

        return response