Skip to content

Commit

Permalink
Impl delivery/{push, multicast, reply}, and add tests (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
okue committed Jun 20, 2019
1 parent 5ce4d6d commit 616b306
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 6 deletions.
68 changes: 66 additions & 2 deletions linebot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
from .http_client import HttpClient, RequestsHttpClient
from .models import (
Error, Profile, MemberIds, Content, RichMenuResponse, MessageQuotaResponse,
MessageQuotaConsumptionResponse, MessageDeliveryBroadcastResponse, IssueLinkTokenResponse,
IssueChannelTokenResponse,
MessageQuotaConsumptionResponse, IssueLinkTokenResponse, IssueChannelTokenResponse,
MessageDeliveryBroadcastResponse, MessageDeliveryMulticastResponse,
MessageDeliveryPushResponse, MessageDeliveryReplyResponse,
)


Expand Down Expand Up @@ -220,6 +221,69 @@ def get_message_delivery_broadcast(self, date, timeout=None):

return MessageDeliveryBroadcastResponse.new_from_json_dict(response.json)

def get_message_delivery_reply(self, date, timeout=None):
"""Get number of sent reply messages.
https://developers.line.biz/en/reference/messaging-api/#get-number-of-reply-messages
Gets the number of messages sent with the /bot/message/reply endpoint.
:param str date: Date the messages were sent. The format is `yyyyMMdd` (Timezone is UTC+9).
:param timeout: (optional) How long to wait for the server
to send data before giving up, as a float,
or a (connect timeout, read timeout) float tuple.
Default is self.http_client.timeout
:type timeout: float | tuple(float, float)
"""
response = self._get(
'/v2/bot/message/delivery/reply?date={date}'.format(date=date),
timeout=timeout
)

return MessageDeliveryReplyResponse.new_from_json_dict(response.json)

def get_message_delivery_push(self, date, timeout=None):
"""Get number of sent push messages.
https://developers.line.biz/en/reference/messaging-api/#get-number-of-push-messages
Gets the number of messages sent with the /bot/message/push endpoint.
:param str date: Date the messages were sent. The format is `yyyyMMdd` (Timezone is UTC+9).
:param timeout: (optional) How long to wait for the server
to send data before giving up, as a float,
or a (connect timeout, read timeout) float tuple.
Default is self.http_client.timeout
:type timeout: float | tuple(float, float)
"""
response = self._get(
'/v2/bot/message/delivery/push?date={date}'.format(date=date),
timeout=timeout
)

return MessageDeliveryPushResponse.new_from_json_dict(response.json)

def get_message_delivery_multicast(self, date, timeout=None):
"""Get number of sent multicast messages.
https://developers.line.biz/en/reference/messaging-api/#get-number-of-multicast-messages
Gets the number of messages sent with the /bot/message/multicast endpoint.
:param str date: Date the messages were sent. The format is `yyyyMMdd` (Timezone is UTC+9).
:param timeout: (optional) How long to wait for the server
to send data before giving up, as a float,
or a (connect timeout, read timeout) float tuple.
Default is self.http_client.timeout
:type timeout: float | tuple(float, float)
"""
response = self._get(
'/v2/bot/message/delivery/multicast?date={date}'.format(date=date),
timeout=timeout
)

return MessageDeliveryMulticastResponse.new_from_json_dict(response.json)

def get_profile(self, user_id, timeout=None):
"""Call get profile API.
Expand Down
3 changes: 3 additions & 0 deletions linebot/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
MessageQuotaResponse,
MessageQuotaConsumptionResponse,
MessageDeliveryBroadcastResponse,
MessageDeliveryReplyResponse,
MessageDeliveryMulticastResponse,
MessageDeliveryPushResponse,
Content as MessageContent, # backward compatibility,
IssueLinkTokenResponse,
IssueChannelTokenResponse,
Expand Down
56 changes: 52 additions & 4 deletions linebot/models/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ def __init__(self, total_usage=None, **kwargs):


class MessageDeliveryBroadcastResponse(Base):
"""MessageDeliveryBroadcastResponse.
https://developers.line.biz/en/reference/messaging-api/#get-number-of-broadcast-messages
"""
"""MessageDeliveryBroadcastResponse."""

def __init__(self, status=None, success=None, **kwargs):
"""__init__ method.
Expand All @@ -209,6 +206,57 @@ def __init__(self, status=None, success=None, **kwargs):
self.success = success


class MessageDeliveryReplyResponse(Base):
"""MessageDeliveryReplyResponse."""

def __init__(self, status=None, success=None, **kwargs):
"""__init__ method.
:param str status: Status of the counting process.
:param int success: The number of messages sent with the Messaging API on the
date specified in date.
:param kwargs:
"""
super(MessageDeliveryReplyResponse, self).__init__(**kwargs)

self.status = status
self.success = success


class MessageDeliveryPushResponse(Base):
"""MessageDeliveryPushResponse."""

def __init__(self, status=None, success=None, **kwargs):
"""__init__ method.
:param str status: Status of the counting process.
:param int success: The number of messages sent with the Messaging API on the
date specified in date.
:param kwargs:
"""
super(MessageDeliveryPushResponse, self).__init__(**kwargs)

self.status = status
self.success = success


class MessageDeliveryMulticastResponse(Base):
"""MessageDeliveryMulticastResponse."""

def __init__(self, status=None, success=None, **kwargs):
"""__init__ method.
:param str status: Status of the counting process.
:param int success: The number of messages sent with the Messaging API on the
date specified in date.
:param kwargs:
"""
super(MessageDeliveryMulticastResponse, self).__init__(**kwargs)

self.status = status
self.success = success


class IssueLinkTokenResponse(Base):
"""IssueLinkTokenResponse.
Expand Down
109 changes: 109 additions & 0 deletions tests/api/test_get_delivery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*-

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import unicode_literals, absolute_import

import unittest

import responses

from linebot import (
LineBotApi
)


class TestLineBotApi(unittest.TestCase):
def setUp(self):
self.tested = LineBotApi('channel_secret')
self.date = '20190101'

@responses.activate
def test_get_delivery_broadcast(self):
responses.add(
responses.GET,
LineBotApi.DEFAULT_API_ENDPOINT +
'/v2/bot/message/delivery/broadcast?date={}'.format(self.date),
json={
'status': 'ready',
'success': 10000
},
status=200
)

res = self.tested.get_message_delivery_broadcast(self.date)
request = responses.calls[0].request
self.assertEqual('GET', request.method)
self.assertEqual('ready', res.status)
self.assertEqual(10000, res.success)

@responses.activate
def test_get_delivery_push(self):
responses.add(
responses.GET,
LineBotApi.DEFAULT_API_ENDPOINT +
'/v2/bot/message/delivery/push?date={}'.format(self.date),
json={
'status': 'ready',
'success': 10000
},
status=200
)

res = self.tested.get_message_delivery_push(self.date)
request = responses.calls[0].request
self.assertEqual('GET', request.method)
self.assertEqual('ready', res.status)
self.assertEqual(10000, res.success)

@responses.activate
def test_get_delivery_reply(self):
responses.add(
responses.GET,
LineBotApi.DEFAULT_API_ENDPOINT +
'/v2/bot/message/delivery/reply?date={}'.format(self.date),
json={
'status': 'ready',
'success': 10000
},
status=200
)

res = self.tested.get_message_delivery_reply(self.date)
request = responses.calls[0].request
self.assertEqual('GET', request.method)
self.assertEqual('ready', res.status)
self.assertEqual(10000, res.success)

@responses.activate
def test_get_delivery_multicast(self):
responses.add(
responses.GET,
LineBotApi.DEFAULT_API_ENDPOINT +
'/v2/bot/message/delivery/multicast?date={}'.format(self.date),
json={
'status': 'ready',
'success': 10000
},
status=200
)

res = self.tested.get_message_delivery_multicast(self.date)
request = responses.calls[0].request
self.assertEqual('GET', request.method)
self.assertEqual('ready', res.status)
self.assertEqual(10000, res.success)


if __name__ == '__main__':
unittest.main()

0 comments on commit 616b306

Please sign in to comment.