Ejemplo n.º 1
0
class MessageManager(object):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, http_client, api_key, verbose=False):
        """
        Constructor
        """
        self.__rest_client = RESTClient(http_client, api_key)
        self.__verbose = verbose

    # ----------------------------------------------------------------------------------------------------------------

    def find_for_topic(self, topic, start_date, end_date):
        request_path = '/v1/messages/topic/' + urllib.parse.quote(topic,
                                                                  safe='')
        params = {
            'start-date': start_date.as_iso8601(),
            'end-date': end_date.as_iso8601()
        }

        # request...
        self.__rest_client.connect()

        try:
            jdict = self.__rest_client.get(request_path, params)

            # messages...
            messages = [
                Message.construct_from_jdict(msg_jdict) for msg_jdict in jdict
            ] if jdict else []
        finally:
            self.__rest_client.close()

        return messages

    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "MessageManager:{rest_client:%s, verbose:%s}" % (
            self.__rest_client, self.__verbose)
Ejemplo n.º 2
0
class MessageManager(object):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, http_client, api_key, verbose=False):
        """
        Constructor
        """
        self.__rest_client = RESTClient(http_client, api_key)
        self.__verbose = verbose

    # ----------------------------------------------------------------------------------------------------------------

    def find_for_topic(self, topic, start_date, end_date):
        request_path = '/staging/topicMessages'
        params = {
            'topic': topic,
            'startTime': start_date.utc().as_iso8601(),
            'endTime': end_date.utc().as_iso8601()
        }

        # request...
        self.__rest_client.connect()

        try:
            jdict = self.__rest_client.get(request_path, params)

            # messages...
            collection = MessageCollection.construct_from_jdict(jdict)

            messages = collection.items
        finally:
            self.__rest_client.close()

        return messages

    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "MessageManager:{rest_client:%s, verbose:%s}" % (
            self.__rest_client, self.__verbose)
Ejemplo n.º 3
0
class MessageManager(object):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, http_client, api_key, _=None):
        """
        Constructor
        """
        self.__rest_client = RESTClient(http_client, api_key)


    # ----------------------------------------------------------------------------------------------------------------

    def find_for_topic(self, topic, start_date, end_date):
        request_path = '/' + '/'.join((topic, start_date.utc().as_iso8601(True), end_date.utc().as_iso8601(True)))

        # request...
        self.__rest_client.connect()

        try:
            jdict = self.__rest_client.get(request_path)

            # messages...
            collection = MessageResponse.construct_from_jdict(jdict)

            messages = [] if collection is None else collection.items

        finally:
            self.__rest_client.close()

        return messages


    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "MessageManager:{rest_client:%s}" % self.__rest_client
Ejemplo n.º 4
0
 def __init__(self, http_client, api_key, _=None):
     """
     Constructor
     """
     self.__rest_client = RESTClient(http_client, api_key)
Ejemplo n.º 5
0
class BylineManager(object):
    """
    classdocs
    """

    __DEVICE = 'device'
    __TOPIC = 'topic'

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, http_client, api_key):
        """
        Constructor
        """
        self.__rest_client = RESTClient(http_client, api_key)

    # ----------------------------------------------------------------------------------------------------------------

    def find_latest_byline_for_topic(self, topic):
        request_path = '/device-topics'

        params = {self.__TOPIC: topic}

        # request...
        self.__rest_client.connect()

        try:
            jdict = self.__rest_client.get(request_path, params)

            # bylines...
            if jdict is None:
                return None

            latest_byline = None

            for item in jdict:
                byline = Byline.construct_from_jdict(item)

                if latest_byline is None or latest_byline.rec < byline.rec:
                    latest_byline = byline

            return latest_byline

        finally:
            self.__rest_client.close()

    def find_bylines_for_topic(self, topic):
        request_path = '/device-topics'

        params = {self.__TOPIC: topic}

        # request...
        self.__rest_client.connect()

        try:
            jdict = self.__rest_client.get(request_path, params)

            # bylines...
            if jdict is None:
                return []

            return sorted(
                [Byline.construct_from_jdict(item) for item in jdict])

        finally:
            self.__rest_client.close()

    def find_bylines_for_device(self, device):
        request_path = '/device-topics'

        params = {self.__DEVICE: device}

        # request...
        self.__rest_client.connect()

        try:
            jdict = self.__rest_client.get(request_path, params)

            # bylines...
            if jdict is None:
                return []

            return sorted(
                [Byline.construct_from_jdict(item) for item in jdict])

        finally:
            self.__rest_client.close()

    def find_byline_for_device_topic(self, device, topic):
        request_path = '/device-topics'

        params = {self.__DEVICE: device}

        # request...
        self.__rest_client.connect()

        try:
            jdict = self.__rest_client.get(request_path, params)

            # bylines...
            if jdict is None:
                return None

            for item in jdict:
                byline = Byline.construct_from_jdict(item)

                if byline.topic == topic:
                    return byline

            return None

        finally:
            self.__rest_client.close()

    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "BylineManager:{rest_client:%s}" % self.__rest_client
Ejemplo n.º 6
0
class MessageManager(object):
    """
    classdocs
    """

    __TOPIC = 'topic'
    __START = 'startTime'
    __END = 'endTime'
    __REC_ONLY = 'rec_only'

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, http_client, api_key, reporter=None):
        """
        Constructor
        """
        self.__rest_client = RESTClient(http_client, api_key)
        self.__reporter = reporter

    # ----------------------------------------------------------------------------------------------------------------

    def find_for_topic(self, topic, start_date, end_date, rec_only):
        request_path = '/topicMessages'

        params = {
            self.__TOPIC: topic,
            self.__START: start_date.utc().as_iso8601(True),
            self.__END: end_date.utc().as_iso8601(True),
            self.__REC_ONLY: str(rec_only).lower()
        }

        # request...
        self.__rest_client.connect()

        try:
            while True:
                jdict = self.__rest_client.get(request_path, params)

                # messages...
                block = MessageResponse.construct_from_jdict(jdict)

                for item in block.items:
                    yield item

                # report...
                if self.__reporter:
                    self.__reporter.print(params[self.__START], len(block))

                # next request...
                if block.next_url is None:
                    break

                next_url = urlparse(block.next_url)
                next_params = parse_qs(next_url.query)

                params[self.__START] = next_params[self.__START][0]

        finally:
            self.__rest_client.close()

    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "MessageManager:{rest_client:%s, reporter:%s}" % (
            self.__rest_client, self.__reporter)
Ejemplo n.º 7
0
 def __init__(self, http_client, api_key, verbose=False):
     """
     Constructor
     """
     self.__rest_client = RESTClient(http_client, api_key)
     self.__verbose = verbose